您的位置:首页 > 编程语言 > C语言/C++

chapter13test2

2015-06-17 19:46 309 查看
这一题跟第一题类似,不过要把原来的字符数组换成字符指针,然后构造函数、赋值函数、=重载注意使用new char[],还有析构函数有delete[],别的没有变化

cd.h

#ifndef CD_H_

#define CD_H_

#include<iostream>
class cd
{
private:
char *performer;
char *label;
int selection;
double playtime;
public:
cd(char *p="none", char *l="none", int s=0, double t=0.0);
cd(const cd&c);
~cd();
virtual void report() const;
virtual cd &operator=(const cd &c);
};

class classic :public cd
{
private:
char *work;
public:
classic(char *w="none", char *p = "none", char *l = "none", int s = 0, double t = 0.0);
classic(const classic &c);
virtual void report() const;
virtual classic &operator=(const classic &c);
};

#endif

cd.cpp

#include<iostream>

#include"CD.h"

#include<cstring>

cd::cd(char *p, char *l, int s, double t)

{
int m = strlen(p) + 1; int n = strlen(l) + 1;
performer = new char[m]; label = new char
;
strcpy_s(performer, m, p);
strcpy_s(label, n, l);
selection = s;
playtime = t;

}

cd::cd(const cd&c)

{
int m = strlen(c.performer) + 1; int n = strlen(label) + 1;
performer = new char[m]; label = new char
;
strcpy_s(performer, m, c.performer);
strcpy_s(label, n, c.label);
selection = c.selection;
playtime = c.playtime;

}

cd::~cd()

{
delete[]performer;
delete[]label;

}

void cd::report() const

{
std::cout << "Performer :" << performer << std::endl;
std::cout << "Label :" << label << std::endl;
std::cout << "Selection :" << selection << std::endl;
std::cout << "Playtime :" << playtime << std::endl;

}

cd &cd::operator=(const cd &c)

{
if (this == &c)
return *this;
else
{
delete[]performer; delete[]label;
int m = strlen(c.performer) + 1; int n = strlen(label) + 1;
performer = new char[m]; label = new char
;
strcpy_s(performer, m, c.performer);
strcpy_s(label, n, c.label);
selection = c.selection;
playtime = c.playtime;
return *this;
}

}

classic::classic(char *w, char *p, char *l, int s, double t) :cd(p,l,s,t)

{
work = new char[strlen(w) + 1];
strcpy_s(work, strlen(w)+1, w);

}

classic::classic(const classic &c) : cd(c)

{
work = new char[strlen(c.work) + 1];
strcpy_s(work, strlen(c.work) + 1, c.work);

}

void classic::report() const

{
std::cout << "Work :" << work << std::endl;
cd::report();

}

classic &classic::operator=(const classic &c)

{
if (this == &c)
return *this;
else
{
cd::operator=(c);
delete[]work;
work = new char[strlen(c.work) + 1];
strcpy_s(work, strlen(c.work) + 1, c.work);
return *this;
}

}

user.cpp

#include<iostream>

#include"CD.h"

using namespace std;

void bravo(const cd &c);

int main()

{
cd c1("Beatles", "Capitol", 14, 35.5);
classic c2 = classic("Piano", "Alfred", "Philips", 2, 57.17);
cd *pcd = &c1;
cout << "Using object directly:\n";
c1.report();
c2.report();

cout << "Using type pointer to object:\n";
pcd->report();
pcd = &c2;
pcd->report();

cout << "Calling a function with referance :\n";
bravo(c1);
bravo(c2);

cout << "Testing assignment.\n";
classic copy;
copy = c2;
copy.report();

return 0;

}

void bravo(const cd &c)

{
c.report();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++