当前位置:   article > 正文

面向对象程序设计第二周作业

面向对象程序设计第二周作业

1. 

  1. //建立一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别、年龄。
  2. //还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。
  3. //编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕输出学生信息。
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. class Student {
  8. private:
  9. string name;
  10. int studentID;
  11. char gender;
  12. int age;
  13. public:
  14. // 构造函数
  15. Student(string name, int studentID, char gender, int age) {
  16. this->name = name;
  17. this->studentID = studentID;
  18. this->gender = gender;
  19. this->age = age;
  20. }
  21. // 成员函数
  22. void printInfo() {
  23. cout << "姓名: " << name << endl;
  24. cout << "学号: " << studentID << endl;
  25. cout << "性别: " << gender << endl;
  26. cout << "年龄: " << age << endl;
  27. }
  28. };
  29. int main() {
  30. // 声明一个学生对象
  31. Student student("张三", 1001, '男', 18);
  32. // 调用成员函数输出学生信息
  33. student.printInfo();
  34. return 0;
  35. }

2.

  1. //类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定)。
  2. /*
  3. class Person
  4. { private:
  5. char name[10];
  6. int age;
  7. int salary;
  8. char tel[8];
  9. public:
  10. Person(char *xname,int xage,int xsalary,char *xtel);
  11. void disp();
  12. };
  13. */
  14. #include <iostream>
  15. #include <cstring>
  16. class Person
  17. {
  18. private:
  19. char name[10];
  20. int age;
  21. int salary;
  22. char tel[8];
  23. public:
  24. Person(char* xname, int xage, int xsalary, char* xtel)
  25. {
  26. strcpy(name, xname);
  27. age = xage;
  28. salary = xsalary;
  29. strcpy(tel, xtel);
  30. }
  31. void disp()
  32. {
  33. std::cout << "Name: " << name << std::endl;
  34. std::cout << "Age: " << age << std::endl;
  35. std::cout << "Salary: " << salary << std::endl;
  36. std::cout << "Tel: " << tel << std::endl;
  37. }
  38. };
  39. int main()
  40. {
  41. // 创建对象obj并使用构造函数为其赋予初始值
  42. Person obj("John Doe", 30, 5000, "1234567");
  43. // 调用disp函数显示obj的信息
  44. obj.disp();
  45. return 0;
  46. }

3.

  1. //建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,
  2. //用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。按要求编程实现。
  3. #include <iostream>
  4. class Student
  5. {
  6. private:
  7. int id;
  8. double score;
  9. public:
  10. Student(int xid, double xscore)
  11. {
  12. id = xid;
  13. score = xscore;
  14. }
  15. double getScore() {
  16. return score;
  17. }
  18. int getId() {
  19. return id;
  20. }
  21. };
  22. int max(Student* students, int size)
  23. {
  24. int maxIndex = 0;
  25. double maxScore = students[0].getScore();
  26. for (int i = 1; i < size; i++)
  27. {
  28. if (students[i].getScore() > maxScore)
  29. {
  30. maxScore = students[i].getScore();
  31. maxIndex = i;
  32. }
  33. }
  34. return students[maxIndex].getId();
  35. }
  36. int main()
  37. {
  38. // 创建对象数组,存放5个学生的数据
  39. Student students[5] = {
  40. Student(1001, 89.5),
  41. Student(1002, 78.2),
  42. Student(1003, 95.0),
  43. Student(1004, 82.7),
  44. Student(1005, 91.3)
  45. };
  46. // 调用max函数找出成绩最高的学生,并输出其学号
  47. int maxId = max(students, 5);
  48. std::cout << "学号最高分的学生学号: " << maxId << std::endl;
  49. return 0;
  50. }

4

  1. //设计一个描述点的类,其中包含一对坐标点数据成员、一个求两个点之间距离的友元函数和显示坐标点的成员函数,并编程实现(数据自定义)。
  2. //提示:需要使用计算两点(a, b)和(c, d)之间的距离公式,并定义坐标点类的友元函数来计算两点间的距离。
  3. #include <iostream>
  4. #include <cmath>
  5. class Point
  6. {
  7. private:
  8. double x;
  9. double y;
  10. public:
  11. Point(double xcoord, double ycoord)
  12. {
  13. x = xcoord;
  14. y = ycoord;
  15. }
  16. friend double distance(const Point& a, const Point& b);
  17. void display()
  18. {
  19. std::cout << "Point coordinates: (" << x << ", " << y << ")" << std::endl;
  20. }
  21. };
  22. double distance(const Point& a, const Point& b)
  23. {
  24. double dx = a.x - b.x;
  25. double dy = a.y - b.y;
  26. return std::sqrt(dx * dx + dy * dy);
  27. }
  28. int main()
  29. {
  30. Point p1(3.2, 4.5);
  31. Point p2(6.8, 9.1);
  32. p1.display();
  33. p2.display();
  34. double dist = distance(p1, p2);
  35. std::cout << "Distance between the points: " << dist << std::endl;
  36. return 0;
  37. }

5.

  1. //运用友元关系定义一个学生的类和一个成绩的类,其中使得成绩类对象可以访问学生对象的私有数据,并编程实现(数据自定义)。
  2. //提示:为使成绩对象可以访问学生对象的私有数据,在定义学生类时,将成绩类声明为他的友元类。
  3. #include <iostream>
  4. #include <string>
  5. class Score; // 前置声明
  6. class Student
  7. {
  8. private:
  9. std::string name;
  10. int age;
  11. public:
  12. Student(const std::string& studentName, int studentAge)
  13. {
  14. name = studentName;
  15. age = studentAge;
  16. }
  17. friend class Score; // 将成绩类声明为学生类的友元类
  18. };
  19. class Score
  20. {
  21. private:
  22. int score;
  23. public:
  24. void setScore(Student& student, int studentScore)
  25. {
  26. // 在成绩类中可以访问学生类的私有数据
  27. student.age += 1;
  28. score = studentScore;
  29. std::cout << "Student " << student.name << " Age: " << student.age << std::endl;
  30. std::cout << "Score: " << score << std::endl;
  31. }
  32. };
  33. int main()
  34. {
  35. Student student("Alice", 18);
  36. Score score;
  37. score.setScore(student, 90);
  38. return 0;
  39. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号