将变量中的数字添加到对象的属性中

Adding number from variable to the property of an object

本文关键字:对象 属性 数字 变量 添加      更新时间:2023-10-16
#include <cstring>
#include <iostream>
using namespace std;
class Spacecraft
{
private:
    int size;
public:
    int setSize( int new_size );
    int getSize();
    int describe();
    Spacecraft (int size);
};
int Spacecraft::setSize( int new_size)
{
    size = new_size;
    return 0;
}
Spacecraft::Spacecraft (int new_size)
{
    size = new_size;
}
int Spacecraft::getSize()
{
    return size;
}
int Spacecraft::describe()
{
    cout <<"Your spacecraft is size " (adding Customcraft.getSize() here);
    return 0;
}

int main() {
    int size;
    cout <<"Type in the size of your spacecraft: ";
    cin >>size;
        cout <<endl;
    Spacecraft Customcraft(the value of size to here);
    Customcraft.describe();


    return 0;
}

我正在尝试将用户输入放入航天器客户(大小(中。这不起作用,所以我想知道如何将大小值添加到其中,以便计算机不会查看单词而是整数值。

是否可以将 Customcraft.getSize(( 链接到 describe(( 函数?当我尝试定制时,无法识别。我是这门课的新手,所以请尽量将解释保持在初学者的水平。

编辑:我还有最后一个问题,谢谢你解决我的第一个问题。在学校,当我把尺寸放入航天器定制飞船(size(时,日食无法识别变量的整数值。那么可能是因为日食(靛蓝(而不是开普勒吗?

你可以写:

cout <<"Your spacecraft is size " << size);