用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(); }
时间: 2023-06-25 07:02:55 浏览: 91
// 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.
相关问题
c++头文件定义模板类和模板函数,cpp文件实现函数功能
在C++中,模板类和模板函数的定义和实现通常分离在不同的文件中。
首先,定义模板类或者模板函数的头文件中要包含模板参数列表和对应的成员或函数声明。例如,定义一个模板类`MyClass`:
```c++
// MyClass.h
template <typename T>
class MyClass {
public:
MyClass(T value);
void printValue();
private:
T m_value;
};
template <typename T>
MyClass<T>::MyClass(T value) : m_value(value) {}
template <typename T>
void MyClass<T>::printValue() {
std::cout << "Value: " << m_value << std::endl;
}
```
然后,在定义好的头文件中,在类或函数声明后面加上函数实现的代码。例如,在实现`MyClass`的构造函数和`printValue`函数:
```c++
// MyClass.cpp
#include "MyClass.h"
template <typename T>
MyClass<T>::MyClass(T value) : m_value(value) {}
template <typename T>
void MyClass<T>::printValue() {
std::cout << "Value: " << m_value << std::endl;
}
```
最后,在使用模板类或模板函数的源文件中,包含定义好的头文件即可。例如,在`main.cpp`中使用`MyClass`:
```c++
// main.cpp
#include "MyClass.h"
int main() {
MyClass<int> myobj(10);
myobj.printValue();
return 0;
}
```
注意,模板类和模板函数的实现必须在包含模板参数列表的同一文件中,否则编译器无法实例化模板。另外,模板函数的实现也可以放在头文件中,这样可以避免多个文件中的重复代码。
用C++写代码:定义一个时间类 Time,能提供和设置由时、分、秒组成的时间,并编写应用程序,定义 时间对象,设置时间,输出该对象提供的时间。并请将类定义作为接口,用多文件结构实现之。
下面是Time类的头文件Time.h的定义:
```c++
#ifndef TIME_H
#define TIME_H
class Time {
public:
Time(int h, int m, int s); // 构造函数
void setTime(int h, int m, int s); // 设置时间
int getHour() const; // 获取小时
int getMinute() const; // 获取分钟
int getSecond() const; // 获取秒钟
void print() const; // 输出时间
private:
int hour; // 小时
int minute; // 分钟
int second; // 秒钟
};
#endif
```
下面是Time类的实现文件Time.cpp:
```c++
#include <iostream>
#include "Time.h"
Time::Time(int h, int m, int s) {
setTime(h, m, s);
}
void Time::setTime(int h, int m, int s) {
hour = (h >= 0 && h < 24) ? h : 0;
minute = (m >= 0 && m < 60) ? m : 0;
second = (s >= 0 && s < 60) ? s : 0;
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}
void Time::print() const {
std::cout << hour << ":" << minute << ":" << second << std::endl;
}
```
下面是应用程序的主函数文件main.cpp:
```c++
#include "Time.h"
int main() {
Time t(10, 30, 0);
t.print();
t.setTime(11, 45, 0);
t.print();
return 0;
}
```
编译上述代码需要将Time.h和Time.cpp文件一起编译:
```bash
g++ -o main main.cpp Time.cpp
```
运行程序:
```bash
./main
```
输出结果:
```
10:30:0
11:45:0
```