#ifndef TABTENN0_H_
#define TABTENN0_H_
#include
//基类
class Cd
{
private:
char performers[50];
char label[20];
int selections; //number of selections
double playtime; //palying time in minutes
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const; //reports all CD data
Cd & operator=(const Cd & d);
};
//派生类
class Classic :public Cd
{
private:
char name[20];
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d, char * c);
Classic(const Classic & d);
Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
#include"tabtenn0.h"
#include
using std::cout;
using std::endl;
//基类实现方法
Cd::Cd(char *s1, char *s2, int n, double x)
{
strcpy_s(performers, s1);
strcpy_s(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
strcpy_s(performers, d.performers);
strcpy_s(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
}
void Cd::Report() const
{
cout << "performers:" << performers << endl;
cout << "label:" << label << endl;
cout << "number of selections:" << selections << endl;
cout << "playing time in minutes:" << playtime << endl;
cout << endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
strcpy_s(performers, d.performers);
strcpy_s(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
//派生类实现方法
Classic::Classic(char * s1, char * s2, char * s3, int n, double x):Cd(s1,s2,n,x) //初始化成员列表
{
strcpy_s(name, s3);
}
Classic::Classic(const Classic & d, char * c):Cd(d)
{
strcpy_s(name, c);
}
Classic::Classic(const Classic & d)
{
}
Classic::Classic()
{
}
void Classic::Report() const
{
cout << "name:" << name << endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & d)
{
if (this == &d)
return *this;
Cd::operator=(d);
strcpy_s(name, d.name);
return *this;
}
#include
#include"tabtenn0.h"
using namespace std;
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(); //use Cd method
c2.Report(); //use Classic method
cout << "Using type cd * pointer to objects:\n";
pcd->Report(); //use Cd method for cd object
pcd = &c2;
pcd->Report(); //use Classic method for classic object
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment:";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
2.动态内存分配,使用new分配内存资源,析构函数使用delete删除指针
#ifndef TABTENN0_H_
#define TABTENN0_H_
#include
//基类
class Cd
{
private:
char * performers;
char * label;
int selections; //number of selections
double playtime; //palying time in minutes
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const; //reports all CD data
Cd & operator=(const Cd & d);
};
//派生类
class Classic :public Cd
{
private:
char * name;
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d, char * c);
Classic(const Classic & d);
Classic();
~Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
#include"tabtenn0.h"
#include
using std::cout;
using std::endl;
//基类实现方法
Cd::Cd(char *s1, char *s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy_s(performers,strlen(s1)+1, s1);
label = new char[strlen(s2) + 1];
strcpy_s(label,strlen(s2)+1, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
performers = new char[strlen(d.performers) + 1];
strcpy_s(performers, strlen(d.performers) + 1, d.performers);
label = new char[strlen(d.label) + 1];
strcpy_s(label, strlen(d.label) + 1, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers = new char[1];
performers[0] = '\0';
label = new char[1];
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
delete[] label;
delete[] performers;
}
void Cd::Report() const
{
cout << "performers:" << performers << endl;
cout << "label:" << label << endl;
cout << "number of selections:" << selections << endl;
cout << "playing time in minutes:" << playtime << endl;
cout << endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
delete[] label;
delete[] performers;
performers = new char[strlen(d.performers) + 1];
strcpy_s(performers, strlen(d.performers) + 1,d.performers);
label = new char[strlen(d.label) + 1];
strcpy_s(label, strlen(d.label) + 1, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
//派生类实现方法
Classic::Classic(char * s1, char * s2, char * s3, int n, double x):Cd(s1,s2,n,x) //初始化成员列表
{
name = new char[strlen(s3) + 1];
strcpy_s(name, strlen(s3) + 1, s3);
}
Classic::Classic(const Classic & d, char * c):Cd(d)
{
name = new char[strlen(c) + 1];
strcpy_s(name, strlen(c) + 1, c);
}
Classic::Classic(const Classic & d)
{
}
Classic::Classic()
{
name = new char[1];
name[0] = '\0';
}
Classic::~Classic()
{
delete[] name;
}
void Classic::Report() const
{
cout << "name:" << name << endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & d)
{
if (this == &d)
return *this;
Cd::operator=(d);
delete[] name;
name = new char[strlen(d.name) + 1];
strcpy_s(name, strlen(d.name) + 1, d.name);
return *this;
}
#include
#include"tabtenn0.h"
using namespace std;
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(); //use Cd method
c2.Report(); //use Classic method
cout << "Using type cd * pointer to objects:\n";
pcd->Report(); //use Cd method for cd object
pcd = &c2;
pcd->Report(); //use Classic method for classic object
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment:";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
// dma.h -- inheritance and dynamic memory allocation
#ifndef DMA_H_
#define DMA_H_
#include
// 抽象基类
class ABC
{
private:
char * label; //char *需要深度复制,string类不需要
int rating;
public:
ABC(const char * l = "null", int r = 0);
ABC(const ABC & rs);
ABC & operator=(const ABC & rs);
virtual ~ABC();
virtual void View() = 0;
friend std::ostream & operator<<(std::ostream & os, const ABC & rs);
char * getLabel() { return label; }
int getRating() { return rating; }
};
class baseDMA :public ABC
{
public:
baseDMA(const char * l = "null", int r = 0);
baseDMA(const baseDMA & rs);
virtual void View();
};
class lacksDMA :public ABC
{
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 & ls);
virtual void View();
};
class hasDMA :public ABC
{
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 --dma class methods
#include "dma.h"
#include
using std::cout;
using std::endl;
//抽象基类
ABC::ABC(const char * l, int r)
{
label = new char[std::strlen(l) + 1];
strcpy_s(label, strlen(l) + 1, l);
rating = r;
}
ABC::ABC(const ABC & rs)
{
label = new char[strlen(rs.label) + 1];
strcpy_s(label, strlen(rs.label) + 1, rs.label);
rating = rs.rating;
}
ABC::~ABC()
{
delete [] label;
}
ABC & ABC::operator=(const ABC & rs)
{
if (this == &rs)
return *this;
delete [] label;
label = new char[strlen(rs.label) + 1];
strcpy_s(label, strlen(rs.label) + 1, rs.label);
rating = rs.rating;
return *this;
}
void ABC::View()
{
cout << "Label:" << label << endl;
cout << "Rating:" << rating << endl;
}
std::ostream & operator<<(std::ostream & os, const ABC & rs)
{
os << "Label: " << rs.label << endl;
os << "Rating: " << rs.rating << endl;
return os;
}
//baseDMA类
baseDMA::baseDMA(const char * l, int r ):ABC(l,r)
{
}
baseDMA::baseDMA(const baseDMA & rs):ABC(rs)
{
}
void baseDMA::View()
{
ABC::View();
}
// lacksDMA类
lacksDMA::lacksDMA(const char * c, const char * l, int r):ABC(l, r)
{
strcpy_s(color, c);
color[COL_LEN - 1] = '\0';
}
lacksDMA::lacksDMA(const char * c, const baseDMA & rs): ABC(rs)
{
strcpy_s(color, c);
color[COL_LEN - 1] = '\0';
}
std::ostream & operator<<(std::ostream & os, const lacksDMA & ls)
{
os << (const ABC &) ls;
os << "Color: " << ls.color << endl;
return os;
}
void lacksDMA::View()
{
ABC::View();
cout << "Color:" << color << endl;
}
// hasDMA类
hasDMA::hasDMA(const char * s, const char * l, int r): ABC(l, r)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const char * s, const baseDMA & rs): ABC(rs)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA & hs): ABC(hs) // invoke base class copy constructor
{
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
hasDMA::~hasDMA()
{
delete [] style;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if (this == &hs)
return *this;
ABC::operator=(hs); // copy base portion
delete [] style; // prepare for new style
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
return *this;
}
std::ostream & operator<<(std::ostream & os, const hasDMA & hs)
{
os << (const baseDMA &) hs;
os << "Style: " << hs.style << std::endl;
return os;
}
void hasDMA::View()
{
ABC::View();
cout << "Style:" << style << endl;
}
#include
#include
#include "dma.h"
const int BASE = 3;
int main()
{
using std::cin;
using std::cout;
using std::endl;
ABC * p_base[BASE];
char temp[40];
char tcolor[40];
char tstyle[40];
int tempnum;
int kind;
for (int i = 0; i < BASE;i++)
{
cout << "Enter base's label: "<> tempnum;
cout << "Enter 1 for base,2 for lacksbase or 3 for hasbase:" << endl;
while (cin >> kind && (kind !=1 && kind != 2&&kind!=3))
cout <<"Enter either 1 , 2 or 3: "<View();
cout << endl;
}
for (int i = 0; i < BASE; i++)
{
delete p_base[i]; // free memory
}
cout << "Done.\n";
/* code to keep window open
if (!cin)
cin.clear();
while (cin.get() != '\n')
continue;
*/
return 0;
}
// dma.h -- inheritance and dynamic memory allocation
#ifndef DMA_H_
#define DMA_H_
#include
using namespace std;
class Port
{
private:
char * brand;
char style[20]; //tawny,ruby,vintage
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; //"The Noble" or "Old Velvet"
int year; //vintage 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, const VintagePort & vp);
};
#endif
// dma.cpp --dma class methods
#include "dma.h"
#include
using std::cout;
using std::endl;
//Port类
Port::Port(const char * br , const char * st , int b)
{
brand = new char[strlen(br) + 1];
strcpy_s(brand, strlen(br) + 1, br);
strcpy_s(style, st);
bottles = b;
}
Port::Port(const Port & p) //复制构造函数
{
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, p.style);
bottles = p.bottles;
}
Port & Port::operator=(const Port & p)
{
if (this == &p)
return *this;
delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, p.style);
bottles = p.bottles;
return *this;
}
Port & Port::operator+=(int b) //增加酒箱
{
bottles += b;
return *this; //返回调用对象
}
Port & Port::operator-=(int b) //减少酒箱
{
if (bottles >= b)
bottles -= b;
if (bottles < b)
cout << "You don't have enough bottles.";
return *this; //返回调用对象
}
void Port::Show() const
{
cout << "Brand: " << brand << endl;
cout << "Kind: " << style << endl;
cout << "Bottles: " << bottles << endl;
}
ostream & operator<<(ostream & os, const Port & p)
{
os << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
//VintagePort类
VintagePort::VintagePort():Port()
{
nickname = new char[1];
nickname[0] = '\0';
year = 0;
}
VintagePort::VintagePort(const char *br, const char * st, int b, const char * nn, int y):Port(br,st,b)
{
nickname = new char[strlen(nn)+1];
strcpy_s(nickname, strlen(nn) + 1, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort & vp):Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
year = vp.year;
}
VintagePort & VintagePort::operator=(const VintagePort & vp)
{
if (this == &vp)
return *this;
Port::operator=(vp);
delete[] nickname;
nickname = new char[strlen(vp.nickname) + 1];
strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::Show() const
{
Port::Show();
cout << "Nickname:" << nickname << endl;
cout << "Year:" << year << endl;
}
ostream & operator<<(ostream & os, const VintagePort & vp)
{
os << (const Port &)vp;
os << ", " << vp.nickname << ", " << vp.year;
return os;
}
//测试程序
#include
#include
#include "dma.h"
using std::endl;
using std::cout;
int main()
{
Port port1("gallo", "tawny", 20);
cout << port1 << endl;
VintagePort vp("gallo", "vintage", 24, "Old Velvet", 16);
VintagePort vp2(vp);
cout << vp2 << endl;
VintagePort vp3;
vp3 = vp;
cout << vp3 << endl<Show();
cout << endl;
p_port = &vp;
p_port->Show();
cout << endl;
return 0;
}