类继承(c++ primer plus)课后习题
// 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 学习之 类继承
主要介绍了类的继承.虚函数.类继承的动态内存分配问题.继承与友元函数. 公有派生 基类的公有成员和私有成员都会成为派生类的一部分. 基类的私有成员只能通过基类的公有或者保护方法访问.但是,基类指针或引 ...
- 《C++ Primer Plus》读书笔记之十一—类继承
第十三章 类继承 1.类继承:扩展和修改类. 2.公有继承格式:冒号指出B类的基类是A,B是派生类. class B :public A { ... }: 3.派生类对象包含基类对象.使用公有派生,基 ...
- 《C++ Primer Plus》第13章 类继承 笔记
类继承通过使用已有的类(基类)定义新的来(派生类),使得能够根据需要修改编程代码.共有继承建立is-a关系,这意味着派生类对象也应该是某种基类对象.作为is-a模型的一部分,派生类继承基类的数据称源和 ...
- C++ primer plus读书笔记——第13章 类继承
第13章 类继承 1. 如果购买厂商的C库,除非厂商提供库函数的源代码,否则您将无法根据自己的需求,对函数进行扩展或修改.但如果是类库,只要其提供了类方法的头文件和编译后的代码,仍可以使用库中的类派生 ...
- 视觉slam十四讲第七章课后习题7
版权声明:本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/newneul/p/8544369.html 7.题目要求:在ICP程序中,将空间点也作为优化变量考虑进来 ...
- C++基础——类继承中方法重载
一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...
- C++基础——类继承
一.前言 好吧,本系列博客已经变成了<C++ Primer Plus>的读书笔记,尴尬.在使用C语言时,多通过添加库函数的方式实现代码重用,但有一个弊端就是原来写好的代码并不完全适用于现 ...
- OpenCV学习笔记之课后习题练习2-5
5.对练习4中的代码进行修改,参考例2-3,给程序加入滚动条,使得用户可以动态调节缩放比例,缩放比例的取值为2-8之间.可以跳过写入磁盘操作,但是必须将变换结果显示在窗口中. 参考博文:blog.cs ...
- 20145329 《JAVA程序设计》课后习题代码编写总结
20145329<Java程序设计>课后习题学习总结 学习内容总结 package cc.openhome; public class Hello2 { public static voi ...
- C++面向程序设计(第二版)课后习题答案解析
最近没什么心情整理零散的知识点,就整理一下第四章的课后习题答案. 1.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员函数,非友元的普通函数.编程序, ...
随机推荐
- ider git Reset Type 使用记录
Soft:在选定提交点之后所做的所有更改都将被暂存(这意味着可以到 Version Control 窗口(Alt+9)的Local Changes 选项卡,以便您可以查看它们,并在必要时稍后提交). ...
- [转载]OpenCV中的channel是什么意思?
转载自https://answers.opencv.org/question/7585/meaning-of-channels/ 简单来说,就是描述一个pixel的颜色用多少个独立的参数描述,这个个数 ...
- Winfrom ComboBox中的性能探索
在为Control维护元素列表的过程中,会不可避免的造成性能损耗,我们接下来要探究的就是哪种方式才是我们的最优解. 方案比较 以ComboBox为例,常见的方式一共有两种:Add.AddRange. ...
- mybatis-plus逻辑删除deleted
项目中数据库表设计原则用到了逻辑删除:数据本身没有被删除,只是将deleted字段设置为1 mybatis-plus在逻辑删除方面的设置如下: mybatis-plus: configuration: ...
- zabbix5.2+mysql+ubuntu20.4
服务端 0.初始化机器 1.mysql安装 # apt-get install mysql-server # apt update 根据提示一步一步确认,要求输入的密码是创建管理员的密码 2.安装za ...
- 【Ubuntu】设置桌面文件夹路径
Ubuntu 系统会将桌面文件夹路径默认设置为 $HOME/Desktop,包括文档.下载.图片等文件夹路径都有各自的默认路径.若想更改这些文件夹路径,可参考『此链接』. 首先到希望更改的路径下建立桌 ...
- dockerfile实践学习
一.dockerfile简介 镜像是分层存储的,每一层在前一层的基础上进行修改. 容器也是分层存储,已经向为基础层,在其他基础上加一层作为容器运行的存储层. 创建镜像的另种两种方法 手动修改容器内容, ...
- taobao.tbk.sc.newuser.order.get( 淘宝客-服务商-新用户订单明细查询 )
淘宝客订单表结构设计(mysql) CREATE TABLE `tbk_order` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `member_id` bi ...
- No.1.4
选择器进阶 [复合选择器]:后代选择器:空格 语法:选择器1 选择器2 {css} 子代选择器:> 语法:选择器1>选择器2 {css} [并集选择器]:, 语法:选择器1 , ...
- 【ZYNQ学习】ZYNQ架构介绍
在上一篇博客中,主要介绍了ZYNQ的基本信息以及如何在vivado上实现自己的设计,但是在实际应用中,掌握ZYNQ的架构是必要的,因此在这篇博客中主要记录一下ZYNQ的架构 本篇博客的主要参考是ZYN ...