Reputation: 3
Thank you to anyone who reads this, in advance.
When I call the function (report) to print out all of the members of the subclass, the fields either come back with strange characters, correct data then strange characters, and sometimes 0's in the non-char variables.
The purpose of this is to prove that I can make a functioning inherited class.
I think I'm probably just not doing something correctly as I'm new to programming. Try not to face-palm too hard. :)
Here's what I have:
// classes
// base class
class Cd
{
private:
protected:
char performers[50];
char label[60];
int selections;
double playtime;
public:
Cd(char * s1, char * s2, int n, double x);
Cd(const Cd & d);
Cd();
~Cd();
void Report() const; // reports all CD data
Cd & operator=(const Cd & d);
};
class Classic : public Cd // Sub-class
{
public:
Classic();
Classic(char * s0, char * s1, char * s2, int n, double x);
~Classic();
Classic & operator=(const Classic & c);
void Report() const;
private:
char primaryWork[50];
};
// Methods
inline Cd::Cd(char * s1,char * s2, int n, double x) // base class constructor
{
std::strncpy(performers, s1, 49);
std::strncpy(label, s2, 59);
selections = n;
playtime = x;
}
inline Classic::Classic(char * s0, char * s1, char * s2, int n, double x)
{
std::strncpy(performers, s1, 41);
std::strncpy(label, s2, 59);
selections = n;
playtime = x;
std::strncpy(primaryWork, s0, 49);
}
Btw, I know this hardly looks like I'm not reusing any code whatsoever. This is just the closest I've gotten it to work. I've also tried (among other variations):
Classic(char * s0, const Cd & c); // constructor declaration
inline Classic::Classic(char * s0, const Cd & c) : Cd(c) // constructor definition
{
std::strncpy(primaryWork, s0, 49);
}
but I get the "no overloaded function takes 5 arguments. Anyway, here's the main() code:
Classic c2("Piano Sonata in B flat, Fantasia in C",
"698 C++ PRIMER PLUS, FIFTH EDITION Alfred Brendel", "Philips", 2, 57.17);
c2.Report();
Report function's definition.
inline void Classic::Report() const
{
cout << endl << "Performers: " << performers << endl;
cout << "Label: " << label << endl;
cout << "Number of Selections: " << selections << endl;
cout << "Playtime: " << playtime << endl;
cout << "Primary Work: " << primaryWork << endl;
}
That's just the first piece, but it's already failing by that point.
Please, any input is welcome. And, thank you again.
Upvotes: 0
Views: 92
Reputation: 7881
You have to tell C++ which constructor to use, else it will default to using the default constructor:
inline Classic::Classic(char * s0, char * s1, char * s2, int n, double x) :
Cd(s1,s2,n,x)
{
std::strncpy(primaryWork, s0, 49);
}
Should do what you want
Upvotes: 2