2015-12-07 130 views
1

我想为我的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; 
} 
+2

“我使用eclipse作为我的编译器。” Eclipse是一个IDE,而不是编译器。 – MrEricSir

+3

@MrEricSir的意思是说,你应该谷歌“虚拟功能的C + +”和“基类指针多态C++”大多数人不会帮助你在这里B/C你没有表现出努力研究这个问题。 – TriHard8

+0

我不知道。我会编辑我的问题... @MrEricSir – redcardinal

回答

3

Let's说,你有以下类:

class Animal 
{ 
private: 
    int x; 
    int y; 
public: 
    virtual string sound() {return "Animal";} 
    void move() {x += 1; y+=1;} 
}; 

class Cow 
{ 
    string sound() {return "Muh"} //this is overriding 
    string sound(string soundYouWant) {return soundYouWant;} //this is not overriding as string sound(string soundYouWant) is not the same as string sound() 
    void move() {x += 1; y+=1;} //this is also not overriding as move() in Animal has no virtual 
}; 

所以总结一下,压倒一切意味着你必须在基类虚方法和派生你重新声明它类。这样,您可以为每个派生类重新定义它(对于基类及其派生类,方法体可以不同)。

我们动态分配数组:

int size; 
std::cin >> size; 
int *array = new int[size]; //the array is stored on the heap 
delete[] array; //deallocates the array and so frees the memory 

如果在栈上创建的阵列(没有新的),则要么必须硬编码其使用尺寸的文字(0,1,2,...)或使用const int variableName。这样,编译器在编译期间知道数组的大小。所以在编写程序时你必须知道数组的大小。因此,编译器不会允许你这样做:std::cin >> size;

使用新的(动态数组),您可以在编译时指定数组大小。因此,让程序计算数组大小或将其作为用户输入是合法的。有了动态数组,你也有很多很多,比使用小堆栈(stackoverflow)更多的内存。

int *array:显然内存内容被解释为整数。 *array指向数组的第一个元素。 int *array不知道数组的大小。你必须自己跟踪。

new int[size]:您正在为堆上的大小*整数保留空间。

您可能知道C++没有垃圾收集器。这是delete[] array;发挥作用。当您不再需要array(这包括指向array的其他指针)时,应该拨打delete来释放内存。对于小型,短时间运行的程序,忘记它无关紧要,因为操作系统会在程序终止后释放内存。不过,你应该使用delete作为不使用它仍然很糟糕,并会导致更大的程序的麻烦。如果在班级中使用array,则应将delete放在班级的分解器(~clasname())中。