chapter13test2

来源:互联网 发布:paxos算法的活锁问题 编辑:程序博客网 时间:2024/04/29 13:52

这一题跟第一题类似,不过要把原来的字符数组换成字符指针,然后构造函数、赋值函数、=重载注意使用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[n];
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[n];
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[n];
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();
}

0 0
原创粉丝点击