1、
/*************CD.h***********************/
#pragma once
#ifndef CD_H_
#define CD_H_
class Cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(const char *s1,const char *s2, int n, double x);
Cd(const Cd &d);
Cd();
virtual ~Cd();
virtual void Report()const;
Cd&operator=(const Cd &d);
};
class Classic :public Cd
{
private:
char name[50];
public:
Classic(const char *na, const char *s1, const char *s2,int n, double x);
Classic(const char *na, const Cd &c);
Classic(const Classic &c);
Classic();
//~Classic();
Classic &operator=(const Classic &c);
virtual void Report()const;
};
#endif // !CD_H_
/***************cd.cpp***********************/
#include<iostream>
#include"CD.h"
using namespace std;
Cd::Cd(const char *s1, const char *s2, int n, double x)
{
strcpy(performers ,s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd &c)
{
strcpy(performers, c.performers);
strcpy(label, c.label);
selections = c.selections;
playtime = c.playtime;
}
Cd::Cd()
{
performers[0] = NULL;
label[0] = 0;
selections = 0;
playtime = 0.0;
}
Cd::~Cd()
{
}
void Cd::Report()const
{
cout << performers << endl;
cout << label << endl;
cout << selections<<endl;
cout << playtime<<endl;
}
Cd & Cd::operator=(const Cd &d)
{
if (this == &d)
return *this;
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(const char *na, const char *s1, const char *s2, int n, double x):Cd(s1,s2,n,x)
{
strcpy(name, na);
}
Classic::Classic(const char *c, const Cd &rs) : Cd(rs)
{
strcpy(name, c);
}
Classic::Classic()
{
Cd();
name[0] = '\0';
}
Classic::Classic(const Classic &c)
{
strcpy(name, c.name);
}
/*
Classic::~Classic()
{}
*/
Classic &Classic::operator=(const Classic &c)
{
if (this == &c)
return *this;
Cd::operator=(c);
strcpy(name, c.name);
return *this;
}
void Classic::Report()const
{
Cd::Report();
cout << "name: " << name << endl;
}
/***************cder.h********************/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include"CD.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();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "testing assignment: ";
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、使用动态内存分配的数组来记录字符串。
/***************cder.h********************/
#include<iostream>
#include"CD.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();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "testing assignment: ";
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();
}
/***************cd.cpp***********************/
#include<iostream>
#include"CD.h"
using namespace std;
Cd::Cd(const char* s1, const char* s2, int n, double x)
{
perf = new char[strlen(s1) + 1];
label = new char[strlen(s2) + 1];
strcpy(perf, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd& c)
{
perf = new char[strlen(c.perf) + 1];
strcpy(perf, c.perf);
label = new char[strlen(c.label) + 1];
strcpy(label, c.label);
selections = c.selections;
playtime = c.playtime;
}
Cd::Cd()
{
perf = new char[1];
perf[0] = NULL;
label = new char[1];
label[0] = 0;
selections = 0;
playtime = 0.0;
}
Cd::~Cd()
{
delete[]perf;
}
void Cd::Report()const
{
cout << perf << endl;
cout << label << endl;
cout << selections << endl;
cout << playtime << endl;
}
Cd& Cd::operator=(const Cd& d)
{
if (this == &d)
return *this;
delete[]perf;
perf = new char[strlen(d.perf) + 1];
strcpy(perf, d.perf);
delete[]label;
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(const char* na, const char* s1, const char* s2, int n, double x) :Cd(s1, s2, n, x)
{
delete[]name;
name = new char[strlen(na) + 1];
strcpy(name, na);
}
Classic::Classic(const char* c, const Cd& rs) : Cd(rs)
{
name = new char[strlen(c) + 1];
strcpy(name, c);
}
Classic::Classic()
{
Cd();
name = new char[1];
name[0] = '\0';
}
Classic::Classic(const Classic& c)
{
name = new char[strlen(c.name) + 1];
strcpy(name, c.name);
}
Classic::~Classic()
{
delete[]name;
}
Classic& Classic::operator=(const Classic& c)
{
if (this == &c)
return *this;
Cd::operator=(c);
delete[]name;
name = new char[strlen(c.name) + 1];
strcpy(name, c.name);
return *this;
}
void Classic::Report()const
{
Cd::Report();
cout << "name: " << name << endl;
}
/***************cder.h********************/
#include<iostream>
#include"CD.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();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "testing assignment: ";
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();
}
3、
/***************dma.h*********************/
#pragma once
#ifndef DMA_H_
#define DMA_H_
#include<iostream>
//Base Class Dma
class Dma
{
private:
char* label;
int rating;
public:
Dma(const char* l = "null", int r = 0);
Dma(const Dma& rs);
virtual ~Dma();
virtual void viewdma()const = 0;
const std::string& Label() { return label; }
int rt()const { return rating;}
};
class baseDma:public Dma
{
public:
baseDma(const char* l = "null", int r = 0);
baseDma(const baseDma& rs);
virtual ~baseDma();
virtual void viewdma()const;
};
//derived class without DMA
// no destructor needed
//uses implicit copy constructor
//uses implicit assignment operator
class lacksDma :public Dma
{
private:
enum { COL_LEN = 40 };
char color[COL_LEN];
protected:
char col()const { return *color; }
public:
lacksDma(const char* c = "blank", const char* l = "null",
int r = 0);
lacksDma(const char* c, const baseDma& rs);
virtual void viewdma()const;
};
//derived class with Dma
class hasDma :public Dma
{
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();
virtual void viewdma()const;
};
#endif // !DMA_H_
/***************dma.cpp*********************/
#include<cstring>
#include"dma.h"
using std::cout;
using std::endl;
//baseDma methods
Dma::Dma(const char* l, int r)
{
label = new char[std::strlen(l) + 1];
std::strcpy(label, l);
rating = r;
}
Dma::Dma(const Dma& rs)
{
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
}
void Dma::viewdma()const
{
cout << "Label: " << label << std::endl;
cout << "Rating: " << rating << std::endl;
}
Dma::~Dma()
{
delete[]label;
}
baseDma::baseDma(const char* l, int r):Dma(l,r)
{
}
baseDma::baseDma(const baseDma& rs):Dma(rs)
{
}
void baseDma::viewdma()const
{
Dma::viewdma();
}
baseDma::~baseDma()
{
}
//lacksDma methods
lacksDma::lacksDma(const char* c, const char* l, int r)
:Dma(l, r)
{
std::strncpy(color, c, 39);
color[39] = '\0';
}
lacksDma::lacksDma(const char* c, const baseDma& rs)
:Dma(rs)
{
std::strncpy(color, c, COL_LEN-1);
color[COL_LEN - 1] = '\0';
}
void lacksDma::viewdma()const
{
Dma::viewdma();
cout<< "Color: " << color << std::endl;
}
//hasDma methods
hasDma::hasDma(const char* s, const char* l, int r)
:Dma(l, r)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDma::hasDma(const char* s, const baseDma& rs)
:Dma(rs)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDma::hasDma(const hasDma& hs)
: Dma(hs)
{
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
}
hasDma::~hasDma()
{
delete[]style;
}
void hasDma::viewdma()const
{
Dma::viewdma();
cout << "Style: " << style << std::endl;
}
/*
std::ostream& operator<<(std::ostream& os, const hasDma& hs)
{
os << (const baseDma&)hs;
os << "Style: " << hs.style << std::endl;
return os;
}
*/
/***************usedma.cpp*********************/
#include<iostream>
#include"dma.h"
const int CLIENTS =3;
using std::cin;
int main()
{
using std::cout;
using std::endl;
Dma* p[CLIENTS];
char kind;
for (int i = 0; i < CLIENTS; i++)
{
cout << "Enter 1 for baseDma,enther 2 for lackDma,"
<<"enter 3 for hasDma.\n";
cin >> kind;
if (kind == '1')
p[i] = new baseDma("goof", 2);
else if (kind == '2')
{
p[i] = new lacksDma("red", "good", 2);
}
else
{
p[i] = new hasDma("great", "good", 2);
}
}
cout << "\n";
for (int i = 0; i < CLIENTS; i++)
{
p[i]->viewdma();
}
for (int i = 0; i < CLIENTS; i++)
delete p[i];
system("PAUSE");
= return 0;
}
4、b)内联函数不需要再重新定义了。
c)operator=()不能继承,因为赋值运算符不能将父类赋值给子类。operator<<()是友元函数,不能声明为虚函数。
/****************port.h******************/
#pragma once
#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& b);
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, const char* st,int b, const char* nn, int y);
VintagePort(const VintagePort& vp);
~VintagePort() { delete[]nickname; }
VintagePort& operator=(const VintagePort& vp);
virtual void Show()const;
friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
#endif // !PORT_H_\]]]]]]]]]]]]]
/****************port.cpp******************/
#include<iostream>
#include"Port.h"
using namespace std;
Port::Port(const char* br, const char* st, int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand,br);
strncpy(style,st,19);
style[19] = '0';
bottles = b;
}
Port::Port(const Port& p)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style,p.style,19 );
style[19] = '0';
bottles = p.bottles;
}
Port& Port::operator=(const Port& b)
{
if (this == &b)
return *this;
delete[]brand;
brand = new char[strlen(b.brand) + 1];
strcpy(brand, b.brand);
strncpy(style, b.style,19);
style[19] = '0';
bottles = b.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
{
cout << "brand is: " << brand << endl;
cout << "style is: " << style << endl;
cout << "bottles number is: " << bottles << endl;
}
ostream& operator<<(ostream& os, const Port& p)
{
os << "brand is: " << p.brand << endl;
os << "style is: " << p.style << endl;
os << "bottles number is: " << p.bottles << endl;
return os;
}
VintagePort::VintagePort():Port()
{
nickname=new char[1];
nickname = 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(nickname, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort& vp):Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
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[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::Show()const
{
Port::Show();
cout << "nickname is: " << nickname << endl;
cout << "years is: " << year << endl;
}
ostream& operator<<(ostream& os, const VintagePort& vp)
{
os << (const Port&)vp;
os << "nickname is: " << vp.nickname << endl;
os << "years is: " << vp.year << endl;
return os;
}
/****************porter.cpp******************/
#include<iostream>
#include"Port.h"
using namespace std;
int main()
{
Port xintian("XinTian", "tawny", 100);
cout << "xintian "<<xintian<<endl;
Port ZhanYu("ZhanYu", "ruby", 200);
VintagePort GanHong("ZhaoWei","ruby", 300, "XiaoYanzi", 20);
cout << "GanHong " << GanHong << endl;
Port temp;
temp = xintian;
cout<<"temp " << temp << endl;
temp += 5;
cout << "temp " << temp << endl;
//mp.Show();
temp -= 10;
//cout << temp << endl;
temp.Show();
cout << endl << "Use point to object&&Use virtual function:" << endl;
Port* p_port;
p_port = &xintian;
p_port->Show();
//virtural function
p_port = &GanHong;
p_port->Show();
}