鱼C论坛

 找回密码
 立即注册
查看: 1365|回复: 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
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
    Student(string name = "xiaoming", unsigned int age = 18, double score = 88.65)
        : name(name), age(age), score(score) {}

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

private:
    string          name;
    unsigned int    age;
    double          score;
};

int main()
{
    Student s1("s1", 20, 99.99);
    Student s2;


    s2.print();

    system("pause");
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

using namespace std;

class Student
{
public:
    Student(string name = "xiaoming", unsigned int age = 18, double score = 88.65)
        : name(name), age(age), score(score) {}

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

private:
    string          name;
    unsigned int    age;
    double          score;
};

int main()
{
    Student s1("s1", 20, 99.99);
    Student s2;


    s2.print();

    system("pause");
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-16 15:33:03 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

什么系统?  什么编译器?  报错是什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 17:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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