第一题:
// 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)课后习题的更多相关文章

  1. C++ Primer Plus 学习之 类继承

    主要介绍了类的继承.虚函数.类继承的动态内存分配问题.继承与友元函数. 公有派生 基类的公有成员和私有成员都会成为派生类的一部分. 基类的私有成员只能通过基类的公有或者保护方法访问.但是,基类指针或引 ...

  2. 《C++ Primer Plus》读书笔记之十一—类继承

    第十三章 类继承 1.类继承:扩展和修改类. 2.公有继承格式:冒号指出B类的基类是A,B是派生类. class B :public A { ... }: 3.派生类对象包含基类对象.使用公有派生,基 ...

  3. 《C++ Primer Plus》第13章 类继承 笔记

    类继承通过使用已有的类(基类)定义新的来(派生类),使得能够根据需要修改编程代码.共有继承建立is-a关系,这意味着派生类对象也应该是某种基类对象.作为is-a模型的一部分,派生类继承基类的数据称源和 ...

  4. C++ primer plus读书笔记——第13章 类继承

    第13章 类继承 1. 如果购买厂商的C库,除非厂商提供库函数的源代码,否则您将无法根据自己的需求,对函数进行扩展或修改.但如果是类库,只要其提供了类方法的头文件和编译后的代码,仍可以使用库中的类派生 ...

  5. 视觉slam十四讲第七章课后习题7

    版权声明:本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/newneul/p/8544369.html  7.题目要求:在ICP程序中,将空间点也作为优化变量考虑进来 ...

  6. C++基础——类继承中方法重载

    一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...

  7. C++基础——类继承

    一.前言  好吧,本系列博客已经变成了<C++ Primer Plus>的读书笔记,尴尬.在使用C语言时,多通过添加库函数的方式实现代码重用,但有一个弊端就是原来写好的代码并不完全适用于现 ...

  8. OpenCV学习笔记之课后习题练习2-5

    5.对练习4中的代码进行修改,参考例2-3,给程序加入滚动条,使得用户可以动态调节缩放比例,缩放比例的取值为2-8之间.可以跳过写入磁盘操作,但是必须将变换结果显示在窗口中. 参考博文:blog.cs ...

  9. 20145329 《JAVA程序设计》课后习题代码编写总结

    20145329<Java程序设计>课后习题学习总结 学习内容总结 package cc.openhome; public class Hello2 { public static voi ...

  10. C++面向程序设计(第二版)课后习题答案解析

    最近没什么心情整理零散的知识点,就整理一下第四章的课后习题答案. 1.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员函数,非友元的普通函数.编程序, ...

随机推荐

  1. Windows 10 与Windows11 功能比较(红字为不同点)

    Windows 10 与Windows11 功能比较(红字为不同点)

  2. macOS Big Sur 设置JAVA_HOME

    默认JAVA_HOME指向的是: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home 这个不是我们自己安装的jdk,另外本 ...

  3. lua-携程

    function SayHey(mag) for i = 1 , 3 doprint(mag)coroutine.yield()end end --创建携程(协同) coFunc= coroutine ...

  4. clear_buff-cache.sh

    #! /bin/bash sync;echo 1 > /proc/sys/vm/drop_caches # 表示清除pagecache sync;echo 2 > /proc/sys/vm ...

  5. csec的key更新

    在对csec的使用中(其他遵循hsm key update协议的芯片也适用),kdf的运算过程中遇到的数据都是128bit.不需要考虑padding的问题.目前并没有找到对padding的一致性的处理 ...

  6. RSTP-快速生成树协议

    1 STP的不足之处STP协议虽然能够解决环路问题,但是由于网络拓扑收敛慢,影响了用户通信质量. 2 RSTP概述RSTP在许多方面对STP进行了优化,它的收敛速度更快,而且能够兼容STP. 通过接口 ...

  7. base64 转图片

    data:image+jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg% ...

  8. php微信公众号开发之生成带参数的二维码

    参考微信公众平台:https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric ...

  9. 封装python代码,避免被轻易反编译

    可使用Cython对python代码进行封装,封装成.pyd库,大致流程可参考: cython打包py成pyd,pyinstaller打包uvicorn服务过程记录_Bolly_He的博客-CSDN博 ...

  10. css - 预编译less下,解决深度选择器失效问题,完成css样式修改

    #若深度选择器有效.使用此可修改样式 /deep/ .cube-btn{ //...自定义css样式 } #深度选择器失效,则: 1.重新定义deep深度选择器 @deep:~'>>> ...