2023/12/27 work

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

using namespace std;

class Stu{
    private:
       int age;
       string sex;
       double height;
    public:
       double score;

       void set_info(int age1,string sex1,double height1);
       int get_age();
       string get_sex();
       double get_height();
       void get_info();

};

void Stu::set_info(int age1, string sex1, double height1){
    age = age1;
    sex = sex1;
    height = height1;
}


int Stu::get_age(){
    return age;
}
string Stu::get_sex(){
    return sex;
}
double Stu::get_height(){
    return height;
}

int main()
{
    Stu s1;
    s1.score = 200;
    s1.set_info(30,"男",172.2);
    //cout << " score= " << s1.score << endl;
    cout << "age = " << s1.get_age() << " sex = " << s1.get_sex() << " heigth = "
         << s1.get_height() << " score= " << s1.score << endl;

    //cout << sizeof(string) << endl;
    return 0;
}

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

#include 

using namespace std;

//定义一个有默认参数的函数

struct Stu{

    public:
    int sum_score(int a,int b=10);
};


int Stu::sum_score(int a,int b){

    return a + b;

}

int main()
{
    Stu stu;
    cout << "sum_score = " << stu.sum_score(80)   << endl;
    return 0;
}

3. 字节对齐

struct data {
char t1 ;     //1
char t2 ;     //1
unsigned short t3 ; //
unsigned long t4 ;  //
};
16 字节
struct data {
char t1 ;    //1 3
int t2 ;       //4
short t3 ;   //2
};   ---> 10 
不是 4 的倍数   12
struct s1
{
char c1 ;   // 1   3
int i ;   //  4
char c2 ;  // 1
};  // 9 
---> 不是 4 的倍数  12
//2 字节对齐
struct s2
{
char c1 ;  //1
char c2 ; //1
int i ; //4
};  
6
typedef struct Test
{
short a ;       //  2  //6
struct
{
int b ;         // 4  +4
double c ; // 8
char d ;    // 1 // 7
};
int e ;  //4
} Test ;

35 不是 4 的  倍数

40 字节

4. 思维导图

你可能感兴趣的:(c++,算法,开发语言)