65,211
社区成员
发帖
与我相关
我的任务
分享
/*1.为CPublication 类定义小于运算符(‘<’)运算符重载函数。CPublication类对象大小的比较是根据其标题(p_title)的字符串的大小
来实现的。(提示:使用strcmp()字符串比较库函数)
2.修改CPublication类派生的CBook和CJournal两个类(CBook类增加作者,页数,出版社三个数据成员以及相应的构造,析购,
设置和打印成员函数。CJournal增加主办单位一个数据成员以及相应的构造,析购,设置和打印成员函数。),
对这两个类都进行赋值运算符重载和增加拷贝构造函数。
(如果有必要,可以对基类数据成员访问属性进行相应修改)
3.写主函数对新增功能进行测试。*/
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
class CPublication
{
protected:
char *p_title;
float price;
char *p_date;
bool on_loan;
public:
CPublication(const char *p_t, float p, const char *p_d)
{
p_title = new char [strlen(p_t) + 1];
strcpy (p_title,p_t);
price = p;
p_date = new char [strlen(p_d) + 1];
strcpy (p_date,p_d);
on_loan = true;
}
CPublication(const CPublication& c)
{
p_title = new char [strlen(c.p_title) + 1];
strcpy (p_title,c.p_title);
price = c.price;
p_date = new char [strlen(c.p_date)+1];
strcpy (p_date,c.p_date);
on_loan = true;
}
void set(const char *p_t, float p, const char *p_d)
{
price = p;
delete [] p_title;
p_title = new char [strlen(p_t)+1];
strcpy (p_title,p_t);
delete [] p_date;
p_date = new char [strlen(p_d)+1];
strcpy (p_date,p_d);
on_loan = true;
}
bool borrowIt()
{
if(on_loan == true)
{
on_loan = false;
return true;
}
else
{
cout << "该书已借出!" << endl;
return false;
}
}
bool returnIt()
{
on_loan = true;
return true;
}
void print() const
{
cout << "书名:" << p_title << endl;
cout << setiosflags(ios::fixed) << setprecision(2) << "价格:" << price << endl;
cout << "出版日期:" << p_date << endl;
if(on_loan == true)
cout << "可借出" << endl;
else
cout << "已借出" << endl;
}
friend bool operator < (const CPublication &c, const CPublication &d);
virtual ~CPublication()
{
delete [] p_title;
delete [] p_date;
}
};
bool operator < (const CPublication &c, const CPublication &d)
{
if(strcmp(c.p_title,d.p_title) < 0)
return true;
else
return false;
}
class CBook : public CPublication
{
protected:
char *b_author;
int pages;
char *b_press;
public:
CBook(const char *p_t, float p, const char *p_d, const char *b_a, int ps, const char *b_p):CPublication(p_t,p,p_d)
{
b_author = new char [strlen(b_a) + 1];
strcpy (b_author,b_a);
pages = ps;
b_press = new char [strlen(b_p) + 1];
strcpy (b_press,b_p);
}
CBook(const CBook &c):CPublication(c)
{
pages = c.pages;
delete [] b_author;
b_author = new char [strlen(c.b_author) + 1];
strcpy (b_author,c.b_author);
delete [] b_press;
b_press = new char [strlen(c.b_press) + 1];
strcpy (b_press,c.b_press);
}
void set(const char *b_a, int ps, const char *b_p)
{
pages = ps;
delete [] b_author;
b_author = new char [strlen(b_a) + 1];
strcpy (b_author,b_a);
delete [] b_press;
b_press = new char [strlen(b_p) + 1];
strcpy (b_press,b_p);
}
void print() const
{
cout << "书名:" << p_title << endl;
cout << setiosflags(ios::fixed) << setprecision(2) << "价格:" << price << endl;
cout << "出版日期:" << p_date << endl;
cout << "作者:" << b_author << endl;
cout << "页数:" << pages << endl;
cout << "出版社:" << b_press << endl;
if(on_loan == true)
cout << "可借出" << endl;
else
cout << "已借出" << endl;
}
friend bool operator < (CBook &c, CBook &d);
~CBook()
{
delete [] b_author;
delete [] b_press;
}
};
bool operator < (CBook &c, CBook &d)
{
if(strcmp(c.b_author,d.b_author) < 0)
return true;
else
return false;
}
class CJournal : public CPublication
{
protected:
char *j_host;
public:
CJournal(const char *p_t, float p, const char *p_d, const char *j_h):CPublication(p_t,p,p_d)
{
j_host = new char [strlen(j_h) + 1];
strcpy (j_host,j_h);
}
CJournal(const CJournal &c):CPublication(c)
{
delete [] j_host;
j_host = new char [strlen(c.j_host) + 1];
strcpy (j_host,c.j_host);
}
void set(const char *j_h)
{
delete [] j_host;
j_host = new char [strlen(j_h) + 1];
strcpy (j_host,j_h);
}
void print() const
{
cout << "书名:" << p_title << endl;
cout << setiosflags(ios::fixed) << setprecision(2) << "价格:" << price << endl;
cout << "出版日期:" << p_date << endl;
cout << "主办单位:" << j_host << endl;
if(on_loan == true)
cout << "可借出" << endl;
else
cout << "已借出" << endl;
}
friend bool operator < (CJournal &c, CJournal &d);
~CJournal()
{
delete [] j_host;
}
};
bool operator < (CJournal &c, CJournal &d)
{
if(strcmp(c.j_host,d.j_host) < 0)
return true;
else
return false;
}
int main()
{
CPublication book("面向对象程序设计C++语言编程",32.00,"2008.1");
book.borrowIt();
book.returnIt();
book.print();
CPublication bookk("模拟电子技术",25.00,"2008.7");
bookk.borrowIt();
bookk.returnIt();
bookk.print();
if(book < bookk)
cout << ">>>>>>>book's title is less than bookk's title" << endl;
else
cout << ">>>>>>>book's title is longer than(equal) bookk's title" << endl;
cout << "----------------------------------------------------------------------------" << endl;
CBook bbook("数字电路与逻辑设计",21.00,"2006.11","蔡良伟",260,"西安电子科技大学出版社");
bbook.print();
CBook bbookk("数据结构(C语言版)",22.00,"2008.3","严蔚敏|吴伟民",335,"清华大学出版社");
bbookk.print();
if(bbook < bbookk)
cout << ">>>>>>>bbook's author is less than bbookk's author" << endl;
else
cout << ">>>>>>>bbook's author is longer than(equal) bbookk's author" << endl;
cout << "----------------------------------------------------------------------------" << endl;
CJournal jbook("概率论与数理统计",19.30,"2008.4","浙江大学");
jbook.print();
CJournal jbookk("物理学下册",31.00,"2007.12","东南大学");
jbookk.print();
if(jbook < jbookk)
cout << ">>>>>>>jbook's host is less than jbookk's host" << endl;
else
cout << ">>>>>>>jbook's host is longer than(equal) jbookk's host" << endl;
return 0;
}