05.构造函数属性初始化 static关键字 常量函数

简介: (创建于2017/12/24)如果一个类中属性有另一个类或者有const类型,如何初始化 #define _CRT_SECURE_NO_WARNINGS#include "iostream" //包含c++的头文件using namespa...

(创建于2017/12/24)

如果一个类中属性有另一个类或者有const类型,如何初始化
 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;

class A {
public:
    A(int a) {

    }
};

class B {
public:
    int b;
    A a;
    A a2;
    const int c;
    B(int b):a(1),a2(2),c(3){

    }
    B(int a, int b, int c, int d,int e) :a(c), a2(d),c(e){

    }
};

int main() {
    B b(1,2,3,4,5);
    system("pause");
    return 0;
}
构造函数中调用构造函数

分析一下下边这个函数,为什么最终打印c得到的是一个很奇怪的数字呢?函数执行到 B b(1,2);的时候会执行B的两个参数的有参构造方法,将ab初始化,然后在此调用三个有参构造方法,传入三个值1,2,100,我们知道调用函数的构造方法会默认产生一个匿名对象,这个匿名对象是B的另一个对象,和b是不同的,由于这个对象没有被接收,导致执行完成之后就调用了他的析构函数销毁了这个对象,所以到此位置,b的c就没有被赋值过,所以打印的是这样的结果

 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;

class B {
public:
    int b;
    int c;
    int d;
    B(int a,int b){
        this->b = a;
        this->c = b;
        B(a, b, 100);
    }

    B(int c, int d, int e) {
        this->b = c;
        this->c = d;
        this->d = e;
    }
    ~B(){
        printf("析构函数执行:%d,%d,%d\n", b, c, d);
    }
};

int main() {
    B b(1,2);
    printf("c:%d\n", b.d);
    system("pause");
    return 0;
}

打印结果
析构函数执行:1,2,100
c:-858993460
请按任意键继续. . .
//注意构造和析构的顺序:
//先执行类内部的对象的构造方法,再执行的类的构造方法,析构函数则是先执行的外层的析构函数,然后执行的对象属性的析构函数

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>   //这个头文件和命名空间有一个不存在 cout就无法使用
using namespace std;
class Teacher {
private:
    char* name;
public:
    Teacher(char* name) {
        this->name = name;
        cout << "Teacher有参构造函数" << endl;
    }
    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }
    char* getName() {
        return this->name;
    }

};

class Student {
private:
    int id;
    Teacher t1;
public:

    //属性对象通过构造方法初始化
    Student(int id, char *n1, char *n2):t1(n1) {
        this->id = id;
        cout << "Student有参构造函数" << endl;
    }
    void myprint() {
        cout << id << "," << t1.getName()<< endl;
    }
    ~Student() {
        cout << "Student析构函数" << endl;
    }
};

void func() {
    Student s1(10, "miss bo", "mrs liu");
    s1.myprint();
}
C和C++开辟内存对比
 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;

class B {
public:
    int b;
    int c;
    int d;
    B(int a,int b){
        this->b = a;
        this->c = b;
        B(a, b, 100);
    }

    B(int c, int d, int e) {
        this->b = c;
        this->c = d;
        this->d = e;
    }
    ~B(){
        printf("析构函数执行:%d,%d,%d\n", b, c, d);
    }
};

int main() {
    ///////////////////基本数据类型///////////////////////
    //C
    int *a = (int *)malloc(sizeof(int));
    *a = 10;
    printf("*a=%d\n", *a);
    free(a);

    //C++
    int *b = new int();
    *b = 20;
    cout << "*b="<<*b<< endl;
    delete b;
    ///////////////////数组类型///////////////////////
    //C
    int *c = (int *)malloc(sizeof(int) * 10);
    *(c + 3) = 30;
    printf("第三个数组元素值=%d\n", c[3]);
    free(c);

    //C++
    int *d = new int[10];
    *(d + 4) = 40;
    cout << "第四个数组元素="<<d[4]<< endl;
    delete [] d;//删除数据加[]

    //////////////////对象类型/////////////////////

    //C
    B *e = (B*)malloc(sizeof(B));
    e->c = 100;
    printf("e->c =%d\n", e -> c);
    free(e);

    //C++
    B *f = new B(200,300);
    cout << "f->c ="<<f->c<< endl;
    delete f;
    system("pause");
    return 0;
}

malloc和delete?new和free?

这两种运算符能否混搭使用呢,比如我用malloc开辟的内存,能否使用delete回收?我用new创建的对象,能否使用free释放?答案是可以的,但是有一点,delete回收内存相对于free,多了一步执行析构函数的过程,而new创建对象相对于malloc,多了一步执行构造方法的过程

//C++的new delete和C的malloc free的区别:
//new delete的方式创建和销毁会分别执行构造函数和析构函数
//malloc free则不会执行

void func2() {
    //C++
    //Teacher *t1 = new Teacher("renzhenming");
    //cout << t1->getName() << endl;
    //释放
    //delete t1;
    //C
    Teacher* t2 = (Teacher*)malloc(sizeof(Teacher));
    t2->setName("renzhenming2");
    free(t2);
}

void main() {
    func();
    cout << "----------------------------------------------" << endl;
    //C++和C创建和回收数组
    //c
    int *p = (int*)malloc(sizeof(int) * 10);
    p[0] = 9;
    free(p);
    //c++
    int *p2 = new int[10];
    p[0] = 2;
    //释放数组
    //注意释放数组需要加[]
    delete[]p2; 
    system("pause");
}
相关文章
|
网络安全 开发工具 数据安全/隐私保护
git pull/push每次都需要输入密码问题
git pull/push每次都需要输入密码问题
1023 0
|
Linux
`grep`命令搜索多个文件中的特定模式
`grep`命令搜索多个文件中的特定模式
724 2
|
Go 开发工具 git
vscode设置go环境
vscode设置go环境
404 0
|
JavaScript Ubuntu 编译器
windows下安装make,使用makefile文件
windows下安装make,使用makefile文件
1440 0
|
Java Maven
IDEA:导入MAVEN多模块项目
IDEA:导入MAVEN多模块项目
IDEA:导入MAVEN多模块项目
|
12天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
11天前
|
存储 人工智能 搜索推荐
终身学习型智能体
当前人工智能前沿研究的一个重要方向:构建能够自主学习、调用工具、积累经验的小型智能体(Agent)。 我们可以称这种系统为“终身学习型智能体”或“自适应认知代理”。它的设计理念就是: 不靠庞大的内置知识取胜,而是依靠高效的推理能力 + 动态获取知识的能力 + 经验积累机制。
379 133