c++ - C++ 中的指针数组

标签 c++ arrays pointers inheritance

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

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

对于 CruiseShip 类,我被指示创建

A print function that overrides the print function in the base class. The CruiseShip class’s print function should display only the ship’s name and the maximum number of passengers.

CargoShip 类也是如此

A print function that overrides the print function in the base class. The CargoShip class’s print function should display only the ship’s name and the ship’s cargo capacity.

我不确定“覆盖”基类中的打印函数是什么意思。

它还指示我

Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be initialized with the addresses of dynamically allocated Ship , CruiseShip , and CargoShip objects. The program should then step through the array, calling each object’s print function.

#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):

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 指向数组的第一个元素。 int *array 不知道数组的大小。你必须自己跟踪。

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

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

关于c++ - C++ 中的指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34144919/

相关文章:

c - 在 C 中打印矩阵时获取垃圾值

c++ - 如何解析 KLV 数据?

c++ - 为什么不继承 C++ 构造函数?

ruby - 如何计算 Ruby 中两个普通区间的交集?

javascript - 对多维数组中的 RGB 颜色进行排序

c - 如何在 ANSI C 程序中返回字符串数组?

c++ - 在 C++ 中,访问说明符不控制静态成员的可见性吗?

c++ - 如何使 SFINAE 使用模板特化?

javascript - 将数组拆分为无序列表?

java - 我可以在 Java 中创建一个变量数组吗?