我正在处理继承分配,并想首先测试我的基类及其 setter、getter 和构造函数,但我已经碰壁了。我的构造函数出了什么问题?我通常只使用无参数构造函数并使用 setter 和 getter 来构建对象,但我们被明确告知要使用这两种类型的构造函数。有人可以帮我吗?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
class Ship
{
public:
Ship();
Ship(string theName, int theYear);
string getName();
void setName(string);
int getYear();
void setYear(int);
void printInfo();
private:
string name;
int yearBuilt;
};
Ship:: Ship()
{
name = "";
yearBuilt = 0;
}
Ship:: Ship(string theName, int theYear)
{
name = theName;
yearBuilt = theYear;
}
string Ship::getName()
{
return name;
}
void Ship::setName(string newName)
{
name = newName;
} …Run Code Online (Sandbox Code Playgroud)