c++ - 用子类覆盖基类中定义的函数

标签 c++ inheritance

我是 C++ 的新手,正在尝试理解这种语言中的继承。我已经创建了一个 Ship 类,定义了一些构造函数、修改器和一个 print 函数,该函数应该 cout 关于这艘船的一些信息。我有两个派生类,一个是 Cruise 类,另一个是 Cargo 类。我第一次编译 Cruise 类的测试时一切正常。实现 Cargo 类后,我无法再编译我的代码。这两个类非常相似并且实现几乎相同。这是我的代码,我收到的错误在下面。

ship.cpp(定义船级)

#include <iostream>
#include <string>
using namespace std;

class Ship {
private:
    string _strName, _strYear;

public:
    //Constructors
    Ship();
    Ship(string, string);

    //Mutators
    void setName(string);
    void setYear(string);
    string getName();
    string getYear();

    //Methods
    virtual void print();
};

//*************************************
// Cunstructors                       *
//*************************************
Ship::Ship() {
    _strName = "NONE";
    _strYear = "NONE";
}
Ship::Ship(string strname, string strYear) {
    setName(strname);
    setYear(strYear);
}

//*************************************
// MUTATORS                           *
//*************************************
void Ship::setName(string strName) {
    _strName = strName;
}

void Ship::setYear(string strYear) {
    _strYear = strYear;
}

string Ship::getName() {
    return _strName;
}

string Ship::getYear() {
    return _strYear;
}

//*************************************
// METHODS                             *
//*************************************
virtual void Ship::print() {
    cout << _strName << endl
        << _strYear << endl;
}

CuiseShip.cpp(继承Ship的游轮类)

#include <iostream>
#include <string>
#include "ship.cpp"
using namespace std;

class CruiseShip:public Ship {
private:
    int _intMaxPassengers;

public:
    //Constructors
    CruiseShip();
    CruiseShip(string, string, int);

    //Mutators
    void setMaxPassengers(int);
    int getMaxPassengers();


    //Methods
    void print();
};

//*************************************
// Cunstructors                       *
//*************************************
CruiseShip::CruiseShip() {
    setName("NONE");
    setYear("NONE");
    setMaxPassengers(0);
}
CruiseShip::CruiseShip(string strname, string strYear, int intMaxPassengers) {
    setName(strname);
    setYear(strYear);
    setMaxPassengers(intMaxPassengers);
}

//*************************************
// MUTATORS                           *
//*************************************
void CruiseShip::setMaxPassengers(int intMaxPassengers) {
    _intMaxPassengers = intMaxPassengers;
}

int CruiseShip::getMaxPassengers() {
    return _intMaxPassengers;
}

//*************************************
// METHODS                             *
//*************************************
void CruiseShip::print() {
    string name = getName();
    string year = getYear();
    string maxPassengers = to_string(getMaxPassengers());

    cout << name << endl
        << year << endl
        << maxPassengers << endl;;
}

CargoShip.cpp(继承自ship)

#include <iostream>
#include <string>
#include "ship.cpp"
using namespace std;

class CargoShip: public Ship {
private:
    int _intCapacity;

public:
    //Constructors
    CargoShip();
    CargoShip(string, string, int);

    //Mutators
    void setCapacity(int);
    int getCapacity();


    //Methods
    void print();
};

//*************************************
// Cunstructors                       *
//*************************************
CargoShip::CargoShip() {
    setName("NONE");
    setYear("NONE");
    setCapacity(0);
}

CargoShip::CargoShip(string strname, string strYear, int intCapacity) {
    setName(strname);
    setYear(strYear);
    setCapacity(intCapacity);
}

//*************************************
// MUTATORS                           *
//*************************************
void CargoShip::setCapacity(int intCapacity) {
    _intCapacity = intCapacity;
}

int CargoShip::getCapacity() {
    return _intCapacity;
}

//*************************************
// METHODS                             *
//*************************************
void CargoShip::print() {
    string name = getName();
    string year = getYear();
    string capacity = to_string(getCapacity());

    cout << name << endl
        << year << endl
        << capacity << endl;;
}

TEST.cpp(main() 用于实现和测试代码)

#include <iostream>
#include <string>
#include "CruiseShip.cpp"
#include "CargoShip.cpp"

int main() {
    CruiseShip cruise("cruise", "1862", 25);
    CargoShip cargo("Mellinium Falcon", "a long time ago", 100);

    cruise.print();
    cargo.print();

    return 0;
}

编译错误

In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:5:7: error: redefinition of ‘class Ship’
 class Ship {
       ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:5:7: error: previous definition of ‘class Ship’
 class Ship {
       ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:27:1: error: redefinition of ‘Ship::Ship()’
 Ship::Ship() {
 ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:27:1: note: ‘Ship::Ship()’ previously defined here
 Ship::Ship() {
 ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:31:1: error: redefinition of ‘Ship::Ship(std::__cxx11::string, std::__cxx11::string)’
 Ship::Ship(string strname, string strYear) {
 ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:31:1: note: ‘Ship::Ship(std::__cxx11::string, std::__cxx11::string)’ previously defined here
 Ship::Ship(string strname, string strYear) {
 ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:39:6: error: redefinition of ‘void Ship::setName(std::__cxx11::string)’
 void Ship::setName(string strName) {
      ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:39:6: note: ‘void Ship::setName(std::__cxx11::string)’ previously defined here
 void Ship::setName(string strName) {
      ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:43:6: error: redefinition of ‘void Ship::setYear(std::__cxx11::string)’
 void Ship::setYear(string strYear) {
      ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:43:6: note: ‘void Ship::setYear(std::__cxx11::string)’ previously defined here
 void Ship::setYear(string strYear) {
      ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:47:8: error: redefinition of ‘std::__cxx11::string Ship::getName()’
 string Ship::getName() {
        ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:47:8: note: ‘std::__cxx11::string Ship::getName()’ previously defined here
 string Ship::getName() {
        ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:51:8: error: redefinition of ‘std::__cxx11::string Ship::getYear()’
 string Ship::getYear() {
        ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:51:8: note: ‘std::__cxx11::string Ship::getYear()’ previously defined here
 string Ship::getYear() {
        ^
In file included from CargoShip.cpp:3:0,
                 from Test.cpp:4:
ship.cpp:58:6: error: redefinition of ‘void Ship::print()’
 void Ship::print() {
      ^
In file included from CruiseShip.cpp:3:0,
                 from Test.cpp:3:
ship.cpp:58:6: note: ‘void Ship::print()’ previously defined here
 void Ship::print() {
      ^

这个错误对我影响不大,而且我似乎找不到解决方案。编译器似乎在告诉我,我正在尝试重新定义 Ship 类。谁能解释一下这是怎么回事?

最佳答案

您不应该从主文件中包含 .cpp 文件。想象#including .cpp 文件是复制和粘贴,并且您将 .cpp 文件包含在主文件和运送文件中 - 这会导致这些拷贝的多个拷贝,因此编译器会定义它不止一次。

您必须将类定义放在 .h 文件中 - WITH HEADER GUARDS 以防止重复,然后将实现细节放在 .cpp 文件中。示例:

船.hpp

// Protects from multiple copies
#ifndef _SHIP_H_
#define _SHIP_H_

#include <iostream>
#include <string>
using namespace std;

class Ship {
private:
    string _strName, _strYear;

public:
    //Constructors
    Ship();
    Ship(string, string);

    //Mutators
    void setName(string);
    void setYear(string);
    string getName();
    string getYear();

    //Methods
    virtual void print();
};

#endif // _SHIP_H_

船.cpp

#include "ship.hpp"    // this .cpp file INCLUDES the .hpp so it can match the functions

//*************************************
// Constructors                       *
//*************************************
Ship::Ship() {
    _strName = "NONE";
    _strYear = "NONE";
}
Ship::Ship(string strname, string strYear) {
    setName(strname);
    setYear(strYear);
}

//*************************************
// MUTATORS                           *
//*************************************
void Ship::setName(string strName) {
    _strName = strName;
}

void Ship::setYear(string strYear) {
    _strYear = strYear;
}

string Ship::getName() {
    return _strName;
}

string Ship::getYear() {
    return _strYear;
}

//*************************************
// METHODS                             *
//*************************************
virtual void Ship::print() {
    cout << _strName << endl
        << _strYear << endl;
}

对其他文件重复相同的操作。

主要.cpp

#include <iostream>
#include <string>
#include "CruiseShip.hpp"   // include .hpp files. Linker will link the .hpp functions with the .cpp implementations.
#include "CargoShip.hpp" // same here

int main() {
    CruiseShip cruise("cruise", "1862", 25);
    CargoShip cargo("Mellinium Falcon", "a long time ago", 100);

    cruise.print();
    cargo.print();

    return 0;
}

要构建,您必须单独编译所有 .cpp 文件以确保它们没有名称冲突。它们将自动与 main.cpp 链接:

g++ --std=c++0x ship.cpp cargo.cpp main.cpp ...等等等等

关于c++ - 用子类覆盖基类中定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52940337/

相关文章:

c++ - 从文本文件中排序数据

c++ - 如何合并两个 vector ?

c++ - 是定义代码 : vector<int> intVector will call default constructor and is same with vector<int> intVector()?

c++ - 在另一个文件中定义匿名类的方法

c++ - 扩展具有相同条件的多个 "if"会导致性能提升

c++ - 在没有事件异常的情况下终止调用(多线程合并排序)

C++ 继承 - QList

python - __init__() 缺少 1 个必需的位置参数 : 'quantity'

.net - 在 .Net 中,为什么调用 Type.GetCustomAttributes(true) 时不返回接口(interface)上声明的属性?

c# - 继承的逻辑错误和语法错误