完成列代码,写出执行结果 #include<iostream> using namespace std; class Cat { int itsAge; public: int getAge(); void setAge(int age); void meow(); //喵喵叫 }; 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(); return 0; }
时间: 2024-01-18 16:04:57 浏览: 82
代码如下:
```
#include<iostream>
using namespace std;
class Cat {
int itsAge;
public:
int getAge();
void setAge(int age);
void meow(); //喵喵叫
};
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();
return 0;
}
```
执行结果如下:
```
Meow.
frisky is a cat who is 5 years old.
Meow.
```
相关问题
使用c++语言,完成列代码,写出执行结果 #include<iostream> using namespace std; class Cat { int itsAge; public: int getAge(); void setAge(int age); void meow(); //喵喵叫 }; 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(); return 0; }
这是一个使用C++语言编写的程序,其功能是定义一个Cat类,包括私有成员itsAge和公有成员函数getAge()、setAge()以及meow()。
程序的输出结果为:
Meow.
frisky is a cat who is 5 years old.
Meow.
代码如下:
```c++
#include<iostream>
using namespace std;
class Cat {
int itsAge;
public:
int getAge();
void setAge(int age);
void meow(); //喵喵叫
};
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();
return 0;
}
```
用C++写代码:将下列程序分离类定义和主函数,改成多文件结构。主函数使用类的方式采取包含类定义的头文件的方法。写出运行结果。#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(); }
// Cat.h
#ifndef CAT_H
#define CAT_H
class Cat {
public:
int GetAge();
void SetAge(int age);
void Meow(); //喵喵叫
protected:
int itsAge;
};
#endif
// Cat.cpp
#include "Cat.h"
#include <iostream>
using namespace std;
int Cat::GetAge() {
return itsAge;
}
void Cat::SetAge(int age) {
itsAge = age;
}
void Cat::Meow() {
cout << "Meow.\n";
}
// main.cpp
#include "Cat.h"
#include <iostream>
using namespace std;
int main() {
Cat frisky;
frisky.SetAge(5);
frisky.Meow();
cout << "frisky is a cat who is " << frisky.GetAge() << " years old.\n";
frisky.Meow();
return 0;
}
// 运行结果
// Meow.
// frisky is a cat who is 5 years old.
// Meow.
阅读全文