面向对象第六讲
构造函数
#pragma warning(disabled:4996)
#include <iostream>
#include <string.h>
using namespace std;
class Test
{
public:
Test()
{
this->name = nullptr;
cout << "Test" << endl;
}
// Test() = default;//声明无参构造函数使用系统默认生成的(C++11)
// Test() = delect;//不让系统默认生成无参构造函数(C++11)
Test(int num, const char *name, int age)
{
this->num = num;
this->age = age;
// this->name = name;//error
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name,name);
cout << "Test int char *" << endl;
}
Test(int num,int age)
{
this->num = num;
this->age = age;
this->name = nullptr;
cout << "Test int int" << endl;
}
Test(const Test &other)
{
this->num = other.num;
this->age = other.age;
//this->name = other.name;
int len = strlen(other.name);
this->name = new char[len + 1];
strcpy(this->name,other.name);
cout << "Test copy Test" << endl;
}
explicit Test(int num)//防止发生隐式转换
{
this->num = 0;
this->age = 0;
this->name = nullptr;
cout << "Test int" << endl;
}
explicit Test(const char *name)
{
this->name = nullptr;
this->num = 0;
this->age = 0;
}
Test(Test&& other)//移动拷贝构造函数
{
this->num = other.num;
this->age = other.age;
this->name = other.name;
other.name = nullptr;
cout << "Test copy Test &&" << endl;
}
~Test()
{
if(name != nullptr)
{
delete [] name;
}
cout << "~Test" << endl;
}
int num;
char *name;//string更加安全
int age;
};
//无参构造函数:系统默认生成、自定义(default、delete)
//有参构造函数:可以重载
//拷贝构造函数(用已有的对象初始化新的对象):默认拷贝构造函数(问题:浅拷贝问题)自定义拷贝函数(深拷贝)
//拷贝构造函数的调用时机:
//1.用已有的对象初始化新的对象
//2.传参:当形参为类的对象时,传参时会调用
//3.函数返回值为对象时
//类型转换构造函数:构造函数只有一个参数的,都是类型转换构造函数;用explicit防止发生类型转换
//移动拷贝构造函数:解决临时对象拷贝开销问题
int main()
{
Test t;//调用无参构造函数:默认的无参构造函数(类里没有任何构造函数时,系统会默认生成)
Test t1(1,"zhangsan",21);..有参构造函数
Test t2(2,23);
Test t3(t1);
//Test t4 = 1;//调用类型转换构造函数,危险(实现了隐式类型转换:可以将其他类型转换成类类型,可读性也差),调用explicit后报错
Test t5(1);
//Test t6 = "hello world";
//Test t7;
//t7 = 1;//报错
return 0;
}
无参构造函数 | 系统默认生成、自定义 |
---|---|
有参构造函数 | 可以重载 |
拷贝构造函数 | 用已有的对象初始化新的对象 |
类型转换构造函数 | 构造函数只有一个参数的都是类型转换构造函数 |
移动拷贝构造函数 | 解决临时对象拷贝效率问题 |
拷贝构造函数
默认拷贝构造函数存在问题:浅拷贝
自定义拷贝构造函数:深拷贝
调用时机:
1.用已有的对象初始化新的对象;
2.传参(当形参为类的对象时,传参时会调用拷贝构造函数);
3.函数返回值为对象时;
class Test
{
public:
Test(int num, const char* name, int age)
{
this->num = num;
this->age = age;
//this->name = name;//error;
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
cout << "Test int char * int" << endl;
}
Test(const Test& other)
{
this->num = other.num;
this->age = other.age;
//this->name = other.name;
int len = strlen(other.name);
this->name = new char[len + 1];
strcpy(this->name, other.name);
cout << "Test copy Test" << endl;
}
~Test()
{
if (name != nullptr)
{
delete[] name;
}
cout << "~Test" << endl;
}
};
void test1(Test t)
{
cout << t.name << endl;
}
Test test2()
{
Test temp(1,"lisi",14);
return temp;
}
int main()
{
Test t1(1,"zhangsan,13");
test(t1);
Test t2 = test2();
return 0;
}
输出
Test int char * int
Test copy Test
zhangsan
~Test
Test in char * int
Test copy Test
~Test
~Test
类型转换构造函数
可以用explicit防止发生类型转换
调用类型转换构造函数危险(它属于隐式类型转换,可以将其他类型转换成类类型,且可读性也不高)
构造函数初始化
#include <iostream>
using namespace std;
class A
{
public:
A(int a) : a(a)
{
this->a = a;
cout << "A" << endl;
}
int a;
}
class Test
{
public:
Test() : num(0),l_num(count),a(5)
{
cout << "Test" << endl;
}
Test(int num, int count) : num(5),count(6),l_num(num)//初始化列表
{
//l_num = num;
//this->num = num;
//this->count = count;
cout << "Test int int" << endl;
}
const int num;
int count;
int &l_num;//定义引用必须初始化
A a;//a成员对象
};
int main()
{
Test t1(1,2);
//const int num;
//int &l_num;
return 0;
}
为什么引入初始化列表?
能实现定义并初始化,先于构造函数执行
以下成员必须在初始化列表初始化:
引用;const修饰的成员;成员对象
类内初始化
一般不会这样写,都在构造函数里面初始化
class Test
{
......
int count = 6;//C++11之后支持
int &l_num = count;
......
};
static&const关键字
static关键字
修饰成员变量,修饰成员方法
#include <iostream>
#include <pthread.h>
using namespace std;
class Test
{
public:
//static修饰方法,该方法属于类,不属于对象,没有this指针,只能访问static修饰的成员,不能访问非static成员,可以通过类名::函数名访问
static void * print(void *)
{
cout << num << endl;
//cout << this->count << endl;//count不能访问
}
static int num;//类成员变量,属于类,被这个类的所有对象共享;可以使用类名::名字访问
//使用注意事项:static修饰的成员必须在类外初始化
int count;
};
int Test::num = 0;
int main()
{
pthread_t id;
pthread_create(&id, NULL,Test::print,NULL);
Test::num = 10;
cout << Test::num << endl;
Test t1;
t1.num = 10;
t1.count = 12;
t1.print();//可以
Test::print();//也可以
//Test t;
//Test t1;
//Test t2;
t.num = 5;
Test::num = 6;
t1.num = 6;
t2.num = 7;
cout << t.num << endl;
return 0;
}
static修饰成员叫做类成员变量,属于类,不属于对象,被这个类的所有对象共享;可以使用类名::名字访问
注意事项: static修饰的成员必须要在类外初始化
static修饰方法,该方法属于类,不属于对象,没有this指针,只能访问static修饰的成员,不能访问非static成员,可以通过类名::函数名访问
工程应用
static修饰成员作用 | 实现对象通信 |
---|---|
static修饰方法作用 | 为了兼容C/C++混合编程的 |
const关键字
修饰成员,修饰方法,修饰对象
class Test
{
......
const int index;//const修饰的成员必须在初始化列表初始化
......
};
class Test
{
public:
Test() : index(count)
{
}
void printCount() const//const修饰成员方法,该成员方法只能使用成员变量的值,不能修改
{
count++;//未加mutable时不能修改
cout << count << endl;
}
mutable int count;//加了mutable可以在const方法里面修改
};
int main()
{
Test t1;
ti,printCount();
const Test t2;//const修饰的对象只能调用const成员方法
t2.printCount();
return 0;
}
mutable关键字作用:告诉编译器该成员可以在const方法中被修改
总结
C/C++中static关键字的作用?
C/C++中const关键字的作用?
C修饰局部变量,修饰全局变量,修饰函数
C++修饰成员变量,修饰成员方法,修饰对象