•
•
•
•
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
![]() |
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?
Output:
#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;
}p: 5t^2 + 7t + 0 k: 0t^2 + 0t + 0
Last edited by titaniumdecoy : Jul 7th, 2008 at 5:45 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,839
Reputation:
Rep Power: 11
Solved Threads: 189
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Member function copy() ??? (C++)
- help! how to implement a copy constructor in c++ for ADT queues?? (C++)
- Need of Default Copy Constructor (C++)
- the common usage of a copy constructor (C++)
- copy constructor and 2 args constructor help (C)
- Copy Constructor help (C++)
- (C++) Writing a Copy Constructor?!? (C++)
Other Threads in the C++ Forum
- Previous Thread: i++ vs. ++i?
- Next Thread: logic or format nightmare



Linear Mode