C++.Homework.Inheritance.01


===========================Publication.h============================

#pragma once
#define LEN 20                             //title、name等可能的最长字节数
#include <string>
#include <iostream>
using namespace std;
class Publication{
private:
	char *title;
	char *name;
	float price;
	char *date;
public:
	Publication();
	Publication(char*,char*,float,char*);
	~Publication();
	void inputData();
	void display();
};
Publication::Publication(){
	title=new char[LEN];
	strcpy(title,"NULL");
	name=new char[LEN];
	strcpy(name,"NULL");
	price=0.00;
	date=new char[LEN];
	strcpy(date,"____/__/__");
}
Publication::Publication(char *title,char *name,float price,char *date){
	this->title=new char[strlen(title)+1];
	strcpy(this->title,title);
	this->name=new char[strlen(name)+1];
	strcpy(this->name,name);
	this->price=price;
	this->date=new char[strlen(date)+1];
	strcpy(this->date,date);
}
Publication::~Publication(){
	delete []title;
	delete []name;
	delete []date;
}
void Publication::inputData(){
	cout<<"Please input the title:";
	cin>>title;
	cout<<"Please input the name:";
	cin>>name;
	cout<<"Please input the price:";
	cin>>price;
	cout<<"Please input the date:";
	cin>>date;
}
void Publication::display(){
	cout<<"Title:"<<title<<endl;
	cout<<"Name :"<<name<<endl;
	cout<<"Price:"<<price<<endl;
	cout<<"Date :"<<date<<endl;
}

===========================Book.h============================

#include "Publication.h"
class Book:public Publication{
private:
	int page;
public:
	Book();
	Book(char*,char*,float,char*,int);
	void inputData();
	void display();
};
Book::Book(){
	page=0;
}
Book::Book(char *title,char *name,float price,char *date,int page):Publication(title,name,price,date){
	this->page=page;
}
void Book::inputData(){
	Publication::inputData();
	cout<<"Please input the page:";
	cin>>page;
}
void Book::display(){
	Publication::display();
	cout<<"page :"<<page<<endl;
}

===========================Tape.h============================

#include "Publication.h"
class Tape:public Publication{
private:
	char *playtime;
public:
	Tape();
	Tape(char*,char*,float,char*,char*);
	~Tape();
	void inputData();
	void display();
};
Tape::Tape(){
	playtime=new char[LEN];
	strcpy(playtime,"--:--:--");
}
Tape::Tape(char *title,char *name,float price,char *date,char *playtime):Publication(title,name,price,date){
	this->playtime=new char[strlen(playtime)+1];
	strcpy(this->playtime,playtime);
}
Tape::~Tape(){
	delete []playtime;
}
void Tape::inputData(){
	Publication::inputData();
	cout<<"Please input the playtime:";
	cin>>playtime;
}
void Tape::display(){
	Publication::display();
	cout<<"Playtime:"<<playtime<<endl;
}

===========================main.cpp============================

#include "Book.h"
#include "Tape.h"
void main(){
	cout<<"=============================Book1=============================="<<endl;
	Book B1;
	B1.display();
	cout<<"=============================Book1=============================="<<endl;
	B1.inputData();
	cout<<"=============================Book1=============================="<<endl;
	B1.display();
	cout<<"=============================Book2=============================="<<endl;
	Book B2("C++面向对象程序设计(第二版)","教材",42.00,"2011/07/01",341);
	B2.display();
	cout<<"=============================Tape1=============================="<<endl;
	Tape T1;
	T1.display();
	cout<<"=============================Tape1=============================="<<endl;
	T1.inputData();
	cout<<"=============================Tape1=============================="<<endl;
	T1.display();
	cout<<"=============================Tape2=============================="<<endl;
	Tape T2("大学英语","综合训练配套DVD",0,"2012/07/01","01:23:45");
	T2.display();
	cout<<"==========================Publication1=========================="<<endl;
	Publication P1;
	P1.display();
	cout<<"==========================Publication1=========================="<<endl;
	P1.inputData();
	cout<<"==========================Publication1=========================="<<endl;
	P1.display();
	cout<<"==========================Publication2=========================="<<endl;
	Publication P2("不知道","不知道",0,"不知道");
	P2.display();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值