鱼C论坛

 找回密码
 立即注册
查看: 1547|回复: 3

[已解决]c++问题

[复制链接]
发表于 2021-4-16 14:11:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
构建一个学生类Student,
含有字符串name,整数成员age及双精度score,
构造函数含三个默认参数初始化学生对象(xiaoming,18,88.65)。
成员函数print没有形参,需使用this指针,显示对象数据成员的内容。
要求编写程序声明两个对象s1、s2,
其中s2使用默认参数进行初始化,并显示学生对象数据成员的值。




求大神指教!
最佳答案
2021-4-16 14:58:42
  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. class Student
  5. {
  6. public:
  7.     Student(string name = "xiaoming", unsigned int age = 18, double score = 88.65)
  8.         : name(name), age(age), score(score) {}

  9.     void print()
  10.     {
  11.         printf_s(" name:\t%s\n age:\t%d\n score:\t%.2lf\n", this->name.c_str(), this->age, this->score);
  12.     }

  13. private:
  14.     string          name;
  15.     unsigned int    age;
  16.     double          score;
  17. };

  18. int main()
  19. {
  20.     Student s1("s1", 20, 99.99);
  21.     Student s2;


  22.     s2.print();

  23.     system("pause");
  24.     return 0;
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-16 14:58:42 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. class Student
  5. {
  6. public:
  7.     Student(string name = "xiaoming", unsigned int age = 18, double score = 88.65)
  8.         : name(name), age(age), score(score) {}

  9.     void print()
  10.     {
  11.         printf_s(" name:\t%s\n age:\t%d\n score:\t%.2lf\n", this->name.c_str(), this->age, this->score);
  12.     }

  13. private:
  14.     string          name;
  15.     unsigned int    age;
  16.     double          score;
  17. };

  18. int main()
  19. {
  20.     Student s1("s1", 20, 99.99);
  21.     Student s2;


  22.     s2.print();

  23.     system("pause");
  24.     return 0;
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-16 15:33:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-16 15:45:38 | 显示全部楼层

什么系统?  什么编译器?  报错是什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 07:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表