作业--day37

2023-12-27 22:31:35

课上strcut的练习改成class,并写一个有默认参数的函数,把声明和定义分开,并在主函数内成功调用

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

class stu{
private:
    int age;
    char sex;
    float high;
public:
    int get_age();
    char get_sex();
    float get_high();
    void set_info(int age, char sex = 'w', float high = 160);
};

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

char stu::get_sex(){
    return sex;
}

float stu::get_high(){
    return high;
}

void stu::set_info(int a, char b, float c){
    age = a;
    sex = b;
    high = c;
}

int main()
{
    stu s;
    s.set_info(24);
    cout << s.get_age() << endl;
    cout << s.get_sex() << endl;
    cout << s.get_high() << endl;
    s.set_info(30, 'm', 175.5);
    cout << s.get_age() << endl;
    cout << s.get_sex() << endl;
    cout << s.get_high() << endl;
    return 0;
}

在这里插入图片描述

字节对齐

1、
struct data{
char t1;
char t2;
unsigned short t3;
unsigned long t4;
}; //32位:12字节,64位:16字节
2、
struct data{
char t1;
int t2;
short t3;
}; //12字节
3、
struct s1{
char c1;
int i;
char c2;
}; //12字节
4、
//2字节对齐
struct s2{
char c1;
int i;
char c2;
}; //8字节
5、
typedef struct Test
{
short a;
struct
{
int b;
double c;
char d;
};
int e;
}Test; //8字节
6、
typedef struct Test
{
short a;
struct
{
int b;
double c[10];
char d;
};
int e;
}Test; //8字节
7、
struct C{
char b;
int a;
short c;
}; //12字节
8、
struct C {
char a;
char b[3];
char c;
}; //5字节
9、
typedef struct
{
int b;
char a;
long e;
char c;
float d;
double t;
}node; //32位:28字节,64位:32字节

思维导图

在这里插入图片描述

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