永发信息网

帮我解决一道C++编程题

答案:3  悬赏:0  手机版
解决时间 2021-05-01 17:36
  • 提问者网友:未信
  • 2021-04-30 23:22
设计一个表示猫的类,包括猫的颜色、体重、年龄等数据,具有设置猫的颜色,修改和显示猫的体重、年龄等操作。(用C++编写)
最佳答案
  • 五星知识达人网友:老鼠爱大米
  • 2021-04-30 23:45
#include<iostream>
#include<string>
using namespace std;
class ccat{
private:
string color;
float weight;
int age;
public:
ccat(){
color="white";
weight=2.5;
age=1;
}
ccat(string c,float w,int a){
color=c;
weight=w;
age=a;
}
void set_color(string c){
color=c;
}
void set_weight(float w){
weight=w;
}
void set_age(int a){
age=a;
}
void show()
{
cout<<"color:"<<color<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"age:"<<age<<endl;
}
};
int main()
{
ccat cat;
cat.show();
return 0;
}
全部回答
  • 1楼网友:痴妹与他
  • 2021-05-01 01:28

#include<iostream> #include<string> using namespace std; class Cat { private: string color;//颜色 float weight;//体重 int age;//年龄 public: Cat()//无参析构函数 { color="white"; weight=2.5; age=1; } Cat(string c,float w,int a)//带参析构函数 { color=c; weight=w; age=a; } void SetColor(string c)//设置颜色函数 { color=c; } void SetWeight(float w)//设置体重函数 { weight=w; } void SetAge(int a)//设置年龄函数 { age=a; } void show() { cout<<"The cat's color:"<<color<<endl; cout<<"The cat's weight:"<<weight<<endl; cout<<"The cat's age:"<<age<<endl; } }; void main() { Cat cat1; cat1.show(); Cat cat2("red",20,5); cat2.show(); }

结果如下:

  • 2楼网友:夜风逐马
  • 2021-05-01 00:00
#include <iostream> #include <string> using namespace std; class Cat{ public: void setcolor() { cout<<"input the color of the cat:"; cin>>color; cout<<endl; } void set(int wei,int ag) { weight=wei; age=ag; } void change(int wei,int ag) { wei=weight; age=ag; } void display() { cout<<"The color of the cat is "<<color<<endl; cout<<"The weight of the cat is "<<weight<<" kg."<<endl; cout<<"The age of the cat is "<<age<<" years old."<<endl; } private: string color; int weight; int age; }; int main() { Cat cat1; cat1.setcolor(); cat1.set(2,5); cat1.display(); cat1.change(5,3); cat1.display(); return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯