美文网首页
C++ 构造函数

C++ 构造函数

作者: ZoranLee | 来源:发表于2021-03-12 18:26 被阅读0次

费曼学习法

https://wiki.mbalib.com/wiki/%E8%B4%B9%E6%9B%BC%E5%AD%A6%E4%B9%A0%E6%B3%95

  • 1、把它教给一个小孩子
  • 2、回顾,找到自己知识的边缘
  • 3、把语言条理化,简化
  • 4、再传授

命名空间

using namespacing std;
std:cout<<"输出函数"<<endl;

//自定义命名空间
namespace ZoranLee{
  int age =33;
  char *name = "ZoranLee";
  void show() {
        cout << "name:" << name << "age:" << age << endl;
    }
}

int main() {
// using namespace ZoranLee;
//    show();//直接调用 配合 using namespace ZoranLee;
//    cout << ZoranLee::age << ZoranLee::name << endl;//使用命名空间调用
}
  • 两个命名空间重复的函数
//新增命名空间 ZoranLee1
namespace ZoranLee1 {
    void show() {
        cout << "hello world" << endl;
    }
}

using namespace ZoranLee1;

int main(){
//直接调用show 是会报错,需要使用::调用
  ZoranLee1::show();
  ZoranLee::show();
return 0 ;
};
  • 命名空间的嵌套
namespace ZoranLee{
  namespace ZoranLee1{
    namespace ZoranLee2{
    int age= 30;
  char * name = "name";
}
}
}

using namespace std;
using namespace ZoranLee::ZoranLee1::ZoranLee2;
int main(){
//直接嵌套引出
cout<<ZoranLee::ZoranLee1::ZoranLee2::age<<endl;//1
cout<<age<<endl;//2
}

构造函数

#include "iostream"
using namespace std;
class Student {
public:
    Student() {
        cout << "无参构造" << endl;
    }

    Student(char *name) : name(name) {
        cout << "一个参数的构造" << endl;
//        this->name = name;
    }

    Student(char *name,int age) {
        cout << "两个参数的构造" << endl;
        this->age = age;
        this->name = name;
    }

    void setAge(int age) {
        this->age = age;
    }

    void setName(char *name) {
        this->name = name;
    }

    int getAge() {
        return this->age;
    }

    char *getName() {
        return this->name;
    }
private:
    int age;
    char *name;
};

//使用栈区构造函数
int main() {
//    Student *stu = new Student();

    Student stu;//无参构造
    stu.setAge(30);
    stu.setName("名字");
    cout << stu.getAge() << "\n" << stu.getName() << endl;

    Student stu1("名字",30); //调用两个参数构造
    cout << stu1.getAge() << "\n" << stu1.getName() << endl;
    return 0;
}

  • 构造函数之间相互调用

 Student(char *name) :Student(name,30) {
        cout << "一个参数的构造" << endl;
        this->name = name;
    }

//会先调用两个参数的构造,再调用一个参数的构造
int main(){
Student stu("名字");
return 0 ;
}
  • 堆区使用
  Student * stu = new Student("名字",30);
  cout<<stu.getName<<endl;
  delete stu;//回收 不能使用free

Student * stu =(Student *) malloc(sizeof Student);
free(stu);

析构函数

  • 析构函数(destructor) 与构造函数相反,当对象结束其生命周期,如对象所在的函数调用完毕时,系统自动执行析构函数。析构函数往往用来做“清理善后” 的工作(例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。
  • delete会调用
  • 不能传参
this ->name = (char *)malloc(sizeof(char*)*10);
strcpy(this->name,name);

~Student(){
  if(this->name){
    free(this->name);
    this->name = NULL;
  }
}

//两个参数的构造函数
Student(char * name,int age):name(name),age(age){
}
  • new/delete;malloc/free

拷贝构造函数

  • 引子
  struct Person{
int age;
char * name;
}
int main(){
  Person person = {20, "张三丰"};
    Person person2 = person;
    std::cout <<"person:"<< &person2 << ";" << &person << std::endl;
    std::cout << "name:" << &(person2.name) << ";" << &(person.name) << std::endl;
    std::cout << "age:"<< &(person2.age) << ";" << &(person.age) << std::endl;

//    person:0x7ffee83342a0;0x7ffee83342b0
//    name:0x7ffee83342a8;0x7ffee83342b8
//    age:0x7ffee83342a0;0x7ffee83342b0
     return 0;
}

由以上打印结果可以看出,在栈空间是将person的值赋值给person2对应的成员,他们地址都不相同,只是值赋值

  • 拷贝函数
    对象1 = 对象2 会执行默认的拷贝构造函数
class Student {
public:
    Student() {

    }

    Student(int age, char *name) {
        this->age = age;
        this->name = name;
    }

    Student(const Student &student) {
        cout << "拷贝构造函数" << endl;
        this->name = student.name;
        this->age = student.age - 10;
    }

    char *getName() {
        return this->name;
    }

    int getAge() {
        return this->age;
    }

private:
    int age;
    char *name;
};

int main() {
    Student stu(30, "张三");
    Student stu2 = stu;
//    Student stu2 ;    
//     stu2 = stu;//这种方式就不调用拷贝函数
    cout << stu2.getAge() << stu2.getName() << endl;
}
image.png
  • 使用堆空间
  
    Student *stu = new Student(30, "zhang");
    Student *stu2 = stu;
    stu2->setName("ssss");
    cout << stu->getName() << stu->getAge() << endl;
    cout << stu2->getName() << stu2->getAge() << endl;
    cout << "stu地址:" << &stu << endl;
    cout << "stu2地址:" << &stu2 << endl;

stu name地址:0x7fcee6c02b78
ssss;30
stu name地址:0x7fcee6c02b78
ssss;30
stu地址:0x7ffeebca42b8
stu2地址:0x7ffeebca42a0

image.png

指针常量、常量指针

  • 常量指针


    image.png
image.png image.png image.png image.png

相关文章

  • windows逆向3

    VC 程序内存和编译的一些特征C++ 构造函数C++ 成员函数C++ 析构函数C++ 全局对象的构造C++ 全局对...

  • C++ 构造过程

    构造函数 C++中每个类必须有一个构造函数,如果用户没有自行编写构造函数,则C++会自动提供一个无参数的构造函数,...

  • C++:面向对象基础

    构造函数 C++中有三种构造函数:默认构造函数,有参构造函数,拷贝构造函数 类对象的初始化 括号法//默认构造函数...

  • [C++之旅] 10 构造函数

    [C++之旅] 10 构造函数 构造函数的特点 构造函数在对象实例化时被自动调用 构造函数与类同名 构造函数没有返...

  • scala学习笔记-构造函数

    scala的构造函数分为主构造函数和辅助构造函数 【辅助构造函数】比较容易理解,它们同C++和Java的构造函数十...

  • c++面向对象-构造、析构、初始化列表

    构造函数 构造函数是指创建对象的时候,会调用的函数。C++构造函数可以重载,指针或者对象均可调用构造函数。 析构函...

  • C++基础③new对象,继承,友元函数,函数的可变参数

    接续上篇C++基础②构造函数,析构函数,拷贝构造函数 前言 C++是一门面向对象的编程语言 , 创建用以创建对象 ...

  • C++ 构造函数,类的成员变量

    c++ 05 构造函数无参构造函数有参构造函数 拷贝构造函数 浅拷贝 深拷贝 类的成员变量 四类特殊的成员变量

  • [字符串] 自己实现一个string类(一)

    C++类一般包括:构造函数,拷贝构造函数,赋值构造函数和析构函数四大函数。 在上面的赋值构造函数中,都是先dele...

  • C++拷贝构造函数——难点

    拷贝构造函数 - C++详细 | 编程字典

网友评论

      本文标题:C++ 构造函数

      本文链接:https://www.haomeiwen.com/subject/qbszqltx.html