User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,468 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,948 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 250 | Replies: 4 | Solved
Reply
Join Date: Mar 2007
Posts: 36
Reputation: titaniumdecoy is on a distinguished road 
Rep Power: 2
Solved Threads: 4
titaniumdecoy's Avatar
titaniumdecoy titaniumdecoy is offline Offline
Light Poster

Default copy constructor

  #1  
Jul 7th, 2008
The default copy constructor should work for the Polynomial class below because it does not contain any dynamic data types. However, when I create a new Polynomial via the copy constructor, it prints 0 for the value of each coefficient. How can I fix this?
#include <iostream>
#include <vector>

class Polynomial
{
  public:
    Polynomial(int val = 0, int exp = 0) : coefs(0, exp), degree(exp)
    { 
        coefs[exp] = val; 
    }
    
    friend std::ostream& operator<< (std::ostream& os, const Polynomial& poly)
    {
        for (int i = poly.degree; i >= 0; i--) {
            os << poly.coefs[i];
            if (i > 1)
                os << "t^" << i;
            if (i == 1)
                os << "t";
            if (i != 0)
                os << " + ";
        }
        return os;
    }
    
  protected:
    std::vector<int> coefs;
    int degree;
};

int main()
{
    Polynomial p(5, 2);
    
    std::cout << "p: " << p << std::endl;
    
    Polynomial k = p;
    
    std::cout << "k: " << k << std::endl;
    
    return 0;
}
Output:
p: 5t^2 + 7t + 0
k: 0t^2 + 0t + 0
Last edited by titaniumdecoy : Jul 7th, 2008 at 5:45 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,839
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Default copy constructor

  #2  
Jul 7th, 2008
I don't know how you got that far...

You never initialize coeffs. You must set its length before playing with any elements. Or push elts onto the end.

Hope this helps.
Reply With Quote  
Join Date: Mar 2007
Posts: 36
Reputation: titaniumdecoy is on a distinguished road 
Rep Power: 2
Solved Threads: 4
titaniumdecoy's Avatar
titaniumdecoy titaniumdecoy is offline Offline
Light Poster

Re: Default copy constructor

  #3  
Jul 7th, 2008
Originally Posted by Duoas View Post
You never initialize coeffs.

coefs is initialized in the initializer list of the constructor:

Polynomial(int val = 0, int exp = 0) : coefs(0, exp), degree(exp)

Am I missing something? Thanks.
Last edited by titaniumdecoy : Jul 7th, 2008 at 6:30 pm.
Reply With Quote  
Join Date: Mar 2007
Posts: 36
Reputation: titaniumdecoy is on a distinguished road 
Rep Power: 2
Solved Threads: 4
titaniumdecoy's Avatar
titaniumdecoy titaniumdecoy is offline Offline
Light Poster

Re: Default copy constructor

  #4  
Jul 7th, 2008
I figured it out. I was calling the std::vector constructor with the arguments reversed (as well as providing an incorrect initial size). The solution:

Polynomial(int val = 0, int exp = 0) : coefs(exp + 1, 0), degree(exp)
Last edited by titaniumdecoy : Jul 7th, 2008 at 7:30 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,839
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Default copy constructor

  #5  
Jul 7th, 2008
You got it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC