C++ 将变量中的数字添加到对象的属性 #包括 #包括 使用名称空间std; 类航天器 { 私人: 整数大小; 公众: int setSize(int new_size); int getSize(); int descripe(); 航天器(国际尺寸); };
将变量中的数字添加到对象的属性C++ 将变量中的数字添加到对象的属性 #包括 #包括 使用名称空间std; 类航天器 { 私人: 整数大小; 公众: int setSize(int new_size); int getSize(); int descripe(); 航天器(国际尺寸); }; ,c++,class,C++,Class,将变量中的数字添加到对象的属性 #包括 #包括 使用名称空间std; 类航天器 { 私人: 整数大小; 公众: int setSize(int new_size); int getSize(); int descripe(); 航天器(国际尺寸); }; int航天器::设置大小(int新尺寸) { 尺寸=新尺寸; 返回0; } 航天器::航天器(新尺寸) { 尺寸=新尺寸; } int::getSize() { 返回大小; } int::descripe() { 你可以写下: #include
#包括
#包括
使用名称空间std;
类航天器
{
私人:
整数大小;
公众:
int setSize(int new_size);
int getSize();
int descripe();
航天器(国际尺寸);
};
int航天器::设置大小(int新尺寸)
{
尺寸=新尺寸;
返回0;
}
航天器::航天器(新尺寸)
{
尺寸=新尺寸;
}
int::getSize()
{
返回大小;
}
int::descripe()
{
你可以写下:
#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;
}
cout试一试:cout我正在做一个练习,它想让我使用descripe函数来输出用户的输入,所以我不得不这样做。如果我们在做有用的代码片段:If(std::cin>>size){…}否则{std::cout我知道,在你的descripe函数中试试。@user2746748你误解了凯文说的话。
cout <<"Your spacecraft is size " << size);