编写C++代码:定义一个类score, 它含有私有数据成员english(英语分数)、公有成员函数setscore ( )(和printscore( ),其中setscore( ) 可用带默认参数的方式来设置english的值,printscore( )用来输出english的值。在主程序中定义类score的两个对象stu1和stu2,其英语85.5和93.5,输出这两个分数。 实验目的讨论与分析
时间: 2024-03-25 08:39:33 浏览: 7
以下是C++代码实现:
```cpp
#include <iostream>
using namespace std;
class Score {
private:
float english;
public:
void setscore(float score = 0) {
english = score;
}
void printscore() {
cout << "English score is: " << english << endl;
}
};
int main() {
Score stu1, stu2;
stu1.setscore(85.5);
stu2.setscore(93.5);
stu1.printscore();
stu2.printscore();
return 0;
}
```
实验目的:通过实现一个简单的类,掌握类的基本概念、成员变量和成员函数的定义,以及对象的创建和使用。同时,还可以了解到C++中的默认参数的使用方法。
相关问题
编写代码:定义一个类score, 它含有私有数据成员english(英语分数)、公有成员函数setscore ( )(和printscore( ),其中setscore( ) 可用带默认参数的方式来设置english的值,printscore( )用来输出english的值。在
C++中,可以这样定义一个score类:
```c++
class score {
private:
int english;
public:
void setscore(int score = 0) {
english = score;
}
void printscore() {
std::cout << "English score: " << english << std::endl;
}
};
```
这个类有一个私有数据成员`english`,表示英语分数。它有一个公有成员函数`setscore`,可以用带默认参数的方式来设置`english`的值;还有一个公有成员函数`printscore`,用来输出`english`的值。
使用方法示例:
```c++
score s;
s.setscore(85);
s.printscore(); // English score: 85
score s2;
s2.printscore(); // English score: 0
```
编写C++代码:定义一个类score, 它含有私有数据成员english(英语分数)、公有成员函数setscore ( )(和printscore( ),其中setscore( ) 可用带默认参数的方式来设置english的值,printscore( )用来输出english的值。在主程序中定义类score的两个对象stu1和stu2,其英语85.5和93.5,输出这两个分数。 实验目的和具体步骤
下面是实现题目要求的C++代码:
```cpp
#include <iostream>
using namespace std;
class Score {
private:
double english;
public:
void setscore(double score = 0.0) {
english = score;
}
void printscore() {
cout << "English score: " << english << endl;
}
};
int main() {
Score stu1, stu2;
stu1.setscore(85.5);
stu2.setscore(93.5);
stu1.printscore();
stu2.printscore();
return 0;
}
```
实验目的:
1. 熟悉C++类的定义和使用;
2. 熟练掌握类的访问控制;
3. 掌握类的成员函数定义和调用。
具体步骤:
1. 定义类Score,将英语分数作为私有数据成员;
2. 定义公有成员函数setscore和printscore,分别用于设置和输出英语分数;
3. 在main函数中创建两个Score对象stu1和stu2;
4. 分别调用对象的setscore函数设置英语分数;
5. 分别调用对象的printscore函数输出英语分数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)