#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();
~Cd();
virtual void Report()const;
Cd& operator=(const Cd& d);
};
class Classic:public Cd
{
private:
char main_productor[20];
public:
Classic(const char* s1,const char* s2, const char* s3, int n, double x);
Classic(const char*s3,const Cd& d);
Classic();
~Classic();
virtual void Report()const;
Classic& operator=(const Classic& d);
};
#endif // !13_1_H_
#include"13_1.h"
#include<iostream>
#include<cstring>
using namespace std;
Cd::Cd(const char* s1,const char* s2, int n, double x)
{
strncpy_s(performers, s1, 50);
performers[49] = '\0';
strncpy_s(label, s2, 20);
label[19] = '\0';
selections = n;
playtime = x;
}
Cd::Cd(const Cd& d)
{
strncpy_s(performers, d.performers, 50);
performers[49] = '\0';
strncpy_s(label, d.label, 20);
label[19] = '\0';
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0.0;
}
Cd::~Cd()
{
}
void Cd::Report()const
{
cout << "Performers:" << performers<<endl;
cout << "Label:" << label<<endl;
cout << "Selections:" << selections << endl;
cout << "Playtime:" << playtime << endl;
}
Cd& Cd::operator=(const Cd& d)
{
if (this == &d)
return *this;
strncpy_s(performers, d.performers, 50);
performers[49] = '\0';
strncpy_s(label, d.label, 20);
label[19] = '\0';
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x) :Cd(s1, s2, n, x)
{
strncpy_s(main_productor, s3, 20);
main_productor[19] = '\0';
}
Classic::Classic(const char*s3,const Cd& d) : Cd(d)
{
strncpy_s(main_productor, s3, 20);
main_productor[19] = '\0';
}
Classic::Classic() : Cd()
{
main_productor[0] = '\0';
}
Classic::~Classic() {}
void Classic::Report()const
{
Cd::Report();
cout << "Main_productor:" << main_productor << endl;
}
Classic& Classic::operator=(const Classic& d)
{
if (this == &d)
return *this;
Cd::operator=(d);
strncpy_s(main_productor, d.main_productor, 20);
main_productor[19] = '\0';
return *this;
}
#include<iostream>
using namespace std;
#include"13_1.h"
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", "Aflred 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 funcion 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();
}
#include <iostream>
using namespace std;
#include "classic.h"
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:\n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd &disk)
{
disk.Report();
return;
}
#ifndef CD_H_
#define CD_H_
class Cd //原有的数组修改为指针便于动态分配内存;
{
private:
char *performers;
char *labels;
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);
};
#endif
#include <iostream>
#include "cd.h"
#include <cstring>
Cd::Cd(const char *s1, const char *s2, int n, double x) //用户定义构造函数;
{
performers = new char[std::strlen(s1) + 1];
std::strcpy(performers, s1);
labels = new char[std::strlen(s2) + 1];
std::strcpy(labels, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd &d) //用户定义构造函数;
{
performers = new char[std::strlen(d.performers) + 1];
std::strcpy(performers, d.performers);
labels = new char[std::strlen(d.labels) + 1];
std::strcpy(labels, d.labels);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd() //用户定义默认构造函数;
{
performers = new char[1];
performers[0] = '\0';
labels = new char[1];
labels[0] = '\0';
selections = 0;
playtime = 0.0;
}
Cd::~Cd()
{
delete[] performers;
delete[] labels;
}
void Cd::Report() const
{
std::cout << "Performers: " << performers << std::endl;
std::cout << "Label: " << labels << std::endl;
std::cout << "Selections: " << selections << std::endl;
std::cout << "Playtime: " << playtime << std::endl;
return;
}
Cd &Cd::operator=(const Cd &d) //重载赋值运算符;
{
if (this == &d)
{
return *this;
}
delete[] performers; //释放原有已分配的内存空间;
delete[] labels; //同上;
performers = new char[std::strlen(d.performers) + 1];
std::strcpy(performers, d.performers);
labels = new char[std::strlen(d.labels) + 1];
std::strcpy(labels, d.labels);
selections = d.selections;
playtime = d.playtime;
return *this;
}
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include "cd.h"
class Classic : public Cd //公有继承派生类;
{
private:
char *cdstr;
public:
Classic();
Classic(const char *s1, const char *s2, const char *s3, int n, double x);
Classic(const char *s, const Cd &d);
~Classic();
virtual void Report() const;
Classic &operator=(const Classic &cs);
};
#endif
#include <iostream>
#include <cstring>
#include "classic.h"
Classic::Classic() : Cd()
{
cdstr = new char[1];
cdstr[0] = '\0';
}
Classic::Classic(const char *s, const char *s1, const char *s2, int n, double x) : Cd(s1, s2, n, x) //基类构造函数的成员列表初始化;
{
cdstr = new char[std::strlen(s) + 1];
std::strcpy(cdstr, s);
}
Classic::Classic(const char *s, const Cd &d) : Cd(d) //基类构造函数的成员列表初始化;
{
cdstr = new char[std::strlen(s) + 1];
std::strcpy(cdstr, s);
}
Classic::~Classic()
{
delete[] cdstr;
}
void Classic::Report() const
{
Cd::Report();
std::cout << "Major article in the CD is: " << cdstr << std::endl;
std::cout.put('\n');
return;
}
Classic &Classic::operator=(const Classic &cs)
{
if (this == &cs)
{
return *this;
}
delete[] cdstr;
Cd::operator=(cs);
cdstr = new char[std::strlen(cs.cdstr) + 1];
std::strcpy(cdstr, cs.cdstr);
return *this;
}
//
// Test.h
// HelloWorld
//
// Created by feiyin001 on 16/12/21.
// Copyright (c) 2016年 FableGame. All rights reserved.
//
#ifndef _Test_H_
#define _Test_H_
#include <iostream>
using namespace std;
namespace FableGame
{
class ABC
{
private:
char* label;
int rating;
public:
ABC(const char* l = "null", int r = 0);
ABC(const ABC& rs);
virtual ~ABC();
ABC& operator=(const ABC& rs);
friend std::ostream& operator<<(std::ostream& os, const ABC& rs);
virtual void view() = 0;
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 ABC& ls);
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 ABC& hs);
hasDMA(const hasDMA& hs);
~hasDMA();
hasDMA& operator=(const hasDMA& hs);
friend std::ostream& operator<<(std::ostream& os, const hasDMA& hs);
virtual void view();
};
}
#endif
//
// Test.cpp
// HelloWorld
//
// Created by feiyin001 on 16/12/21.
// Copyright (c) 2016年 FableGame. All rights reserved.
//
#include "Test.h"
#include <iostream>
#include <cstdlib>
using namespace std;
using namespace FableGame;
FableGame::ABC::ABC(const char* l /*= "null"*/, int r /*= 0*/)
{
int size = strlen(l) + 1;
label = new char[size];
strcpy_s(label, size, l );
rating = r;
}
FableGame::ABC::ABC(const ABC& rs)
{
int size = strlen(rs.label) + 1;
label = new char[size];
strcpy_s(label, size, rs.label);
rating = rs.rating;
}
FableGame::ABC::~ABC()
{
delete[] label;
}
ABC& FableGame::ABC::operator=(const ABC& rs)
{
if (this == &rs)
{
return *this;
}
delete[] label;
int size = strlen(rs.label) + 1;
label = new char[size];
strcpy_s(label, size, rs.label );
rating = rs.rating;
return *this;
}
void FableGame::ABC::view()
{
cout << "Label: " << getLabel() << endl;
cout << "Rating: " << getRating() << endl;
}
std::ostream& FableGame::operator<<(std::ostream& os, const ABC& rs)
{
os << "Label: " << rs.label << endl;
os << "Rating: " << rs.rating << endl;
return os;
}
FableGame::lacksDMA::lacksDMA(const char* c, const char* l, int r) : ABC(l, r)
{
strncpy_s(color, c, 39);
color[39] = '\0';
}
FableGame::lacksDMA::lacksDMA(const char* c, const ABC& rs) :ABC(rs)
{
strncpy_s(color, c, COL_LEN - 1);
color[COL_LEN - 1] = '\0';
}
void FableGame::lacksDMA::view()
{
ABC::view();
cout << "Color: " << color << endl;
}
std::ostream& FableGame::operator<<(std::ostream& os, const lacksDMA& ls)
{
os << (const ABC&)ls;
os << "Color: " << ls.color << endl;
return os;
}
FableGame::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);
}
FableGame::hasDMA::hasDMA(const char* s, const ABC& rs) :ABC(rs)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
FableGame::hasDMA::hasDMA(const hasDMA& hs) :ABC(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
FableGame::hasDMA::~hasDMA()
{
delete[] style;
}
hasDMA& FableGame::hasDMA::operator=(const hasDMA& hs)
{
if (this == &hs)
{
return *this;
}
ABC::operator=(hs);
delete[] style;
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
return *this;
}
void FableGame::hasDMA::view()
{
ABC::view();
cout << "Style: " << style << endl;
}
std::ostream& FableGame::operator<<(std::ostream& os, const hasDMA& hs)
{
os << (const ABC&)hs;
os << "Style: " << hs.style << endl;
return os;
}
FableGame::baseDMA::baseDMA(const char* l /*= "null"*/, int r /*= 0*/) :ABC(l, r)
{
}
FableGame::baseDMA::baseDMA(const baseDMA& rs) : ABC(rs)
{
}
void FableGame::baseDMA::view()
{
ABC::view();
}
#include <iostream>
#include "Test.h"
#include <string>
using namespace std;
using namespace FableGame;
const int ARR_SIZE = 3;
int main(int argc, const char * argv[])
{
ABC* p_ABC[ARR_SIZE];
char temp[40];
int tempnum;
int kind;
for (int i = 0; i < ARR_SIZE; i ++)
{
cout << "Enter label: ";
cin.getline( temp, 40);
cout << "Enter Rating: ";
cin >> tempnum;
cout << "Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: ";
while (cin >> kind && (kind != 1 && kind != 2 && kind != 3))
{
cout << "Enter either 1 or 2 or 3: ";
}
cin.get();
if (kind == 1)
{
p_ABC[i] = new baseDMA(temp, tempnum);
}
else if (kind == 2)
{
char color[40];
cout << "Enter the color: ";
cin.getline(color, 40);
p_ABC[i] = new lacksDMA(color, temp, tempnum);
}
else if (kind == 3)
{
char style[40];
cout << "Enter the style: ";
cin.getline(style, 40);
p_ABC[i] = new hasDMA(style, temp, tempnum);
}
}
cout << endl;
for (int i = 0; i < ARR_SIZE; i ++)
{
p_ABC[i]->view();
cout << endl;
}
for (int i = 0; i < ARR_SIZE; i ++)
{
delete p_ABC[i];
}
cout << "Done." <<endl;
return 0;
}
//
// Test.h
// HelloWorld
//
// Created by feiyin001 on 16/12/21.
// Copyright (c) 2016年 FableGame. All rights reserved.
//
#ifndef _Test_H_
#define _Test_H_
#include <iostream>
using namespace std;
namespace FableGame
{
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();
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
//
// Test.cpp
// HelloWorld
//
// Created by feiyin001 on 16/12/21.
// Copyright (c) 2016年 FableGame. All rights reserved.
//
#include "Test.h"
#include <iostream>
#include <cstdlib>
using namespace std;
using namespace FableGame;
FableGame::Port::Port(const char* br /*= "none"*/, const char* st /*= "none"*/, int b /*= 0*/)
{
brand = new char[strlen(br) + 1];
strcpy_s(brand, strlen(br) + 1, br);
strcpy_s(style, 20, st);
bottles = b;
}
FableGame::Port::Port(const Port& p)
{
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, 20, p.style);
bottles = p.bottles;
}
Port& FableGame::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, 20, p.style);
bottles = p.bottles;
return *this;
}
Port& FableGame::Port::operator+=(int b)
{
bottles += b;
return *this;
}
Port& FableGame::Port::operator-=(int b)
{
bottles -= b;
return *this;
}
void FableGame::Port::Show() const
{
cout << "Brand: " << brand << endl;
cout << "Kind : " << style << endl;
cout << "Bottles : " << bottles << endl;
}
ostream& FableGame::operator<<(ostream& os, const Port& p)
{
os << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
FableGame::VintagePort::VintagePort() :Port()
{
}
FableGame::VintagePort::VintagePort(const char* br, int b, const char* nn, int y) : Port(br, "", b)
{
nickname = new char[strlen(nn) + 1];
strcpy_s(nickname, strlen(nn) + 1, nn);
year = y;
}
FableGame::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& FableGame::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 FableGame::VintagePort::show() const
{
Port::Show();
cout << "nickname: " << nickname << endl;
cout << "year: " << year << endl;
}
ostream& FableGame::operator<<(ostream& os, const VintagePort& vp)
{
os << (const Port&)vp;
os << ", " << vp.nickname << ", " << vp.year;
return os;
}