11_3.cpp

来自「为初学者提供的最佳的C++程序设计源程序库」· C++ 代码 · 共 82 行

CPP
82
字号
#include<iostream.h>
#include<string.h>
#define Max 20
class Station
{
protected:
	char from[Max];
	char to[Max];
public:
	Station()
	{
		strcpy(from,"");
		strcpy(to,"");
	}
	Station(char f[],char t[])
	{
		strcpy(from,f);
		strcpy(to,t);
	}
	void getdata()
	{
		cout<<"输入起始站	终点站:";
		cin>>from>>to;
	}
	void disp()
	{
		cout<<"从"<<from<<"站到"<<to<<"站";
	}
};
class Mile
{
protected:
	double mile;
public:
	Mile(){mile=0;}
	Mile(double m){mile=m;}
	void getdata()
	{
		cout<<"输入里程:";
		cin>>mile;
	}
	void disp()
	{
		cout<<"是"<<mile<<"公里";
	}
};
class Price:public Station,public Mile
{
	double price;
public:
	Price():Station(),Mile()
	{
		price=0;
	}
	Price(char f[],char t[],double m):Station(f,t),Mile(m)
	{
		price=8+(int)((m-3+0.49)*2)*0.7;
	}
	void getdata()
	{
		Station::getdata();
		Mile::getdata();
		price=8+(int)((mile-3+0.49)*2)*0.7;
	}
	void disp()
	{
		cout<<" ";
		Station::disp();
		Mile::disp();
		cout<<",价格是"<<price<<"元"<<endl;
	}
};
void main()
{
	Price p1;
	p1.getdata();
	Price p2("汽车西站","火车站",33.6);
	cout<<"输出结果为:"<<endl;
	p1.disp();
	p2.disp();
}

⌨️ 快捷键说明

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