我想为我的c + +类写一些代码。我正在使用eclipse。我很难理解问题中的一些说明。在C + +指针阵列
我创建了一个名为Ship的基类,然后为我的CruiseShip类和CargoShip类使用了继承。
对于游轮类,我奉命创建
,它覆盖在基类中的打印功能打印功能。 CruiseShip 班级的打印功能应只显示船名和乘客的最大号码 。
并且类似地对于CargoShip类
重写在基类的打印功能的打印功能。班级的打印功能应该只显示船名和船的货物容量。
我不确定在基类中“覆盖”打印函数意味着什么。
它也指示我
证明有船舶指针数组程序的类。数组 元素应使用动态分配的Ship, CruiseShip和CargoShip对象的地址进行初始化。然后程序应该遍历数组,每个对象的打印函数调用 。
#include <iostream>
#include <string>
using namespace std;
class Ship
{
protected:
string ship_name;
int year_built;
public:
Ship()
{
ship_name="";
year_built=0;
}
void set_ship_name(string str)
{
ship_name=str;
}
void set_year(int y)
{
year_built=y;
}
int get_year()
{
return year_built;
}
string get_ship_name()
{
return ship_name;
}
void print(string, int)
{
cout<<"Ship name is "<<ship_name<<" and it was built in the year "<<year_built<<endl;
}
};
class CruiseShip: public Ship
{
private:
int max_passengers;
public:
CruiseShip()// :Ship(str,year)
{
max_passengers=0;
}
void set_passengers(int pass)
{
max_passengers=pass;
}
int get_passengers()
{
return max_passengers;
}
void print1(string, int)
{
cout<<"Ship name is "<<get_ship_name()<<" and max number of passengers are "<<max_passengers<<endl;
}
};
class CargoShip: public Ship
{
private:
int cargo_capacity_in_tons;
public:
CargoShip()//:Ship (str,year)
{
cargo_capacity_in_tons=0;
}
void set_capacity(int pass)
{
cargo_capacity_in_tons=pass;
}
int get_capacity()
{
return cargo_capacity_in_tons;
}
void print2(string, int)
{
cout<<"Ship name is "<<get_ship_name()<<" and its capacity is "<<cargo_capacity_in_tons<<" Tons."<<endl;
}
};
int main(){
CruiseShip ship1;
CargoShip ship2;
string ship_name1;
string ship_name2;
int year_built1;
int year_built2;
int max_passengers;
int cargo_capacity_in_tons;
cout<<"What is the name of the cruise ship?"<<endl;
cin>>ship_name1;
ship1.set_ship_name(ship_name1);
cout<<"What year was "<<ship_name1<<" built in?"<<endl;
cin>>year_built1;
ship1.set_year(year_built1);
cout<<"What is the maximum capacity of "<<ship_name1<<"?"<<endl;
cin>>max_passengers;
ship1.set_passengers(max_passengers);
//ship1.print(ship_name1, year_built1);
ship1.print1(ship_name1, max_passengers);
cout<<"What is the name of the cargo ship?"<<endl;
cin>>ship_name2;
ship2.set_ship_name(ship_name2);
cout<<"What year was "<<ship_name2<<" built in?"<<endl;
cin>>year_built2;
ship2.set_year(year_built2);
cout<<"What is the maximum capacity of "<<ship_name2<<" in tons?"<<endl;
cin>>cargo_capacity_in_tons;
ship2.set_capacity(cargo_capacity_in_tons);
ship2.print2(ship_name2, cargo_capacity_in_tons);
return 0;
}
“我使用eclipse作为我的编译器。” Eclipse是一个IDE,而不是编译器。 – MrEricSir
@MrEricSir的意思是说,你应该谷歌“虚拟功能的C + +”和“基类指针多态C++”大多数人不会帮助你在这里B/C你没有表现出努力研究这个问题。 – TriHard8
我不知道。我会编辑我的问题... @MrEricSir – redcardinal