DAY2C++

发布时间:2023年12月27日

1 定义学生结构体,包含私有成员年龄、性别、身高,公有成员成绩,在结构体中定义公有的函数,完成对私有成员的赋值和获取操作,要求:函数结构体内声明,结构体外定义。用class。

#include <iostream>

using namespace std;

class Stu{
    int year;
    char sex;
    int height;
public:
    float score;
    int get_privateyear();
    int get_privateheight();
    char get_privatesex();
    void set_private(int year,char sex,int height,float s);
};
int Stu::get_privateyear()
{
    return year;
}
int Stu::get_privateheight()
{
    return height;
}
char Stu::get_privatesex()
{
    return sex;
}
void Stu::set_private(int a,char b,int c,float s)
{
    year = a;
    sex = b;
    height = c;
    score = s;
}
int main()
{
    Stu s1;
    cout<<"请依次输入年龄、身高、成绩、性别"<<endl;
    int a,c;
    float s;
    char b;
    cin>>a;
    cin>>c;
    cin>>s;
    cin>>b;
    s1.set_private(a,b,c,s);
    cout<<"年龄"<<s1.get_privateyear()<<endl;
    cout<<"身高"<<s1.get_privateheight()<<endl;
    cout<<"成绩"<<s1.score<<endl;
    cout<<"性别"<<s1.get_privatesex()<<endl;

    return 0;
}

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

#include <iostream>

using namespace std;
int aa(int a,int b = 1);

int main()
{
    cout << aa(1)<< endl;
    return 0;
}
int aa(int a,int b)

{
    return a+b;
}

3.计算结构体大小

16 12 12 6 40 92 8 5 32

思维导图

文章来源:https://blog.csdn.net/Aklkimg/article/details/135254173
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。