1.定义一个cat类,
设置猫咪年龄:函数名为SetAge;
获取猫咪年龄:函数名为GetAge;
输出喵喵叫:函数名为Meow,输出“Meow”
2.在主函数实现
定义猫咪对象frisky
设置猫咪年龄为5
输出喵喵叫
输出猫咪年龄
输出喵喵叫
#include<iostream>
using namespace std;
class cat{
int age;
public:
void setage(int a){
age=a;
}
void getage(){
return age;
}
void mewo(){
cout<<"Mewo"<<endl;
}
};
int main(){
cat frisky;
frisky.mewo();
frisky.setage(5);
frisky.getage();
cout<<"frisky is a cat who is "<<frisky.age<<"years old.";
frisky.mewo();
}
#include <iostream>
using namespace std;
class cat
{
int _age = 0;
public:
void SetAge(int age)
{
_age = age;
}
int GetAge() const
{
return _age;
}
void Meow() const
{
cout << "Meow" << endl;
}
};
int main()
{
cat frisky;
frisky.Meow();
frisky.SetAge(5);
cout << "frisky is a cat who is " << frisky.GetAge() << " years old." << endl;
frisky.Meow();
return 0;
}
getage是有int型返回值的
所以不能用void,应该是
int getage(){
return age;
}