C++实现电影管理时,电影类的源文件怎么写
时间: 2024-04-08 07:34:29 浏览: 15
以下是电影类的源文件示例:
```cpp
#include "Movie.h"
// 默认构造函数
Movie::Movie() {
title = "";
director = "";
year = 0;
rating = 0.0;
}
// 带参数的构造函数
Movie::Movie(const std::string& title, const std::string& director, int year, float rating) {
this->title = title;
this->director = director;
this->year = year;
this->rating = rating;
}
// 获取电影标题
std::string Movie::getTitle() const {
return title;
}
// 设置电影标题
void Movie::setTitle(const std::string& title) {
this->title = title;
}
// 获取导演
std::string Movie::getDirector() const {
return director;
}
// 设置导演
void Movie::setDirector(const std::string& director) {
this->director = director;
}
// 获取上映年份
int Movie::getYear() const {
return year;
}
// 设置上映年份
void Movie::setYear(int year) {
this->year = year;
}
// 获取评分
float Movie::getRating() const {
return rating;
}
// 设置评分
void Movie::setRating(float rating) {
this->rating = rating;
}
```
在源文件中,我们实现了电影类的构造函数和成员函数。构造函数用于初始化电影对象的属性,成员函数用于获取和设置电影的各个属性。
需要注意的是,源文件中包含了 `"Movie.h"` 头文件,以便在实现类的成员函数时能够访问类的声明。确保在编译时能够正确包含相关的头文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)