1.cd.h:
#pragma once
class Cd
{
public:
Cd(char* s1, char* s2, int n, double x);
Cd(const Cd& d);
Cd();
virtual ~Cd();
virtual void Report();
virtual Cd& operator=(const Cd& d);
private:
char performers[50];
char label[20];
int selections;
double playtime;
};
class Classic:public Cd
{
public:
Classic(char*detail, char* s1, char* s2, int n, double x);
Classic();
~Classic();
Classic(const Classic& d);
void Report();
Classic& operator=(const Classic& d);
private:
char details[100];
};
cd.cpp:
#include <cstring>
#include <iostream>
#include "Cd.h"
using namespace std;
Cd::Cd()
{
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
}
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;
}
void Cd::Report() {
cout<< performers << endl;
cout << label << endl;
cout << selections << endl;
cout << 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;
}
Classic::Classic(char*s, char* s1, char* s2, int n, double x):Cd(s1,s2,n,x)
{
strcpy_s(details, strlen(s) + 1, s);
}
Classic::Classic():Cd()
{
details[0] = '\0';
}
Classic::~Classic()
{
}
Classic::Classic(const Classic& d):Cd(d) {
strcpy_s(details, strlen(d.details) + 1, d.details);
}
void Classic::Report() {
Cd::Report();
cout << details << endl;
}
Classic& Classic::operator=(const Classic& d) {
Cd::operator=(d);
strcpy_s(details, strlen(d.details) + 1, d.details);
return *this;
}
main:
#include <iostream>
#include "Cd.h"
using namespace std;
void Bravo(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 cop;
cop = c2;
cop.Report();
return 0;
}
void Bravo(Cd &disk) { disk.Report(); }
2.字符串数组改为指针,赋值时分配内存
cd2.h:
class Cd
{
public:
Cd(const char* s1, const char* s2, int n, double x);
Cd(const Cd& d);
Cd();
virtual ~Cd();
virtual void Report();
virtual Cd& operator=(const Cd& d);
private:
char* performers;
char* label;
int selections;
double playtime;
};
class Classic :public Cd
{
public:
Classic(const char*detail, const char* s1, const char* s2, int n, double x);
Classic();
~Classic();
Classic(const Classic& d);
void Report();
Classic& operator=(const Classic& d);
private:
char* details;
};
cd.cpp:
#include <cstring>
#include <iostream>
#include "Classic.h"
using namespace std;
Cd::Cd()
{
performers = new char[1];
performers[0] = '\0';
label = new char[1];
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
}
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_s(performers, strlen(s1) + 1, s1);
strcpy_s(label, strlen(s2) + 1, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd& d) {
performers = new char[strlen(d.performers)] + 1;
label = new char[strlen(d.label)] + 1;
strcpy_s(performers, strlen(d.performers) + 1, d.performers);
strcpy_s(label, strlen(d.label) + 1, d.label);
selections = d.selections;
playtime = d.playtime;
}
void Cd::Report() {
cout << performers << endl;
cout << label << endl;
cout << selections << endl;
cout << playtime << endl;
}
Cd& Cd::operator=(const Cd& d) {
performers = new char[strlen(d.performers)] + 1;
label = new char[strlen(d.label)] + 1;
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;
}
Classic::Classic(const char*s, const char* s1, const char* s2, int n, double x) :Cd(s1, s2, n, x)
{
details = new char[strlen(s)] + 1;
strcpy_s(details, strlen(s) + 1, s);
}
Classic::Classic() : Cd()
{
details = new char[1];
details[0] = '\0';
}
Classic::~Classic()
{
}
Classic::Classic(const Classic& d) :Cd(d) {
details = new char[strlen(d.details)] + 1;
strcpy_s(details, strlen(d.details) + 1, d.details);
}
void Classic::Report() {
Cd::Report();
cout << details << endl;
}
Classic& Classic::operator=(const Classic& d) {
Cd::operator=(d);
details = new char[strlen(d.details)] + 1;
strcpy_s(details, strlen(d.details) + 1, d.details);
return *this;
}
构造函数中的字符串参数,有的编译器要求类型匹配,(具体原因是编译器设置?我也不知道,请高手来解惑哈)可改为const
3. dma.h:
#pragma once
class DMA
{
public:
DMA();
~DMA();
virtual void View() = 0;
virtual void Data() = 0;
};
class BaseDMA:public DMA
{
public:
BaseDMA(const char* l = "null",int r = 0);
~BaseDMA();
virtual void View();
virtual void Data();
private:
char* name;
int num;
};
class LackDMA : public BaseDMA
{
public:
LackDMA(int o = 0, const char* l = "null", int r = 0);
~LackDMA();
virtual void View();
private:
int lack;
};
class HasDMA :public BaseDMA {
public:
HasDMA(const char* o = "none",const char* l = "null", int r = 0);
~HasDMA();
virtual void View();
private:
char* other;
};
dma.cpp:
#include <iostream>
#include<cstring>
#include "DMA.h"
using namespace std;
DMA::DMA()
{
}
DMA::~DMA()
{
}
BaseDMA::BaseDMA(const char* l, int r) {
int len = strlen(l) + 1;
name = new char[len];
strcpy_s(name, len, l);
num = r;
}
BaseDMA::~BaseDMA() {
delete[] name;
name = nullptr;
}
void BaseDMA::Data() {
cout << "name is " << name << " and number is " << num;
}
void BaseDMA::View() {
cout << "The BaseDMA's ";
Data();
cout << endl;
}
LackDMA::LackDMA(int o, const char* l, int r):BaseDMA(l,r) {
lack = o;
}
LackDMA::~LackDMA() {}
void LackDMA::View() {
cout << "The LackDMA's ";
BaseDMA::Data();
cout << " and lack is " << lack;
cout << endl;
}
HasDMA ::HasDMA(const char* o, const char* l, int r):BaseDMA(l,r) {
int len = strlen(o) + 1;
other = new char[len];
strcpy_s(other, len, o);
}
HasDMA ::~HasDMA() {
delete[] other;
other = nullptr;
}
void HasDMA::View() {
cout << "The HasDMA's ";
BaseDMA::Data();
cout << " and other is " << other;
cout << endl;
}
main:
#include <iostream>
#include "DMA.h"
using namespace std;
const int NUM = 4;
const int LEN = 40;
int main()
{
DMA * dmas[NUM];
int i;
for (i = 0; i < NUM; i++) {
char temp[LEN];
long tempnum;
char kind;
cout << "Enter dma's name:";
cin.getline(temp, LEN);
cout << "Enter dma's number:";
cin >> tempnum;
cout << "Enter 1 for BaseDMA or 2 for LackDMA or 3 for HadDMA:";
while (cin >> kind && kind != '1' && kind != '2'&& kind != '3')
cout << "Enter 1 to 3:";
if (kind == '1')
dmas[i] = new BaseDMA(temp, tempnum);
else if(kind =='2'){
int lack;
cout << "Enter other number:";
cin >> lack;
dmas[i] = new LackDMA(lack,temp, tempnum);
}
else if(kind =='3'){
char other[LEN];
cout << "Enter other string:";
cin.getline(other, LEN);
dmas[i] = new HasDMA(other,temp, tempnum);
}
while (cin.get() != '\n')
continue;
}
cout << endl;
for (i = 0; i < NUM; i++) {
dmas[i]->View();
cout << endl;
}
for (i = 0; i < NUM; i++) {
delete dmas[i];
}
cout << "Done.\n";
return 0;
}
4.port,h:
#pragma once
#include <iostream>
using namespace std;
class Port
{
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);
private:
char* brand;
char style[20];
int bottles;
};
class VintagePort:public Port
{
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);
private:
char* nickname;
int year;
};
port.cpp:
#include "Port.h"
Port::Port(const char* br, const char* st, int b)
: bottles(b)
{
int len = strlen(br) + 1;
brand = new char[len];
strcpy_s(brand, len,br);
len = strlen(st) + 1;
strcpy_s(style,len, st);
}
void Port::Show()const {
cout << "Brand: " << brand << endl;
cout << "Kind: " << style << endl;
cout << "Bottles: " << bottles << endl;
}
Port::Port(const Port& p) {
int len = strlen(p.brand) + 1;
brand = new char[len];
strcpy_s(brand, len, p.brand);
len = strlen(p.style) + 1;
strcpy_s(style, len, p.style);
bottles = p.bottles;
}
Port& Port::operator=(const Port& p) {
delete[] brand;
int len = strlen(p.brand) + 1;
brand = new char[len];
strcpy_s(brand, len, p.brand);
len = strlen(p.style) + 1;
strcpy_s(style, len, p.style);
bottles = p.bottles;
return *this;
}
Port& Port::operator+=(int b) {
bottles += b;
return *this;
}
Port& Port::operator-=(int b) {
bottles -= b;
return *this;
}
ostream& operator<<(ostream& os, const Port& p) {
cout << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
VintagePort::VintagePort() :Port() {
int len = strlen("null") + 1;
nickname = new char[len];
strcpy_s(nickname, len, "null");
year = 0;
}
VintagePort::VintagePort(const char* br, int b, const char* nn, int y):Port(br,"Vintage",b) {
int len = strlen(nn) + 1;
nickname = new char[len];
strcpy_s(nickname, len, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort& vp):Port(vp) {
int len = strlen(vp.nickname) + 1;
nickname = new char[len];
strcpy_s(nickname, len, vp.nickname);
year = vp.year;
}
VintagePort& VintagePort::operator=(const VintagePort& vp) {
delete nickname;
Port::operator=(vp);
int len = strlen(vp.nickname) + 1;
nickname = new char[len];
strcpy_s(nickname, len, 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 << Port(vp);
os << ", " << vp.nickname << ", " << vp.year;
return os;
}
main:
#include <iostream>
#include "Port.h"
using namespace std;
int main()
{
Port port1("Gallo", "tawny", 23);
VintagePort vp("None", 65, "The Noble", 1989);
port1.Show();
vp.Show();
VintagePort vp2 = vp;
cout << vp2 << endl;
Port port2 = port1;
cout << port2 << endl;
port2 += 23;
cout << port2 << endl;
return 0;
}
b.基类中的虚函数可在子类中重新定义,子类不使用基类的方法则重新定义,否则不需要重新定义。
c.operator<<是友元函数,operator=是赋值运算符,都不可继承。