c++ / day02

1. 把课上strcut的练习,尝试着改成class

代码

#include <iostream>

using namespace std;

class Stu
{
private:
    int age;
    char gender;
    int height;

public:
    float score;

    int get_age();
    char get_gender();
    int get_height();

    void set_age(int age);
    void set_gender(char gender);
    void set_height(int height);

};

int Stu::get_age()
{
    return age;
}

char Stu::get_gender()
{
    return gender;
}

int Stu::get_height()
{
    return height;
}

void Stu::set_age(int age)
{
    Stu::age = age;
}

void Stu::set_gender(char gender)
{
    Stu::gender = gender;
}

void Stu::set_height(int height)
{
    Stu::height = height;
}


int main()
{
    Stu s1;
    s1.set_age(18);
    s1.set_gender('f');
    s1.set_height(165);
    s1.score = 99.0;
    cout << "s1.age = " << s1.get_age() << endl;
    cout << "s1.gender = " << s1.get_gender() << endl;
    cout << "s1.height = " << s1.get_height() << endl;

    return 0;
}

运行结果

2. 写一个有默认参数的函数,把声明和定义分开,并在主函数内成功调用

代码

#include <iostream>

using namespace std;

void speak(int count=1, string content="hello");

int main()
{
    speak(1);
    speak(3, "Hello, xiaoming");
    return 0;
}

void speak(int count, string content)
{
    for(int i=1; i<=count; i++)
    {
       cout << content << endl;
    }

}

运行结果

3. 整理思维导图

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值