C++笔记 构造函数属性初始化,new,delete,对象的大小

来源:互联网 发布:cf录视频软件 编辑:程序博客网 时间:2024/06/07 01:38

1 构造函数属性初始化

class Teacher{private:    char* name;public:    Teacher(char* name){        this->name = name;    }    ~Teacher(){        cout << "Teacher析构函数" << endl;    }    char* getName(){        return this->name;    }};class Student{private:    int id;    //属性对象    Teacher t1;    Teacher t2;public:    Student(int id, char *t1_name, char* t2_name) :t1(t1_name),t2(t2_name){        this->id = id;        cout << "Student有参构造函数" << endl;    }    void myprint(){        cout << id << "," << t1.getName() << "," << t2.getName() << endl;    }    ~Student(){        cout << "Student析构函数" << endl;    }};void func(){    Student s1(10,"AV boduo","AV xiaoze");    s1.myprint();}
void main(){    func();    system("pause");}

打印结果

Student有参构造函数10,AV boduo,AV xiaozeStudent析构函数Teacher析构函数Teacher析构函数

从打印结果可以看出,先是类死销毁,然后成员变量再销毁 。

可以看出初始化成员变量的方式:

Student(int id, char *t1_name, char* t2_name) :t1(t1_name),t2(t2_name){        this->id = id;        cout << "Student有参构造函数" << endl;    }

是在构造方法中传参,并用参数来构造成员变量。

2 new创建对象,delete释放对象

class Teacher{private:    char* name;public:    Teacher(char* name){        this->name = name;    }    ~Teacher(){        cout << "Teacher析构函数" << endl;    }    char* getName(){        return this->name;    }    void setName(char *name){        this->name = name;    }};void func(){    //=================C++    //会调用构造函数和析构函数    Teacher *t1 = new Teacher("longze");    cout << t1->getName() << endl;    delete t1;  //释放    //==================C    Teacher *t2 = (Teacher*)malloc(sizeof(Teacher));    t2->setName("liuyan");    free(t2);}
void main(){    func();    system("pause");}

打印结果

longzeTeacher析构函数

根据打印结果,可以得出结论:
使用这种方式创建对象,会调用构造函数和析构函数。

    Teacher *t1 = new Teacher("longze");

但是使用这种方式,并不会调用构造函数和析构函数

Teacher *t2 = (Teacher*)malloc(sizeof(Teacher));

3 new创建数组对象,delete释放数组对象

void main(){    //==============C    int *p1 = (int*)malloc(sizeof(int)*10);    p1[0] = 9;    free(p1);    //==============C++    int *p2 = new int[10];    delete[] p2;    system("pause");    system("pause");}

4 static 访问静态属性和方法

class Timer{public:    static int times;//计数器public:    Timer(){        cout << "Timer无参构造函数" << endl;    }    ~Timer(){            cout << "Timer析构函数" << endl;    }     //计数 静态函数    static void count(){        times++;        cout << times << endl;    }};//静态属性初始化赋值int Timer::times = 9;
void main(){    //访问静态属性    Timer::times++;    cout << Timer::times << endl;    //直接通过类名访问    Timer::count();    //也可以通过对象名访问    Timer t;        t.count();    system("pause");}

打印结果

1011Timer无参构造函数12

5 类的大小

class A{public:    int i;    int j;    int k;    static int m;};class B{public:    int i;    int j;    int k;    void myprintf(){        cout << "打印" << endl;    }};
void main(){    cout << sizeof(A) << endl;    cout << sizeof(B) << endl;    system("pause");}

打印结果

1212

从打印结果可以看出,静态成员变量和方法并不占用类的空间.
C/C++内存分区:栈,堆,全局(静态,全局),常量区(字符串),程序代码区
普通属性和结构体的内存布局相同

6 常函数

class Teacher{private:    char*name;    int age;public:    Teacher(char* name,int age){        this->name = name;        this->age = age;        cout << "Teacher有参构造函数" << endl;    }    //常函数 修饰的是this    //既不能改变指针的值,又不能改变指针指向的内容    void myprint()const{        printf("%#x\n",this);        //改变属性的值        //this->name = "张三";//报错 不可改变        cout<<this->name << "," << this->age << endl;    }    void myprint2(){        cout << this->name << "," << this->age << endl;    }    ~Teacher(){        cout << "Teacher析构函数" << endl;    }};
void main(){    Teacher t("James",20);    const Teacher t2("rose",18);    //t2.myprint2();//常量对象只能调用常量函数,不能调用非常量函数    //常函数 当前对象不能被修改,防止数据被非法访问    printf("%#x\n",&t);    t.myprint();    printf("%#x\n",&t2);    t2.myprint();    system("pause");}

打印结果

Teacher有参构造函数Teacher有参构造函数0x8ffbf40x8ffbf4James,200x8ffbe40x8ffbe4rose,18

打印结果并没有什么卵用。主要是常函数的特点:常函数内的属性不能改变。

阅读全文
0 0