//1 *************************
//cd.h
#ifndef CD_H_
#define CD_H_
#include<iostream>
class Cd
{
private:
char performers[50];
char label[30];
int selections;
double playtime;
protected:
struct Formatting
{
std::ios_base::fmtflags flag;
std::streamsize pr;
};
Formatting SetFormat() const;
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const;
Cd & operator=(const Cd & d);
friend std::ostream & operator<<(std::ostream &os,char *s) ;
};
class Classic : public Cd
{
private:
char songs1[50];
public:
Classic(char *s0 = "Null", char * s1 = "Null", char * s2 = "Null", int n = 0, double x = 0.0);
Classic(const Cd & c, char * s1 = "Null");
~Classic();
virtual void Report() const;
};
#endif // CD_H_
//cd.cpp
#include<iostream>
#include"cd.h"
using std::ios_base;
Cd::Formatting Cd::SetFormat() const
{
Formatting f;
f.flag =
std::cout.setf(ios_base::fixed, ios_base::floatfield);
f.pr = std::cout.precision(2);
return f;
}
Cd::Cd(char * s1, char * s2, int n, double x)
{
strcpy_s(performers, strlen(s1)+1, s1);
strcpy_s(label, strlen(s2)+1, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
strcpy_s(performers, strlen(d.performers)+1, d.performers);
strcpy_s(label, strlen(d.label)+1, 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
{
using std::cout;
using std::endl;
Formatting f = SetFormat();
cout << "performers: " << performers << endl;
cout << "label: " << label << endl;
cout << "selections: " << selections << endl;
cout << "playtime: " << playtime << endl;
}
Cd & Cd::operator=(const Cd & d)
{
strcpy_s(performers, strlen(d.performers)+1, d.performers);
strcpy_s(label,strlen(d.label)+1, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
std::ostream & operator<<(std::ostream& os,char s[])
{
for (int i = 0; i < strlen(s); ++i)
os << s[i];
return os;
}
Classic::Classic(char * s0, char * s1, char * s2, int n, double x) :Cd(s1,s2,n,x)
{
strcpy_s(songs1, strlen(s0) + 1, s0);
}
Classic::Classic(const Cd & c, char * s1) :Cd(c)
{
strcpy_s(songs1, strlen(s1) + 1, s1);
}
Classic::~Classic()
{
}
void Classic::Report() const
{
using std::cout;
using std::endl;
Formatting f = SetFormat();
Cd::Report();
cout << "songs1 is: " << songs1 << endl;
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include <ctime> //time() prototype
#include<stdlib.h>
#include "cd.h"
using std::cout;
using std::endl;
using std::cin;
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();
system("pause");
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
//2***************************************
//cd.h
#ifndef CD_H_
#define CD_H_
#include<iostream>
class Cd
{
private:
char *performers;
char *label;
int selections;
double playtime;
protected:
struct Formatting
{
std::ios_base::fmtflags flag;
std::streamsize pr;
};
Formatting SetFormat() const;
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const;
Cd & operator=(const Cd & d);
friend std::ostream & operator<<(std::ostream &os,char *s) ;
};
class Classic : public Cd
{
private:
char *songs1;
public:
Classic(char *s0 = "Null", char * s1 = "Null", char * s2 = "Null", int n = 0, double x = 0.0);
Classic(const Cd & c, char * s1 = "Null");
~Classic();
virtual void Report() const;
};
#endif // CD_H_
//cd.cpp
#include<iostream>
#include"cd.h"
using std::ios_base;
Cd::Formatting Cd::SetFormat() const
{
Formatting f;
f.flag =
std::cout.setf(ios_base::fixed, ios_base::floatfield);
f.pr = std::cout.precision(2);
return f;
}
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[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
delete[] performers;
delete[] label;
}
void Cd::Report() const
{
using std::cout;
using std::endl;
Formatting f = SetFormat();
cout << "performers: " << performers << endl;
cout << "label: " << label << endl;
cout << "selections: " << selections << endl;
cout << "playtime: " << playtime << endl;
}
Cd & Cd::operator=(const Cd & d)
{
delete[] performers;
delete[] label;
if (this == &d)
return *this;
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;
}
std::ostream & operator<<(std::ostream& os,char s[])
{
for (int i = 0; i < strlen(s); ++i)
os << s[i];
return os;
}
Classic::Classic(char * s0, char * s1, char * s2, int n, double x) :Cd(s1,s2,n,x)
{
songs1 = new char[strlen(s0) + 1];
strcpy_s(songs1, strlen(s0) + 1, s0);
}
Classic::Classic(const Cd & c, char * s1) :Cd(c)
{
strcpy_s(songs1, strlen(s1) + 1, s1);
}
Classic::~Classic()
{
}
void Classic::Report() const
{
using std::cout;
using std::endl;
Formatting f = SetFormat();
Cd::Report();
cout << "songs1 is: " << songs1 << endl;
}
//3**********************
//使用ABC,即抽象类结构进行类的派生和多态实现。
//DMA.h
#ifndef DMA_H_
#define DMA_H_
#include<iostream>
// base DMA
class baseDMA {
private:
char * label;
int rating;
protected:
void show() { std::cout << label; };
const int Rate() { return rating; };
public:
baseDMA(const char * l = "null", int r = 0);
baseDMA(const baseDMA &rs);
virtual ~baseDMA();
baseDMA & operator=(const baseDMA &rs);
virtual void view() = 0; //pure virtual function;
friend std::ostream &operator<<(std::ostream &os, char *c);
};
//lackDMA
class lackDMA :public baseDMA
{
private:
enum { COL_LEN=40 };
char color[COL_LEN];
public:
lackDMA(const char *l = "null", int r = 0, char *c = "blank") ;
virtual ~lackDMA();
virtual void view();
};
//hasDMA
class hasDMA :public baseDMA
{
private:
char * style;
public:
hasDMA(const char *l = "null", int r = 0, char *s = "none");
hasDMA(const char *s, const baseDMA &rs);
hasDMA(const hasDMA & hs);
virtual ~hasDMA();
virtual void view() ;
};
#endif // !DMA_H_
//DMA.cpp
#include<iostream>
#include"DMA.h"
using std::endl;
using std::cout;
using std::cin;
using std::ios_base;
baseDMA::baseDMA(const char * l, int r)
{
label = new char[strlen(l) + 1];
strcpy_s(label, strlen(l) + 1, l);
rating = r;
}
baseDMA::baseDMA(const baseDMA & rs)
{
label = new char[strlen(rs.label) + 1];
strcpy_s(label, strlen(rs.label) + 1, rs.label);
rating = rs.rating;
}
baseDMA::~baseDMA()
{
delete[] label;
}
std::ostream & operator<<(std::ostream & os, char *s)
{
for (int i = 0; i < strlen(s) + 1; i++)
cout << s[i];
return os;
}
baseDMA & baseDMA::operator=(const baseDMA & rs)
{
label = new char[strlen(rs.label) + 1];
label = rs.label;
rating = rs.rating;
return *this;
}
//lackDMA
lackDMA::lackDMA(const char * l, int r, char * c):baseDMA(l,r)
{
strcpy_s(color, strlen(c) + 1, c);
}
lackDMA::~lackDMA()
{
}
void lackDMA::view()
{
cout << "Label: ";
show();
cout << endl;
cout << "Rate: " << Rate() << endl;
cout << "Color: " << color << endl;
}
//hasDMA
hasDMA::hasDMA(const char * l, int r, char * s):baseDMA(l,r)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const char * s, const baseDMA & rs):baseDMA(rs)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA & hs):baseDMA(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
hasDMA::~hasDMA()
{
delete[] style;
}
void hasDMA::view()
{
cout << "Label: ";
show();
cout << endl;
cout << "Rate: " << Rate() << endl;
cout << "Style: " << style << endl;
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include<stdlib.h>
#include"DMA.h"
using std::cout;
using std::endl;
using std::cin;
const int MAX = 2;
int main()
{
baseDMA *p[MAX];
char *s1 = "good";
char *s2 = "bad";
char *s3 = "normal";
int r1 = 1;
int r2 = 2;
int r3 = 3;
char *c1 = "dark";
char *st1 = "simple";
p[0] = new lackDMA(s1, r1,c1);
p[1] = new hasDMA(s3, r2, st1);
for (int i = 0; i < MAX; i++)
p[i]->view(),
cout << endl;
for (int i = 0; i < MAX; i++)
delete p[i];
cout << "Done.\n";
system("pause");
return 0;
}
//4******************************************************************
//port.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);//copy constructor;
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);
friend ostream & operator<<(ostream & os, const char * c);
};
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_H_
//port.cpp
#include<iostream>
#include"port.h"
using std::endl;
using std::cout;
using std::cin;
using std::ios_base;
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, strlen(st) + 1, 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, strlen(p.style) + 1, p.style);
bottles = p.bottles;
}
Port & Port::operator=(const Port & p)
{
delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
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
{
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 << "\n";
return os;
}
ostream & operator<<(ostream & os, const char * c)
{
for (int i = 0; i < strlen(c); i++)
os << c[i];
return os;;
}
ostream & operator<<(ostream & os, const VintagePort & vp)
{
os << (const Port &)vp;
os << vp.nickname << " , " << vp.year << endl;
return os;
}
VintagePort::VintagePort()
{
Port();
nickname[0] = '\0';
year = 0;
}
VintagePort::VintagePort(const char * br, int b, const char * nn, int y):Port(br,"none",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
{
cout << "Nickname: " << nickname <<endl;
cout << "Year: " << year << endl;
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include<stdlib.h>
#include"port.h"
using std::cout;
using std::endl;
using std::cin;
int main()
{
char *b1 = "benzs";
char *s1 = "normal";
int bo1 = 2;
Port p1(b1, s1, bo1);
Port p2 = p1;
p1.Show();
p2.Show();
cout << p1;
cout << p2;
char *n1 = "bz";
int y1 = 2001;
VintagePort v1(b1, bo1, n1, y1);
VintagePort v2 = v1;
v1.Show();
v2.Show();
cout << v1;
cout << v2;
system("pause");
return 0;
}
C++ Primer Plus 第六版 第十三章 练习题参考答案
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...