完整代码如下:
编译器版本:VS2012
#include<string>#include<time.h>
#include<iostream>
using namespace std
class Date
{
public:
Date(int y, int m, int d)
{
tm_year = y
tm_month = m
tm_mday = d
}
Date(const Date& D)
{
tm_year = D.tm_year
tm_month = D.tm_month
tm_mday = D.tm_mday
}
int tm_year
int tm_month
int tm_mday
}
class Book
{
public:
Book(string Book_name, string writer):title(Book_name),author(writer){}
Book(const Book& Books)
{
title = Books.title
author = Books.author
}
~Book(){}
Book operator=(Book Books)
{
title = Books.title
author = Books.author
}
friend ostream& operator<<(ostream&, const Book&)
protected:
string title
string author
}
ostream& operator<<(ostream& os, const Book& Book_a)
{
os << "title:" << Book_a.title << endl
<< "author:" << Book_a.author << endl
return os
}
class AudioBook:public Book
{
public:
AudioBook(string Book_name, string writer, string reader, Date lent_date):Book(Book_name, writer),narrator(reader),borrow_date(lent_date){}
AudioBook(const AudioBook&)//没有必要实现
AudioBook operator=(AudioBook ab)//没有必要实现
friend ostream& operator<<(ostream&, const AudioBook&)
~AudioBook(){}//没有必要实现
private:
string narrator
Date borrow_date
}
ostream& operator<<(ostream& os, const AudioBook& audio_Book_a)
{
os << "title:" << audio_Book_a.title << endl
<< "author:" << audio_Book_a.author << endl
<< "narrator:" << audio_Book_a.narrator << endl
<< "borrow_date:" << audio_Book_a.borrow_date.tm_year
<< "-" << audio_Book_a.borrow_date.tm_month
<< "-" << audio_Book_a.borrow_date.tm_year << endl
return os
}
class PaperBook:public Book
{
public:
PaperBook(string Book_name, string writer, string id, Date lent_date):Book(Book_name, writer),Book_id(id),borrow_date(lent_date){}
PaperBook(const PaperBook&)
PaperBook operator=(PaperBook pb)
friend ostream& operator<<(ostream&, const PaperBook&)
~PaperBook(){}
private:
string Book_id
Date borrow_date
}
ostream& operator<<(ostream& os, const PaperBook& paper_Book_a)
{
os << "title:" << paper_Book_a.title << endl
<< "author:" << paper_Book_a.author << endl
<< "narrator:" << paper_Book_a.Book_id << endl
<< "borrow_date:" << paper_Book_a.borrow_date.tm_year
<< "-" << paper_Book_a.borrow_date.tm_month
<< "-" << paper_Book_a.borrow_date.tm_year << endl
return os
}
int main()
{
time_t tmNow = time(NULL)
//solution method.
//选择项目->属性->C/C++预处理器->编辑预处理器定义->添加"_CRT_SECURE_NO_WARNINGS"
//上述处理后可正常运行程序
tm* pt = gmtime(&tmNow)//Warning3 at VS2012
Date d(1900+pt->tm_yday,1+pt->tm_mon,pt->tm_mday)
AudioBook ab1("Hello","Andrew","ABC",d)
AudioBook ab2("Yesterday","Karpenter","ABC",d)
PaperBook pb1("C","Tan","ABC",d)
PaperBook pb2("C++","Andrew","1001",d)
Book bk[4]={ab1,ab2,pb1,pb2}
for(int i=0i<4i++)
cout << bk[i] << endl
return 0
}
在C语言中,不存在继承的概念。继承是面向对象语言的概念,而C语言是面向过程的语言。
C++是在C语言基础上发展出的面向对象语言,其中的类可以继承。
在C++的继承中,区分继承类型,有三种关键字可以使用:
1 公有继承,使用关键字public
2 私有继承,使用关键字private
3 保护继承,使用关键字protect。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)