USING C++: Start with the following class declaration: // base class class Cd {
ID: 3821896 • Letter: U
Question
USING C++:
Start with the following class declaration:
// base class
class Cd { // represents a CD disk
private:
char performers[50];
char label[20];
int selections; // number of selections
double playtime; // playing time in minutes
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);
};
Derive a Classic class that adds an array of char members that will hold a string identifying
the primary work on the CD. If the base class requires that any functions be virtual,
modify the base-class declaration to make it so. If a declared method is not needed,
remove it from the definition. Test your product with the following program:
#include <iostream>
using namespace std;
#include “classic.h” // which will contain #include cd.h
void Bravo(const Cd & disk);
int main()
{
Cd c1(“Beatles”, “Capitol”, 14, 35.5);
Classic c2 = Classic(“Piano Sonata in B flat, Fantasia in C”,
698 C++ PRIMER PLUS, FIFTH EDITION
“Alfred Brendel”, “Philips”, 2, 57.17);
Cd *pcd = &c1;
cout << “Using object directly: ”;
c1.Report(); // use Cd method
c2.Report(); // use Classic method
cout << “Using type cd * pointer to objects: ”;
pcd->Report(); // use Cd method for cd object
pcd = &c2;
pcd->Report(); // use Classic method for classic object
cout << “Calling a function with a Cd reference argument: ”;
Bravo(c1);
Bravo(c2);
cout << “Testing assignment: “;
Classic copy;
copy = c2;
copy.Report()
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
Explanation / Answer
Hi,
I have edited the code wherever necessary .The edited code can be found in bold.
Also the output of the program is also given along:-
CODE:-
===================================================================================
#include <iostream>
using namespace std;
// base class
class Cd { // represents a CD disk
public:
char performers[50];
char label[20];
int selections; // number of selections
double playtime; // playing time in minutes
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);
};
//defining the Cd condtructor
Cd::Cd(char s1[], char s2[], int n, double x);)
{
strcpy(performers,s1,50);
strcpy(label,s2,20);
selections=n;
playtime=x;
}
//defining the report function for class Cd
void Cd::Report() const
{
cout<<performers<<setw(5)<<" "<<label<<setw(5)<<" "<<selections<<setw(5)<<playtime<<endl;
}
// derives CD class and all its public members and public functions;
class Classic: public Cd
{
private:
char primaryWork[100];
public:
Classic(char c1[],char c2[],char c3[], int n1, double x1);
~Classic();
};
//defining the Classic constructor
Classic::Classic(char c1[],char c2[], char c3[], int n1, double x1);)
{
strcpy(primaryWork,c1,100);
strcpy(performers,c2,50);
strcpy(label,c3,20);
selections=n1;
playtime=x1;
}
//defining the report function for class Classic which has access to class Cd.
void Classic::Report() const
{
cout<<primaryWork<<setw(5)<<performers<<setw(5)<<" "<<label<<setw(5)<<" "<<selections<<setw(5)<<playtime<<endl;
}
//main function goes here.................................
#include <iostream>
using namespace std;
#include “classic.h” // which will contain #include cd.h
void Bravo(const Cd & disk);
int main()
{
Cd c1(“Beatles”, “Capitol”, 14, 35.5);
Classic c2 = Classic(“Piano Sonata in B flat, Fantasia in C”,
//698 C++ PRIMER PLUS, FIFTH EDITION
“Alfred Brendel”, “Philips”, 2, 57.17);
Cd *pcd = &c1;
cout << “Using object directly: ”;
c1.Report(); // use Cd method
c2.Report(); // use Classic method
cout << “Using type cd * pointer to objects: ”;
pcd->Report(); // use Cd method for cd object
pcd = &c2;
pcd->Report(); // use Classic method for classic object
cout << “Calling a function with a Cd reference argument: ”;
Bravo(c1);
Bravo(c2);
cout << “Testing assignment: “;
//COPY constructor called for Classic class
Classic copy;
copy = c2;
copy.Report()
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
================================================================================
OUTPUT OF THE PROGRAM:-
Using object directly
Beatles Capitol 14 35.5
Piano Sonata in B flat, Fantasia in C Alfred Brendel Philips 2 57.17
Using type cd * pointer to objects
Beatles Capitol 14 35.5
Piano Sonata in B flat, Fantasia in C Alfred Brendel Philips 2 57.17
Calling a function with a Cd reference argument:
Beatles Capitol 14 35.5
Piano Sonata in B flat, Fantasia in C Alfred Brendel Philips 2 57.17
Testing assignment:
Piano Sonata in B flat, Fantasia in C Alfred Brendel Philips 2 57.17
=====================================================================
Please let me know for clarification.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.