类继承(c++ primer plus)课后习题

2023-03-07,,

第一题:

// 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, intn, doublex);
Cd(const Cd & d);
Cd();
~Cd();
void Report() const; // reports all CD data
Cd & operator=(const Cd & d);
};

派生出一个Classic类,并添加一组char成员,用于存储指出CD中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:

cd.h

#ifndef CD_H_
#define CD_H_ #include <iostream> class Cd
{ private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(char const*s1,char const * s2,int n,double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report()const;
virtual Cd & operator=(const Cd & d);
}; class Classic:public Cd
{
private:
char * filename;
public:
Classic(char const*s1,char const*s2,char const* s3,int n,double x);
Classic(const Cd & d,char const* s3);
Classic(const Classic & dt);
Classic();
virtual ~ Classic();
virtual void Report()const;
virtual Classic & operator=(const Classic & d);
}; #endif

cd1.cpp

#include <iostream>
#include "cd.h"
#include <cstring> Cd::Cd(char const* s1,char const* s2,int n,double x)
{
std::strcpy(performers,s1);
std::strcpy(label,s2);
selections=n;
playtime=x;
} Cd::Cd()
{ std::strcpy(performers, "null");
std::strcpy(label, "null");
selections = 0;
playtime = 0;
} Cd::Cd(const Cd & d)
{
std::strcpy(performers,d.performers);
std::strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
} Cd::~Cd()
{
} void Cd::Report()const
{
std::cout << "Performers: " << performers << std::endl;
std::cout << "Label: " << label << std::endl;
std::cout << "Selections: " << selections << std::endl;
std::cout << "Playtime: " << playtime << std::endl;
} Cd & Cd::operator=(const Cd & d)
{
std::strcpy(performers, d.performers);
std::strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return * this;
} Classic::Classic(char const*s1,char const* s2,char const* s3,int n,double x):Cd(s1,s2,n,x)
{ std::strcpy(filename,s3);
} Classic::Classic(const Cd & d,char const*s3):Cd(d)
{ strcpy(filename,s3);
} Classic::Classic(const Classic & dt):Cd(dt)
{
strcpy(filename,dt.filename);
} Classic::Classic():Cd()
{
filename=std::strcpy(filename,"null");
} Classic::~Classic()
{ } void Classic::Report()const
{
Cd::Report();
std::cout<<"Filename"<<filename<<std::endl;
} Classic & Classic::operator=(const Classic & d)
{
Cd::operator=(d);
std::strcpy(filename,d.filename);
return * this;
}

User_cd.cpp

#include <iostream>
using namespace std;
#include "cd.h"
#include <cstring>
#include <cctype>
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",
"Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1; cout << "Using object directly:\n";
c1.Report();
c2.Report(); cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report(); cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2); cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report(); } void Bravo(const Cd & disk)
{
disk.Report();
}

第二题:

// 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, intn, doublex);
Cd(const Cd & d);
Cd();
~Cd();
void Report() const; // reports all CD data
Cd & operator=(const Cd & d);
};

派生出一个Classic类,并添加一组char成员,用于存储指出CD中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:(用动态数组的方式来写)

cd1.h

#ifndef CD_H_
#define CD_H_ #include <iostream> class Cd
{ private:
char *performers;
char *label;
int selections;
double playtime;
public:
Cd(char const *s1,char const * s2,int n,double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report()const;
virtual Cd & operator=(const Cd & d);
}; class Classic:public Cd
{
private:
char * filename;
public:
Classic(char const *s1,char const * s2,char const * s3,int n,double x);
Classic(const Cd & d,char const * s3);
Classic(const Classic & dt);
Classic();
virtual ~ Classic();
virtual void Report()const;
virtual Classic & operator=(const Classic & d);
}; #endif

cd1.cpp

#include <iostream>
#include "cd.h"
#include <cstring> Cd::Cd(char const * s1,char const * s2,int n,double x)
{
int len1=std::strlen(s1);
int len2=std::strlen(s2);
performers=new char[len1+1];
label=new char[len2+1];
std::strcpy(performers,s1);
std::strcpy(label,s2);
selections=n;
playtime=x;
} Cd::Cd()
{
int null_length = std::strlen("null");
performers = new char[null_length + 1];
label = new char[null_length + 1]; std::strcpy(performers, "null");
std::strcpy(label, "null");
selections = 0;
playtime = 0;
} Cd::Cd(const Cd & d)
{
int len1=std::strlen(d.performers);
int len2=std::strlen(d.label);
performers=new char [len1+1];
label=new char [len2+1];
std::strcpy(performers,d.performers);
std::strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
} Cd::~Cd()
{
delete [] performers;
delete [] label;
performers=nullptr;
label=nullptr;
} void Cd::Report()const
{
std::cout << "Performers: " << performers << std::endl;
std::cout << "Label: " << label << std::endl;
std::cout << "Selections: " << selections << std::endl;
std::cout << "Playtime: " << playtime << std::endl;
} Cd & Cd::operator=(const Cd & d)
{
if(performers==nullptr&&label==nullptr)
{
Cd(d);
}
else
{
delete [] performers;
delete [] label;
Cd(d);
}
return * this;
} Classic::Classic(char const *s1,char const * s2,char const * s3,int n,double x):Cd(s1,s2,n,x)
{
int len1=strlen(s3);
filename=new char [len1+1];
std::strcpy(filename,s3);
} Classic::Classic(const Cd & d,char const *s3):Cd(d)
{
int len1=std::strlen(s3);
filename=new char [len1+1];
strcpy(filename,s3);
} Classic::Classic(const Classic & dt):Cd(dt)
{
int len1=std::strlen(dt.filename);
filename=new char [len1+1];
strcpy(filename,dt.filename);
} Classic::Classic():Cd()
{
int len1=std::strlen("null");
filename=std::strcpy(filename,"null");
} Classic::~Classic()
{
delete [] filename;
filename=nullptr;
} void Classic::Report()const
{
Cd::Report();
std::cout<<"Filename"<<filename<<std::endl;
} Classic & Classic::operator=(const Classic & d)
{
Cd::operator=(d);
delete [] filename;
filename=nullptr;
int len=std::strlen(d.filename);
std::strcpy(filename,d.filename);
return * this;
}

User_cd1.cpp

#include <iostream>
using namespace std;
#include "cd.h"
#include <cstring>
#include <cctype>
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",
"Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1; cout << "Using object directly:\n";
c1.Report();
c2.Report(); cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report(); cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2); cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report(); return ;
} void Bravo(const Cd & disk)
{
disk.Report();
}

第三题

dma.h

#ifndef DMA_H_
#define DMA_H_
#include <iostream> class DMA
{
public:
DMA();
~DMA();
virtual void View()=0;
}; class baseDMA:public DMA
{
private:
char * label;
int rating;
public:
baseDMA(const char * l="null",int r=0);
baseDMA(const baseDMA & rs);
virtual ~baseDMA();
baseDMA & operator=(const baseDMA & rs);
friend std::ostream & operator<<(std::ostream & os,const baseDMA & rs);
virtual void View();
}; class lacksDMA:public baseDMA
{
private:
enum {COL_LEN=40};
char color[COL_LEN];
public:
lacksDMA(const char *c="blank",const char * l="null",int r=0);
lacksDMA(const char * c,const baseDMA & rs);
friend std::ostream & operator<<(std::ostream & os,const lacksDMA &rs);
virtual void View();
}; class hasDMA:public baseDMA
{
private:
char * style;
public:
hasDMA(const char * s="none",const char * l="null",int r=0);
hasDMA(const char *s,const baseDMA & rs);
hasDMA(const hasDMA & hs);
~hasDMA();
hasDMA & operator=(const hasDMA &rs);
friend std::ostream & operator<<(std::ostream & os,const hasDMA & rs);
virtual void View();
}; #endif

dma.cpp

#include <iostream>
#include <cstring>
#include <cstdlib>
#include "dma.h" DMA::DMA()
{ } DMA::~DMA()
{ } baseDMA::baseDMA(const char *l,int r)
{
int len=std::strlen(l);
label=new char [len+1];
std::strcpy(label,l);
rating=r;
} baseDMA::baseDMA(const baseDMA & rs)
{
int len=std::strlen(rs.label);
label=new char [len+1];
std::strcpy(label,rs.label);
rating=rs.rating;
} baseDMA::~baseDMA()
{
delete [] label;
label=nullptr;
} baseDMA & baseDMA::operator=(const baseDMA & rs)
{
if(label==nullptr)
{
baseDMA(rs);
}
else
{
delete [] label;
baseDMA(rs);
}
return *this;
} std::ostream & operator<<(std::ostream & os,const baseDMA & rs)
{
os<<"Label="<<rs.label<<std::endl;
os<<"Rating="<<rs.rating<<std::endl;
return os;
} void baseDMA::View()
{
std::cout<<"Label="<<label<<std::endl;
std::cout<<"Rating="<<rand<<std::endl;
} lacksDMA::lacksDMA(const char *c,const char * l,int r):baseDMA(c,r)
{
std::strcpy(color,l); } lacksDMA::lacksDMA(const char *c,const baseDMA & rs):baseDMA(rs)
{
std::strcpy(color,c);
} std::ostream & operator<<(std::ostream & os,const lacksDMA & rs)
{
operator<<(os,(baseDMA)rs)<<std::endl;
std::cout<<"color="<<rs.color<<std::endl;
return os;
} void lacksDMA::View()
{
baseDMA::View();
std::cout<<"color="<<color<<std::endl;
} hasDMA::hasDMA(const char * s,const char * l,int r):baseDMA(l,r)
{
int len=std::strlen(s);
style=new char [len+1];
std::strcpy(style,s);
} hasDMA::hasDMA(const char * s,const baseDMA & rs):baseDMA(rs)
{
int len=std::strlen(s);
style=new char [len+1];
std::strcpy(style,s);
} hasDMA::hasDMA(const hasDMA & hs):baseDMA(hs)
{
int len=std::strlen(hs.style);
style=new char [len+1];
std::strcpy(style,hs.style);
} hasDMA::~hasDMA()
{
delete [] style;
style=nullptr;
} hasDMA & hasDMA::operator=(const hasDMA &rs)
{
baseDMA::operator=(rs);
delete[] style; int style_length = std::strlen(rs.style);
style = new char[style_length + 1]; std::strcpy(style, rs.style); return *this;
} std::ostream & operator<<(std::ostream & os, const hasDMA & rs)
{
operator<<(os, baseDMA(rs)) << std::endl;
os << "Style: " << rs.style; return os;
} void hasDMA::View()
{
baseDMA::View();
std::cout << "Style: " << style << std::endl;
}

User_dma.cpp

#include <iostream>
#include "dma.h"
const int CLIMENTS=5;
#include <cstring>
using namespace std;
int main()
{ baseDMA * dma[CLIMENTS];
std::string label;
int rating;
char choice;
std::string color;
std::string style; for(int i = 0; i < CLIMENTS; i++)
{
cout << "Enter lable: ";
getline(cin, label);
cout << "Enter rating: ";
cin >> rating;
cout << "Enter 1 for baseDMA or 2"
" for lacksDMA or 3 for hasDMA: ";
while (cin >> choice && (choice != '1' && choice != '2' && choice != '3'))
{
cout << "Enter 1 or 2 or 3: ";
}
while (cin.get() != '\n');
if (choice == '1')
{
dma[i] = new baseDMA(label.c_str(), rating);
}
else if (choice == '2')
{
cout << "Enter color: ";
getline(cin, color);
dma[i] = new lacksDMA(color.c_str(), label.c_str(), rating);
}
else
{
cout << "Enter style: ";
getline(cin, style);
dma[i] = new hasDMA(style.c_str(), label.c_str(), rating);
} }
cout << endl;
for (int i = 0; i < CLIMENTS; i++)
{
dma[i]->View();
cout << endl;
} for (int i = 0; i < CLIMENTS; i++)
{
delete dma[i];
} cout << "Done.\n";
}

第四题

题目太长了,就省略了

potr.h

#ifndef PORT_H_
#define PORT_H_
#include <iostream> using namespace std; class port
{
private:
char * brand;
char style[20];
int bottles;
public:
port(const char *br="none",const char * st="none",int b=0);
port(const port & p);
virtual ~port(){delete [] brand;}
port & operator=(const port & p);
port & operator+=(int b);
port & operator-=(int b);
int BottleCount()const{return bottles;}
virtual void Show()const;
friend ostream & operator<<(ostream & os,const port & p);
}; class Vintageport:public port
{
private:
char * nickname;
int year;
public:
Vintageport();
Vintageport(const char * br,int b,const char * nn,int y);
Vintageport(const Vintageport & vp);
~Vintageport(){delete [] nickname;}
Vintageport & operator =(const Vintageport & vp);
void Show()const;
friend ostream & operator <<(ostream & os,const Vintageport & vp);
}; #endif

port.cpp

#include <iostream>
#include <cstring>
#include <cstdlib>
#include "port.h" port::port(const char * br,const char * st,int b)
{
int len=std::strlen(br);
brand=new char [len+1];
std::strcpy(brand,br);
std::strcpy(style,st);
bottles=b;
} port::port(const port & p)
{
int len=std::strlen(p.brand);
brand=new char [len+1];
std::strcpy(brand,p.brand);
std::strcpy(style,p.style);
bottles=p.bottles;
} port & port::operator=(const port & p)
{
delete [] brand;
int len=std::strlen(p.brand);
brand=new char [len+1];
std::strcpy(brand,p.brand);
std::strcpy(style,p.style);
bottles=p.bottles;
return *this;
} port & port::operator+=(int b)
{
bottles+=b;
return *this;
} port & port::operator-=(int b)
{
bottles-=b;
return * this;
} void port::Show()const
{
std::cout<<"Brand="<<brand<<std::endl;
std::cout<<"Style="<<style<<std::endl;
std::cout<<"Bottles="<<bottles<<std::endl;
} std::ostream & operator<<(ostream & os,const port & p)
{
os<<p.brand<<","<<p.style<<","<<p.bottles<<std::endl;
return os;
} Vintageport::Vintageport()
{ } Vintageport::Vintageport(const char *br,int b,const char * nn,int y):port(br,"Vintage",b)
{
int len=std::strlen(nn);
nickname=new char [len+1];
strcpy(nickname,nn);
year=y;
} Vintageport::Vintageport(const Vintageport & vp):port(vp)
{
int len =std::strlen(vp.nickname);
nickname=new char [len+1];
std::strcpy(nickname,vp.nickname);
year=vp.year;
} Vintageport & Vintageport::operator=(const Vintageport & vp)
{
delete [] nickname;
port::operator=(vp);
int len=std::strlen(vp.nickname);
nickname=new char [len+1];
std::strcpy(nickname,vp.nickname);
year=vp.year;
return *this;
} void Vintageport::Show()const
{
port::Show();
std::cout<<"Nickname="<<nickname<<std::endl;
std::cout<<"Year="<<year<<endl;
} std::ostream & operator<<(ostream & os,const Vintageport & vp)
{
os<<port(vp);
os<<","<<vp.nickname<<","<<vp.year<<std::endl;
return os;
}

User_port.cpp

#include <iostream>
#include <cstring>
#include <cstdlib>
#include "port.h" int main()
{
port port1("Gallo", "tawny", 20);
Vintageport vp("None", 20, "The Noble", 1997); port1.Show();
vp.Show(); Vintageport vp2 = vp;
port port2 = port1; cout << vp2 << endl;
cout << port2 << endl;
}

 

继承(c++ primer plus)课后习题的相关教程结束。

《类继承(c++ primer plus)课后习题.doc》

下载本文的Word格式文档,以方便收藏与打印。