⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 程序12.5:综合应用2.cpp

📁 学习C++的一些范例
💻 CPP
字号:
/* 程序12.5:综合应用2.cpp:*/
#include<iostream>		//包含头文件
#include<string>		//包含头文件
using namespace std;	//使用名字空间std
class publication		//声明基类publication
{
protected:
	char *title;		//书的标题变量
	float price;		//书的价格变量
public:
	void getData(char *name,float rate);//声明基类成员函数
	void putData();						//声明基类成员函数
};
void publication::getData(char *name,float rate)
{
	title=name;
	price=rate;
}
void publication::putData()	
{
	cout<<"Title : "<<title<<endl;
	cout<<"Price : "<<price<<endl;
}
class book:public publication	//声明子类book
{
	int pageCount;
public:
	void getData(char *name,float price,int pages)
	{
		publication::getData(name,price);
		pageCount=pages;
	}
    void putData()
    {
		publication::putData();
		cout<< "No of Pages:"<<pageCount<<endl;
    }
};
class tape: public publication
{
	int playingTime;
public:
	void getData(char *name,float price,int time)
	{
		publication::getData(name, price);
        playingTime=time;
	}
	void putData()
	{
		publication::putData();
        cout<< "Playing time:"<<playingTime<<endl;
	}
};
/*int main()	//错误的main()函数,无法完成滞后联编
{	//因为子类和基类getData()函数不同(参数个数不同)
	publication *Ptr;
	Ptr= new book;
	Ptr->getData("C++教程书",50,400);	
	Ptr->putData();
	Ptr= new tape;
	Ptr->getData("C++教程盘",15,60);	
	Ptr->putData();
	delete Ptr;
	return 0;
}*/
int main()
{
	book *Ptr1= new book;
	Ptr1->getData("C++教程书",50,400);	
	Ptr1->putData();
	delete Ptr1;
	tape *Ptr2= new tape;
	Ptr2->getData("C++教程盘",15,60);	
	Ptr2->putData();
	delete Ptr2;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -