class People { public: string name;//姓名 int age;//年龄 string id;//学号 People() {} People(string id, string name, int age ) { setId(id); setName(name); setAge(age); } //set void setId(string id) { this->id = id; } void setName(string name) { this->name = name; } void setAge(int age) { if (age >= 0 && age <= 100) { this->age = age; } } //get string getId() { return id; } string getName() { return name; } int getAge() { return age; } }; class Student: public People { public: double score;//成绩 Student() { } Student(string id, string name, int age, double score) :People(id, name, age) { setScore(score); } double getScore() { return score; } //set void setScore(double score) { if (score >= 0 && score <= 100) { this->score = score; } } };
时间: 2024-02-26 09:53:15 浏览: 18
这段代码定义了两个类:People 和 Student。People 类包含了姓名、年龄和学号等属性以及相应的 set 和 get 方法。Student 类继承了 People 类,并添加了一个成绩属性,以及相应的 set 和 get 方法。
在 People 类中,setId、setName 和 setAge 方法用于设置学号、姓名和年龄,getId、getName 和 getAge 方法用于获取学号、姓名和年龄的值。
在 Student 类中,setScore 和 getScore 方法用于设置和获取成绩属性的值。
需要注意的是,成员变量和方法的访问权限使用了 public 关键字,表示可以被其他类访问。同时,类的构造函数也被定义了,用于初始化类的属性。
相关问题
C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等
1. 定义people类,包含以下成员变量和成员函数:
```cpp
class people {
private:
string name;
int age;
char gender;
string phone;
public:
people(string n, int a, char g, string p);
void display();
};
```
其中,构造函数people(string n, int a, char g, string p)用于初始化成员变量;display()函数用于显示人员信息。
2. 从people类中派生出student类,包含以下成员变量和成员函数:
```cpp
class student : public people {
private:
string id;
string major;
public:
student(string n, int a, char g, string p, string i, string m);
void display();
};
```
其中,构造函数student(string n, int a, char g, string p, string i, string m)用于初始化成员变量;display()函数用于显示学生信息。
3. 实现people类和student类的成员函数:
```cpp
people::people(string n, int a, char g, string p) {
name = n;
age = a;
gender = g;
phone = p;
}
void people::display() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << gender << endl;
cout << "电话:" << phone << endl;
}
student::student(string n, int a, char g, string p, string i, string m) : people(n, a, g, p) {
id = i;
major = m;
}
void student::display() {
people::display();
cout << "学号:" << id << endl;
cout << "专业:" << major << endl;
}
```
4. 编写测试代码,验证people类和student类的功能:
```cpp
int main() {
people p("张三", 20, 'M', "13888888888");
p.display();
student s("李四", 18, 'F', "13999999999", "2021000001", "计算机科学与技术");
s.display();
return 0;
}
```
输出结果如下:
```
姓名:张三
年龄:20
性别:M
电话:13888888888
姓名:李四
年龄:18
性别:F
电话:13999999999
学号:2021000001
专业:计算机科学与技术
```
类的设计:定义一个类People,定义学生信息的类Student(为People的子类),定义Test类(主类) 要求: ① 成员变量: People类:sName表示姓名,sSex表示性别,sAge表示年龄 Student类:sNo表示学号,sScore1表示课程1成绩,sScore2表示课程2成绩,sScore3表示课程3成绩;sSum1,sSum2,sSum3,分别表示所有学生的3门总成绩和,sNum表示学生数。;sSum1,sSum2,sSum3 和 sNum 设为类成员变量。 ② People类和Student类定义一个带参数的构造方法对各成员变量初始化,并对sSum和sNum进行运算。 ③ People类和Student类给成员变量定义对应的get方法,获取各个成员变量的值。 ④ Student类定义类方法 show方法,显示学生所有信息。 ⑤ Student类定义类方法 average方法,求学生平均成绩。 ⑥ Student类定义类方法 averagei方法,求所有学生第i门课程的平均成绩。 ⑦ 在Test类的main方法中创建5个Student类的对象数组,输出每个学生的信息。输出5个学生的3门课程的平均成绩。
以下是类的代码实现:
```java
// People类
public class People {
private String sName;
private String sSex;
private int sAge;
public People(String name, String sex, int age) {
this.sName = name;
this.sSex = sex;
this.sAge = age;
}
public String getName() {
return sName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
}
// Student类
public class Student extends People {
private String sNo;
private int sScore1;
private int sScore2;
private int sScore3;
private static int sSum1 = 0;
private static int sSum2 = 0;
private static int sSum3 = 0;
private static int sNum = 0;
public Student(String name, String sex, int age, String no, int score1, int score2, int score3) {
super(name, sex, age);
this.sNo = no;
this.sScore1 = score1;
this.sScore2 = score2;
this.sScore3 = score3;
sSum1 += score1;
sSum2 += score2;
sSum3 += score3;
sNum++;
}
public String getNo() {
return sNo;
}
public int getScore1() {
return sScore1;
}
public int getScore2() {
return sScore2;
}
public int getScore3() {
return sScore3;
}
public static void show(Student student) {
System.out.println("姓名:" + student.getName() + ",性别:" + student.getSex() + ",年龄:" + student.getAge() +
",学号:" + student.getNo() + ",成绩1:" + student.getScore1() + ",成绩2:" + student.getScore2() +
",成绩3:" + student.getScore3());
}
public static double average(Student[] students) {
double sum = 0;
for (Student student : students) {
sum += student.getScore1() + student.getScore2() + student.getScore3();
}
return sum / (3 * sNum);
}
public static double averagei(Student[] students, int i) {
double sum = 0;
for (Student student : students) {
if (i == 1) {
sum += student.getScore1();
} else if (i == 2) {
sum += student.getScore2();
} else if (i == 3) {
sum += student.getScore3();
}
}
return sum / sNum;
}
}
// Test类
public class Test {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student("张三", "男", 18, "20191001", 80, 90, 70);
students[1] = new Student("李四", "女", 19, "20191002", 85, 95, 75);
students[2] = new Student("王五", "男", 20, "20191003", 90, 80, 85);
students[3] = new Student("赵六", "女", 21, "20191004", 75, 85, 90);
students[4] = new Student("钱七", "男", 22, "20191005", 85, 95, 80);
for (Student student : students) {
Student.show(student);
}
System.out.println("平均成绩:" + Student.average(students));
System.out.println("第1门课程平均成绩:" + Student.averagei(students, 1));
System.out.println("第2门课程平均成绩:" + Student.averagei(students, 2));
System.out.println("第3门课程平均成绩:" + Student.averagei(students, 3));
}
}
```
运行结果如下:
```
姓名:张三,性别:男,年龄:18,学号:20191001,成绩1:80,成绩2:90,成绩3:70
姓名:李四,性别:女,年龄:19,学号:20191002,成绩1:85,成绩2:95,成绩3:75
姓名:王五,性别:男,年龄:20,学号:20191003,成绩1:90,成绩2:80,成绩3:85
姓名:赵六,性别:女,年龄:21,学号:20191004,成绩1:75,成绩2:85,成绩3:90
姓名:钱七,性别:男,年龄:22,学号:20191005,成绩1:85,成绩2:95,成绩3:80
平均成绩:83.0
第1门课程平均成绩:83.0
第2门课程平均成绩:89.0
第3门课程平均成绩:80.0
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)