喵喵叫
总时间限制: 100000ms 内存限制: 100000kB
描述
1.定义一个cat类,
设置猫咪年龄:函数名为SetAge;
获取猫咪年龄:函数名为GetAge;
输出喵喵叫:函数名为Meow,输出“Meow”
2.在主函数实现
定义猫咪对象frisky
设置猫咪年龄为5
输出喵喵叫
输出猫咪年龄
输出喵喵叫
输入
NULL
输出
Meow.
frisky is a cat who is 5 years old.
Meow.
样例输入
NULL
样例输出
Meow.
frisky is a cat who is 5 years old.
Meow.
完美AC:
#include<iostream>
using namespace std;
class cat
{
public:
int GetAge();
void SetAge(int age);
void Meow();
protected:
int itsAge;
};
int cat::GetAge()
{
return itsAge;
}
void cat::SetAge(int age)
{
itsAge=age;
}
void cat::Meow()
{
cout<<"Meow.\n";
}
int main()
{
cat frisky;
frisky.SetAge(5);
frisky.Meow();
cout<<"frisky is a cat who is "<<frisky.GetAge()<<" years old.\n";
frisky.Meow();
}
不管题意的AC(主要是懒):
#include<iostream>
using namespace std;
class cat {
private:
int year;
public:
cat(int year) {//此处一定要叫cat,但int后面的变量名可以取别的
cat::year=year;//若上一行为int a,这里改成cat::year=a;
//其他地方不用改
}
void print1() {
cout<<"Meow."<<endl;
}
void print2() {
cout<<"frisky is a cat who is "<<year<<" years old."<<endl;
}
};
int main() {
int year=5;
cat c(year);
c.print1();
c.print2();
c.print1();
return 0;
}