C++ Primer Plus 第十三章 课后编程练习题1-4

//1题
//类定义, 由于没有使用new分配内存,所以系统自己提供的默认构造函数, 复制构造函数, 赋值运算符, 和析构函数都可以使用。
#ifndef base_hpp
#define base_hpp

class Cd
{
private:
char performers[50]; //表演者
char label[20]; //唱片公司
int selections; //选择数
double playtime; //播放时间
public:
Cd(char * s1, char * s2, int n, double x);
virtual ~Cd();; //析构函数
virtual void Report() const; //显示信息
};

class Classic : public Cd
{
private:
char leading_song[100]; //主打歌
public:
Classic(char * ls = “null”, char * s1 = “null”,
char * s2 = “null”, int n = 0, double x = 0);
~Classic();
void Report() const;
};

#endif /* base_hpp */

//类函数定义
#include
#include
#include “base.hpp”

//Cd 基类函数定义
Cd::Cd(char * s1, char * s2, int n, double x)
{
std::strncpy(performers, s1, 50);
std::strncpy(label, s2, 20);
selections = n;
playtime = x;
}

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;
}

//Classic 类函数定义 leading_song
Classic::Classic(char * ls, char * s1, char * s2, int n, double x)
: Cd(s1, s2, n, x)
{
std::strcpy(leading_song, ls);
}

Classic::~Classic(){}

void Classic::Report() const
{
this->Cd::Report();
std::cout <<"leading song: " << leading_song << std::endl;
}

//2题///////////////////////////////////////////////////////////////////////////////////////////
//类定义
#ifndef base_hpp
#define base_hpp

class Cd
{
private:
char * performers;
char * label;
int selections;
double playtime;
public:
Cd();
Cd(const char * s1, const char * s2, int n, double x);
Cd(const Cd & d);
virtual ~Cd();
virtual void Report()const;
Cd & operator=(const Cd & d);
};

class Classic : public Cd
{
private:
char * leading_song;
public:
Classic(const char * ls = “null”, const char * s1 = “null”, const char * s2 = “null”,
int n = 0, double x = 0.0);
Classic(const char * ls, const Cd & d);
Classic(const Classic & cl);
~Classic();
virtual void Report() const;
Classic & operator=(const Classic & cl);
};
#endif /* base_hpp */

//类函数定义
#include “base.hpp”
#include
#include
//默认构造函数
Cd::Cd()
{
performers = new char[4];
performers = nullptr;
label = new char[4];
label = nullptr;
selections = 0;
playtime = 0.0;
}

//构造函数
Cd::Cd(const char * s1, const char * s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
label = new char[strlen(s2) + 1];
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}

//复制构造函数
Cd::Cd(const Cd & d)
{
delete [] performers;
delete [] label;
performers = new char[strlen(d.performers) + 1];
label = new char[strlen(d.label) + 1];
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
}

//析构函数
Cd::~Cd()
{
delete [] performers;
delete [] label;
selections = 0;
playtime = 0.0;
}

//显示信息
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;
std::cout << std::endl;
}

//赋值运算符
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
delete [] performers;
delete [] label;
performers = new char[strlen(d.performers) + 1];
label = new char[strlen(d.label) + 1];
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;

return *this;

}

//Classic char * leading_song
Classic::Classic(const char * ls, const char * s1, const char * s2,
int n, double x)
:Cd(s1, s2, n, x)
{
leading_song = new char[std::strlen(ls) + 1];
std::strcpy(leading_song, ls);
}
Classic::Classic(const Classic & cl)
:Cd(cl)
{
delete [] leading_song;
leading_song = new char[std::strlen(cl.leading_song) + 1];
strcpy(leading_song, cl.leading_song);
}
void Classic::Report() const
{
Cd::Report();
std::cout <<"Leading_song: " << leading_song << std::endl;
}
Classic & Classic::operator=(const Classic & cl)
{
if (this == &cl)
return *this;
Cd::operator=(cl);
delete []leading_song;
leading_song = new char[std::strlen(cl.leading_song) + 1];
std::strcpy(leading_song, cl.leading_song);
return *this;
}

Classic::Classic(const char * ls, const Cd & d)
:Cd(d)
{
leading_song = new char[std::strlen(ls) + 1];
std::strcpy(leading_song, ls);
}
Classic::~Classic()
{
delete [] leading_song;
}

//3/////////////////////////////////////////////////////////////////////////////////////
//类定义
#ifndef base_hpp
#define base_hpp
#include
class baseABC
{
private:
char * label;
int rating;
protected:
//查看基类私有成员
const char * Label() const {return label;}
const int Rating() const { return rating;}
public:
baseABC(const char * l = “null”, int r = 0);
baseABC(const baseABC & rs);
virtual void Show()const;
virtual ~baseABC();
baseABC & operator=(const baseABC & rs);
friend std::ostream & operator<<(std::ostream & os,
const baseABC & rs);
};

class baseDMA : public baseABC
{
public:
baseDMA(const char * l = “null”, int r = 0);
~baseDMA(){}
virtual void Show()const;
baseDMA(const baseDMA & rs);
baseDMA & operator=(const baseDMA & rs);
friend std::ostream & operator<<(std::ostream & os,
const baseDMA & rs);
};

class lacksDMA :public baseABC
{
private:
enum {COL_LEN = 40};
char color[COL_LEN];
public:
lacksDMA(const char * c = “blank”, const char * l = “null”,
int r = 0);
virtual void Show()const;
lacksDMA(const char * c, const baseABC & rs);
friend std::ostream & operator<<(std::ostream & os,
const lacksDMA & rs);
};

class hasDMA :public baseABC
{
private:
char * style;
public:
hasDMA(const char * s = “none”, const char * l = “null”, int r = 0);
hasDMA(const char * s, const baseABC & rs);
hasDMA(const hasDMA & hs);
virtual void Show()const;
~hasDMA();
hasDMA & operator=(const hasDMA & hs);
friend std::ostream & operator<<(std::ostream & os, const hasDMA & rs);
};
#endif /* base_hpp */

//类函数定义
#include “base.hpp”
#include
#include
//baseABC
baseABC::baseABC(const char * l, int r)
{
label = new char[std::strlen(l) + 1];
std::strcpy(label, l);
rating = r;
}
baseABC::baseABC(const baseABC & rs)
{
delete [] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
}

baseABC & baseABC::operator=(const baseABC & rs)
{
if (this == &rs)
return *this;
delete [] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
return *this;
}

void baseABC::Show()const
{
std::cout <<"Label: " << label << std::endl;
std::cout <<"Rating: " << rating << std::endl;
}

std::ostream & operator<<(std::ostream & os,
const baseABC & rs)
{
os <<"Label: " << rs.label << std::endl;
os <<"Rating: " << rs.rating << std::endl;
return os;
}

baseABC::~baseABC()
{
delete [] label;
rating = 0;
}

//baseDMA
baseDMA::baseDMA(const char * l, int r)
:baseABC(l, r)
{
}

baseDMA::baseDMA(const baseDMA & rs)
:baseABC(rs)
{
}

void baseDMA::Show()const
{
baseABC::Show();
}

baseDMA & baseDMA::operator=(const baseDMA & rs)
{
if (this == &rs)
return *this;
baseABC::operator=(rs);
return *this;
}
//显示调用基类友元函数
std::ostream & operator<<(std::ostream & os,
const baseDMA & rs)
{
os << (const baseABC &) rs;
return os;
}

//lacksDMA
lacksDMA::lacksDMA(const char * c, const char * l, int r)
:baseABC(l, r)
{
std::strncpy(color, c, COL_LEN);
}

lacksDMA::lacksDMA(const char * c, const baseABC & rs)
:baseABC(rs)
{
std::strncpy(color, c, COL_LEN);
}

void lacksDMA::Show()const
{
baseABC::Show();
std::cout <<"Color: " << color << std::endl;
}

std::ostream & operator<<(std::ostream & os,
const lacksDMA & rs)
{
os << (const baseABC &) rs;
os <<"Color: " << rs.color << std::endl;

return os;

}

//hasDMA char * style
hasDMA::hasDMA(const char * s, const char * l, int r)
:baseABC(l, r)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}

hasDMA::hasDMA(const char * s, const baseABC & rs)
:baseABC(rs)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs)
:baseABC(hs)
{
delete [] style;
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
}

void hasDMA::Show()const
{
baseABC::Show();
std::cout <<"Stule: " << style << std::endl;
}

hasDMA::~hasDMA()
{
delete [] style;
}

hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if (this == &hs)
return *this;
baseABC::operator = (hs);
delete [] style;
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
return *this;
}

std::ostream & operator<<(std::ostream & os, const hasDMA & rs)
{
os << (const baseABC &) rs;
os <<"Style: " << rs.style << std::endl;
return os;
}

//main()
#include “base.hpp”
#include
#include
//baseABC
baseABC::baseABC(const char * l, int r)
{
label = new char[std::strlen(l) + 1];
std::strcpy(label, l);
rating = r;
}
baseABC::baseABC(const baseABC & rs)
{
delete [] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
}

baseABC & baseABC::operator=(const baseABC & rs)
{
if (this == &rs)
return *this;
delete [] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
return *this;
}

void baseABC::Show()const
{
std::cout <<"Label: " << label << std::endl;
std::cout <<"Rating: " << rating << std::endl;
}

std::ostream & operator<<(std::ostream & os,
const baseABC & rs)
{
os <<"Label: " << rs.label << std::endl;
os <<"Rating: " << rs.rating << std::endl;
return os;
}

baseABC::~baseABC()
{
delete [] label;
rating = 0;
}

//baseDMA
baseDMA::baseDMA(const char * l, int r)
:baseABC(l, r)
{
}

baseDMA::baseDMA(const baseDMA & rs)
:baseABC(rs)
{
}

void baseDMA::Show()const
{
baseABC::Show();
}

baseDMA & baseDMA::operator=(const baseDMA & rs)
{
if (this == &rs)
return *this;
baseABC::operator=(rs);
return *this;
}
//显示调用基类友元函数
std::ostream & operator<<(std::ostream & os,
const baseDMA & rs)
{
os << (const baseABC &) rs;
return os;
}

//lacksDMA
lacksDMA::lacksDMA(const char * c, const char * l, int r)
:baseABC(l, r)
{
std::strncpy(color, c, COL_LEN);
}

lacksDMA::lacksDMA(const char * c, const baseABC & rs)
:baseABC(rs)
{
std::strncpy(color, c, COL_LEN);
}

void lacksDMA::Show()const
{
baseABC::Show();
std::cout <<"Color: " << color << std::endl;
}

std::ostream & operator<<(std::ostream & os,
const lacksDMA & rs)
{
os << (const baseABC &) rs;
os <<"Color: " << rs.color << std::endl;

return os;

}

//hasDMA char * style
hasDMA::hasDMA(const char * s, const char * l, int r)
:baseABC(l, r)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}

hasDMA::hasDMA(const char * s, const baseABC & rs)
:baseABC(rs)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs)
:baseABC(hs)
{
delete [] style;
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
}

void hasDMA::Show()const
{
baseABC::Show();
std::cout <<"Stule: " << style << std::endl;
}

hasDMA::~hasDMA()
{
delete [] style;
}

hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if (this == &hs)
return *this;
baseABC::operator = (hs);
delete [] style;
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
return *this;
}

std::ostream & operator<<(std::ostream & os, const hasDMA & rs)
{
os << (const baseABC &) rs;
os <<"Style: " << rs.style << std::endl;
return os;
}
//4/////////////////////////////////////////////////////////////////////////////////////////

//main()
/我不知道怎么用基类指针调用派生类<< 友元函数。所以没有测试operator<<()
//以后学到了再修改
#include
#include “port.hpp”
const int MAX = 4;
const int NUM = 40;
int main()
{
using std::cout;
using std::cin;
using std::endl;

Port * array[MAX];
char br[NUM];
char sty[20];
char nick[NUM];
int bott = 0;
char kind;
for (int i = 0; i < MAX; i++)
{
    cout <<"请输入品牌名: ";
    cin >> br;
    cout <<"请输入种类: ";
    cin >> sty;
    cout <<"请输入瓶装量: ";
    cin >> bott;
    cout <<"请选择种类1 for Port "
    <<"2 for VintagePort.\n";
    while (cin >> kind && (kind != '1' && kind !='2'))
        cout <<"请重新输入 1 或 2. \n";
    if (kind == '1')
        array[i] = new Port(br, sty, bott);
    else
    {
        int years;
        cout <<"请输入酒的nickname; ";
        cin >> nick;
        cout <<"请输入酒的年份: ";
        cin >> years;
        array[i] = new VintagePort(br,sty,bott,nick,years);
    }
    while (cin.get() != '\n')
        continue;
}
cout << endl;
for (int i = 0; i < MAX; i++)
{
    array[i]->Show();
    cout << endl;
}

//我不知道怎么用基类指针调用派生类<< 友元函数。所以没有测试operator<<()

for (int i = 0; i < MAX; i++)
{
    delete array[i];
}
cout <<"Done.\n";
return 0;

}

//类函数定义
#include “port.hpp”
#include
#include
//Port 类
Port::Port(const char * br, const char * st, int b)
{
brand = new char[strlen(br) + 1];
std::strcpy(brand, br);
std::strncpy(style, st, 20);
bottles = b;
}

//赋值构造函数
Port::Port(const Port & p)
{
delete [] brand;
brand = new char[strlen(p.brand) + 1];
std::strncpy(style, p.style, 20);
bottles = p.bottles;
}

//赋值运算符
Port & Port::operator=(const Port & p)
{
if (this == &p)
return *this;
delete [] brand;
brand = new char[strlen(p.brand) + 1];
std::strncpy(style, p.style, 20);
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;
}

ostream & operator<<(ostream & os, const Port & p)
{
os << p.brand <<", “;
os << p.style <<”, ";
os << p.bottles;

return os;

}

//VintagePort
VintagePort::VintagePort()
:Port()
{
nickname = new char [5];
std::strncpy(nickname, “none”, 5);
year = 0;
}

VintagePort::VintagePort(const char * br, const char * st, int b, const char * nn, int y)
:Port(br, st, b)
{
nickname = new char[std::strlen(nn) + 1];
std::strcpy(nickname, nn);
year = y;
}

VintagePort::VintagePort(const VintagePort & vp)
:Port(vp)
{
delete [] nickname;
nickname = new char[std::strlen(vp.nickname) + 1];
std::strcpy(nickname, vp.nickname);
year = vp.year;
}

VintagePort & VintagePort::operator=(const VintagePort & vp)
{
if (this == &vp)
return *this;
Port::operator=(vp);
delete [] nickname;
nickname = new char[std::strlen(vp.nickname) + 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 << std::endl;
}

ostream & operator<<(ostream & os, VintagePort & vp)
{
os << (Port &) vp;
os <<", " << vp.nickname <<", " << vp.year;

return os;

}

//类定义
#ifndef port_hpp
#define port_hpp

#include
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); //bottles +
Port & operator-=(int b); //bottles -

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, const char * st, 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, VintagePort & vp);
};
#endif /* port_hpp */

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/104596779