繁体   English   中英

C ++中的指针数组

[英]Array of Pointers in C++

我正在尝试为我的c ++类编写一些代码。 我正在使用eclipse。 我很难理解问题中的一些说明。

我创建了一个名为Ship的基类,然后将继承用于我的CruiseShip类和CargoShip类。

对于CruiseShip课程,我被指示创建

一个打印函数,它覆盖基类中的print函数。 CruiseShip类的打印功能应仅显示船名和最大乘客数。

同样适用于CargoShip类

一个打印函数,它覆盖基类中的print函数。 CargoShip类的打印功能应仅显示船名和船舶的货物容量。

我不确定“覆盖”基类中的print函数意味着什么。

它还指示我

演示具有Ship指针数组的程序中的类。 应使用动态分配的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;
}

假设您有以下课程:

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

如果在堆栈上创建一个数组(没有new),则必须使用文字(0,1,2,...)或使用const int variableName对其大小进行硬编码。 这样,编译器在编译期间就知道数组大小。 因此,在编写程序时必须知道数组大小。 因此,编译器不允许你这样做: std::cin >> size;

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM