赞
踩
1.
- //建立一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别、年龄。
- //还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。
- //编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕输出学生信息。
- #include <iostream>
- #include <string>
-
- using namespace std;
-
- class Student {
- private:
- string name;
- int studentID;
- char gender;
- int age;
-
- public:
- // 构造函数
- Student(string name, int studentID, char gender, int age) {
- this->name = name;
- this->studentID = studentID;
- this->gender = gender;
- this->age = age;
- }
-
- // 成员函数
- void printInfo() {
- cout << "姓名: " << name << endl;
- cout << "学号: " << studentID << endl;
- cout << "性别: " << gender << endl;
- cout << "年龄: " << age << endl;
- }
- };
-
- int main() {
- // 声明一个学生对象
- Student student("张三", 1001, '男', 18);
-
- // 调用成员函数输出学生信息
- student.printInfo();
-
- return 0;
- }
-

2.
- //类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定)。
- /*
- class Person
- { private:
- char name[10];
- int age;
- int salary;
- char tel[8];
- public:
- Person(char *xname,int xage,int xsalary,char *xtel);
- void disp();
- };
- */
- #include <iostream>
- #include <cstring>
-
- class Person
- {
- private:
- char name[10];
- int age;
- int salary;
- char tel[8];
-
- public:
- Person(char* xname, int xage, int xsalary, char* xtel)
- {
- strcpy(name, xname);
- age = xage;
- salary = xsalary;
- strcpy(tel, xtel);
- }
-
- void disp()
- {
- std::cout << "Name: " << name << std::endl;
- std::cout << "Age: " << age << std::endl;
- std::cout << "Salary: " << salary << std::endl;
- std::cout << "Tel: " << tel << std::endl;
- }
- };
-
- int main()
- {
- // 创建对象obj并使用构造函数为其赋予初始值
- Person obj("John Doe", 30, 5000, "1234567");
-
- // 调用disp函数显示obj的信息
- obj.disp();
-
- return 0;
- }

3.
- //建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,
- //用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。按要求编程实现。
- #include <iostream>
-
- class Student
- {
- private:
- int id;
- double score;
-
- public:
- Student(int xid, double xscore)
- {
- id = xid;
- score = xscore;
- }
-
- double getScore() {
- return score;
- }
-
- int getId() {
- return id;
- }
- };
-
- int max(Student* students, int size)
- {
- int maxIndex = 0;
- double maxScore = students[0].getScore();
-
- for (int i = 1; i < size; i++)
- {
- if (students[i].getScore() > maxScore)
- {
- maxScore = students[i].getScore();
- maxIndex = i;
- }
- }
-
- return students[maxIndex].getId();
- }
-
- int main()
- {
- // 创建对象数组,存放5个学生的数据
- Student students[5] = {
- Student(1001, 89.5),
- Student(1002, 78.2),
- Student(1003, 95.0),
- Student(1004, 82.7),
- Student(1005, 91.3)
- };
-
- // 调用max函数找出成绩最高的学生,并输出其学号
- int maxId = max(students, 5);
- std::cout << "学号最高分的学生学号: " << maxId << std::endl;
-
- return 0;
- }

4
- //设计一个描述点的类,其中包含一对坐标点数据成员、一个求两个点之间距离的友元函数和显示坐标点的成员函数,并编程实现(数据自定义)。
- //提示:需要使用计算两点(a, b)和(c, d)之间的距离公式,并定义坐标点类的友元函数来计算两点间的距离。
- #include <iostream>
- #include <cmath>
-
- class Point
- {
- private:
- double x;
- double y;
-
- public:
- Point(double xcoord, double ycoord)
- {
- x = xcoord;
- y = ycoord;
- }
-
- friend double distance(const Point& a, const Point& b);
-
- void display()
- {
- std::cout << "Point coordinates: (" << x << ", " << y << ")" << std::endl;
- }
- };
-
- double distance(const Point& a, const Point& b)
- {
- double dx = a.x - b.x;
- double dy = a.y - b.y;
- return std::sqrt(dx * dx + dy * dy);
- }
-
- int main()
- {
- Point p1(3.2, 4.5);
- Point p2(6.8, 9.1);
-
- p1.display();
- p2.display();
-
- double dist = distance(p1, p2);
- std::cout << "Distance between the points: " << dist << std::endl;
-
- return 0;
- }

5.
- //运用友元关系定义一个学生的类和一个成绩的类,其中使得成绩类对象可以访问学生对象的私有数据,并编程实现(数据自定义)。
- //提示:为使成绩对象可以访问学生对象的私有数据,在定义学生类时,将成绩类声明为他的友元类。
- #include <iostream>
- #include <string>
-
- class Score; // 前置声明
-
- class Student
- {
- private:
- std::string name;
- int age;
-
- public:
- Student(const std::string& studentName, int studentAge)
- {
- name = studentName;
- age = studentAge;
- }
-
- friend class Score; // 将成绩类声明为学生类的友元类
- };
-
- class Score
- {
- private:
- int score;
-
- public:
- void setScore(Student& student, int studentScore)
- {
- // 在成绩类中可以访问学生类的私有数据
- student.age += 1;
- score = studentScore;
- std::cout << "Student " << student.name << " Age: " << student.age << std::endl;
- std::cout << "Score: " << score << std::endl;
- }
- };
-
- int main()
- {
- Student student("Alice", 18);
- Score score;
-
- score.setScore(student, 90);
-
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。