类继承(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,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员函数,非友元的普通函数.编程序, ...
随机推荐
- GNN学习(一):基础知识
1 # !usr/bin/env python 2 # -*- coding:utf-8 _*- 3 # @Time :2022/8/20 10:46 4 # @Author: VVZ 5 # @Fi ...
- 2018GPLT
2018GPLT 7-1 天梯赛座位分配 一共有n所学校参加比赛,每所学校有\(a_i\)只队伍,每只队伍共10人,要保证每个学校的所有队员不能相邻就坐,令每一所学校的队伍排成一排纵列,然后从第一所学 ...
- [转]如何将本地项目上传至Gitee仓库(详细教程)
码云(Gitee)简单介绍 Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放 ...
- BP神经网络及手工搭建神经网络
import pandas as pd import numpy as np import matplotlib.pyplot as plt def sigmoid(x): # 定义网络激活函数 re ...
- SQL Server性能优化工具Profiler
SQL Server Profiler是什么 SQL Server Profiler是一个界面,用于创建和管理跟踪并分析和重播跟踪结果. 这些事件保存在一个跟踪文件中,稍后试图诊断问题时,可以对该文件 ...
- ORM框架的延迟加载(懒加载)
Hibernate的延迟加载分类的延迟加载和集合的延迟加载,类的延迟加载又分调用session的load()方法的延迟加载和加载实体单向关联的另一个实体的延迟加载 1.修改配置文件 spring.jp ...
- centons7.2 双网口聚合功能配置和验证
1.启动网络管理器运行systemctl restart NetworkManager2.创建主备绑定连接 nmcli connection add con-name bond0 type bond ...
- nmap扫描结果保存 xml to html for windows
首先 Nmap扫描443端口并保存为xml报告输出 nmap -T5 -Pn -p 443 -iL C:\Users\loki\Desktop\443_Scan.txt -oX C:\Users\lo ...
- Kafka Reblance & max.poll.interval.ms 重复消费问题
1. 什么是kafka Reblance 消费组是MQ中一个非常重要的概念,一个消费组监听一个Topic时,Kafka服务端会给消费组中的每一个实例,进行队列分配,每一个实例指定消费一个或多个队列(分 ...
- download links
1 anaconda https://mirrors.bfsu.edu.cn/anaconda/archive/