0
点赞
收藏
分享

微信扫一扫

C++ 构造函数

千行 2021-09-28 阅读 108
技术

费曼学习法

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;
}

  • 使用堆空间
  
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

指针常量、常量指针

  • 常量指针


举报

相关推荐

0 条评论