1.CD
#ifndef CLASSIC_H_
#define CLASSIC_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();
virtual ~CD();
virtual void Report() const;
};
class Classic :public CD
{
private:
char main[50];
public:
Classic(const char * m = "null", const char * s1 = "nullbody", const char * s2 = "null", int n = 0, double x = 0.0);
Classic(const char * m, const CD & d);
virtual void Report() const;
};
#endif
#include "stdafx.h"
#include "classic.h"
#include <iostream>
CD::CD(const char * s1, const char * s2, int n, double x)
{
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
CD::CD()
{
strcpy(performers, "nullbody");
strcpy(label, "null");
selections = 0;
playtime = 0.0;
}
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::Classic(const char * m, const char * s1, const char * s2, int n, double x) :CD(s1, s2, n, x)
{
strcpy(main, m);
}
Classic::Classic(const char * m, const CD & d) : CD(d)
{
strcpy(main, m);
}
void Classic::Report() const
{
CD::Report();
std::cout << "main: " << main << std::endl;
}
#include "stdafx.h"
#include "classic.h"
#include <iostream>
void Bravo(const CD & disk);
int main()
{
using std::cout;
CD c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantansia 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();
std::cin.get();
return 0;
}
void Bravo(const CD & disk)
{
disk.Report();
}
2.动态分配内存完成1
#ifndef CLASSIC_H_
#define CLASSIC_H_
class CD
{
private:
char * performers;
char * label;
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 * main;
public:
Classic(const char * m = "null", const char * s1 = "nullbody", const char * s2 = "null", int n = 0, double x = 0.0);
Classic(const char * m, const CD & d);
virtual void Report() const;
Classic(const Classic & c);
Classic & operator=(const Classic & c);
~Classic();
};
#endif
#include "stdafx.h"
#include "classic.h"
#include <iostream>
CD::CD(const char * s1, const char * s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy(performers, s1);
label = new char[strlen(s2) + 1];
strcpy(label, s2);
selections = n;
playtime = x;
}
CD::CD()
{
performers = nullptr;
label = nullptr;
selections = 0;
playtime = 0.0;
}
CD::~CD()
{
delete[] performers;
delete[] label;
}
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(const CD & d)
{
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
CD & CD::operator=(const CD & d)
{
if (this == &d)
return *this;
else
{
delete[] performers;
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
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 * m, const char * s1, const char * s2, int n, double x) :CD(s1, s2, n, x)
{
main = new char[strlen(m) + 1];
strcpy(main, m);
}
Classic::Classic(const char * m, const CD & d) : CD(d)
{
main = new char[strlen(m) + 1];
strcpy(main, m);
}
void Classic::Report() const
{
CD::Report();
std::cout << "main: " << main << std::endl;
}
Classic::Classic(const Classic & c) :CD(c)
{
main = new char[strlen(c.main) + 1];
strcpy(main, c.main);
}
Classic & Classic::operator=(const Classic & c)
{
if (this == &c)
return *this;
else
{
CD::operator=(c);
delete[] main;
main = new char[strlen(c.main) + 1];
strcpy(main, c.main);
return *this;
}
}
Classic::~Classic()
{
delete[] main;
}
#include "stdafx.h"
#include "classic.h"
#include <iostream>
void Bravo(const CD & disk);
int main()
{
using std::cout;
CD c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantansia 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();
std::cin.get();
return 0;
}
void Bravo(const CD & disk)
{
disk.Report();
}
3.三个DMA由ABC派生
#ifndef DMA_H_
#define DMA_H_
#include <iostream>
class DMA
{
private:
char * label;
int rating;
public:
DMA(const char * l = "null", int r = 0);
DMA(const DMA & rs);
virtual ~DMA();
DMA & operator=(const DMA & rs);
virtual void View() const = 0;
};
class baseDMA :public DMA
{
public:
baseDMA(const char * l = "null", int r = 0);
virtual void View() const;
};
class lacksDMA :public DMA
{
private:
enum{ COL_LEN = 40 };
char color[COL_LEN];
public:
lacksDMA(const char * c = "black", const char * l = "null", int r = 0);
lacksDMA(const char * c, const DMA & rs);
virtual void View() const;
};
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 DMA & rs);
hasDMA(const hasDMA & hs);
~hasDMA();
virtual hasDMA & operator=(const hasDMA & hs);
virtual void View() const;
};
#endif
#include "stdafx.h"
#include "dma.h"
DMA::DMA(const char * l, int r)
{
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}
DMA::DMA(const DMA & rs)
{
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
}
DMA::~DMA()
{
delete[] label;
}
DMA & DMA::operator=(const DMA & rs)
{
if (this == &rs)
return *this;
else
{
delete[] label;
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
}
void DMA::View() const
{
std::cout << "label: " << label << std::endl;
std::cout << "rating: " << rating << std::endl;
}
baseDMA::baseDMA(const char * l, int r) :DMA(l, r)
{
}
void baseDMA::View() const
{
DMA::View();
}
lacksDMA::lacksDMA(const char * c, const char * l, int r) :DMA(l, r)
{
strcpy(color, c);
}
lacksDMA::lacksDMA(const char * c, const DMA & rs) : DMA(rs)
{
strcpy(color, c);
}
void lacksDMA::View() const
{
DMA::View();
std::cout << "color: " << color << std::endl;
}
hasDMA::hasDMA(const char * s, const char * l, int r) :DMA(l, r)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const char * s, const DMA & rs) : DMA(rs)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs) : DMA(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{
delete[] style;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if (this == &hs)
return *this;
else
{
DMA::operator=(hs);
delete[] style;
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
return *this;
}
}
void hasDMA::View() const
{
DMA::View();
std::cout << "style: " << style << std::endl;
}
#include "stdafx.h"
#include "dma.h"
#include <ctime>
#include <string>
const int CLIENT = 4;
int main()
{
using std::cout;
using std::cin;
using std::endl;
DMA * p_clients[CLIENT];
char lb[50];
int rt;
char kind;
for (int i = 0; i < CLIENT; i++)
{
cout << "Enter label: ";
cin.getline(lb, 50);
cout << "Enter rating: ";
cin >> rt;
while (cin.get() != '\n')
continue;
cout << "Enter 1 for baseDMA, 2 for lacksDMA or 3 for hasDMA: ";
while (cin >> kind && kind != '1' && kind != '2' && kind != '3')
{
cout << "Enter 1, 2 or 3: ";
}
while (cin.get() != '\n')
continue;
if (kind == '1')
p_clients[i] = new baseDMA(lb, rt);
else if (kind == '2')
{
char cl[50];
cout << "Enter color: ";
cin.getline(cl, 50);
p_clients[i] = new lacksDMA(cl, lb, rt);
}
else
{
char st[50];
cout << "Enter style: ";
cin.getline(st, 50);
p_clients[i] = new hasDMA(st, lb, rt);
}
}
cout << endl;
for (int i = 0; i < CLIENT; i++)
{
p_clients[i]->View();
cout << endl;
}
for (int i = 0; i < CLIENT; i++)
delete p_clients[i];
cout << "Done!\n";
clock_t delay = 100 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
}
4.葡萄酒类
#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(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
#include "stdafx.h"
#include "port.h"
Port::Port(const char * br, const char * st, int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand, br);
strcpy(style, st);
bottles = b;
}
Port::Port(const Port & p)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strcpy(style, p.style);
bottles = p.bottles;
}
Port & Port::operator=(const Port & p)
{
if (this == &p)
return *this;
else
{
delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strcpy(style, p.style);
bottles = p.bottles;
return *this;
}
}
Port & Port::operator+=(int b)
{
this->bottles += b;
return *this;
}
Port & Port::operator-=(int b)
{
this->bottles -= b;
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(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;
else
{
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: " << 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 "stdafx.h"
#include "port.h"
int main()
{
Port p("WLJ", "hong", 20);
VintagePort vp("MZ", "hei", 30, "maoz", 1987);
cout << "p:\n";
p.Show();
cout << endl;
cout << p << endl;
cout << "vp:\n";
vp.Show();
cout << endl << vp << endl;
cout << "p += 20: ";
p += 20;
p.Show();
cout << "\nvp -= 20: ";
vp -= 20;
vp.Show();
cout << endl << "copy constructor: \n";
Port p1(p);
p1.Show();
cout << "fu zhi yun suan fu:\n";
VintagePort vp2 = vp;
vp2.Show();
cin.get();
return 0;
}