一对象的初始化和处理
1.1构造函数和析构函数
语法:
代码:
#include <iostream>
using namespace std;
class person {
public:
person() {
cout << "person 构造函数调用" << endl;
}
~person() {
cout << "person 析构函数调用" << endl;
}
};
void func() {
person a;
}
int main() {
func();
return 0;
}
1.2构造函数的分类和调用
1.2.1构造函数分类
(1)有参和无参构造函数
代码:
#include <iostream>
using namespace std;
class person {
public:
person() {
cout << "person 无参构造函数调用" << endl;
}
person(int a) {
cout << "person 有参构造函数调用" << endl;
}
~person() {
cout << "person 析构函数调用" << endl;
}
};
void func() {
person a(1);
}
int main() {
func();
return 0;
}
(2)普通和拷贝构造函数
代码:
#include <iostream>
using namespace std;
class person {
public:
int age;
person() {
cout << "person的普通构造函数" << endl;
}
person(const person& p) {
age = p.age;
//可以将传入的人的所有属性,拷贝到神上
cout << "person的拷贝构造函数" << endl;
}
};
void func() {
//1.括号法
person p1;
p1.age = 10;
person p2(p1);
cout << p2.age << endl;
}
int main() {
func();
return 0;
}
1.2.2构造函数的调用方式
(1)括号法:
#include <iostream>
using namespace std;
class person {
public:
int age;
person() {
cout << "person的普通构造函数" << endl;
}
person(int a) {
age = a;
cout << "person的普通构造函数" << endl;
}
person(const person& p) {
age = p.age;
//可以将传入的人的所有属性,拷贝到神上
cout << "person的拷贝构造函数" << endl;
}
};
void func() {
//1.括号法
person p1; //默认
p1.age = 10;
person p2(20); //有参
person p3(p1); //拷贝
//2.
}
int main() {
func();
return 0;
}
(2)显示法:
#include <iostream>
using namespace std;
class person {
public:
int age;
person() {
cout << "person的普通构造函数" << endl;
}
person(int a) {
age = a;
cout << "person的普通构造函数" << endl;
}
person(const person& p) {
age = p.age;
//可以将传入的人的所有属性,拷贝到神上
cout << "person的拷贝构造函数" << endl;
}
};
void func() {
//2.显示法
person p1; //默认
p1.age = 10;
person p2=person(20); //有参
person p3=person(p1); //拷贝
//2.
}
int main() {
func();
return 0;
}
(3) 隐式转换法:
#include <iostream>
using namespace std;
class person {
public:
int age;
person() {
cout << "person的普通构造函数" << endl;
}
person(int a) {
age = a;
cout << "person的普通构造函数" << endl;
}
person(const person& p) {
age = p.age;
//可以将传入的人的所有属性,拷贝到神上
cout << "person的拷贝构造函数" << endl;
}
};
void func() {
//3.隐式转换发
person p1; //默认
person p2=10; //相当于person p2=person(10);
person p3 = p2; //相当于person p3 = person(p2);
}
int main() {
func();
return 0;
}
1.3拷贝构造函数调用时机
1.3.1旧对象创建新对象
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
int b;
person() {
cout << "person的无参构造函数" << endl;
}
person(int a1) {
a = a1;
cout << "person的有参构造函数" << endl;
}
person(const person &p) {
a = p.a;
cout << "person的拷贝构造函数" << endl;
}
~person() {
cout << "person的析构函数" << endl;
}
};
void fun() {
person A(10);
person B(A);
cout << B.a << ' ' << B.b << endl;
}
int main() {
fun();
return 0;
}
1.3.2值传递的方式给函数参数传值
#include <iostream>
using namespace std;
class person {
public:
int a;
int b;
int c;
person() {
cout << "person的无参构造函数" << endl;
}
person(int a1) {
a = a1;
cout << "person的有参构造函数" << endl;
}
person(const person &p) {
a = p.a;
cout << "person的拷贝构造函数" << endl;
}
~person() {
cout << "person的析构函数" << endl;
}
};
void fun(person A) {
cout << A.a << ' ' << A.b <<' '<<A.c << endl;
}
int main() {
person A(10);
A.b = 90;
cout << A.a << ' ' << A.b << ' ' << A.c << endl;
fun(A);
return 0;
}
1.3.3以值方式返回局部对象
#include <iostream>
using namespace std;
class person {
public:
int a;
int b;
int c;
person() {
cout << "person的无参构造函数" << endl;
}
person(int a1) {
a = a1;
cout << "person的有参构造函数" << endl;
}
person(const person &p) {
a = p.a;
cout << "person的拷贝构造函数" << endl;
}
~person() {
cout << "person的析构函数" << endl;
}
};
person fun() {
person A(10);
A.b = 90;//已经赋值,但是形参还是随机值
cout << A.a << ' ' << A.b << ' ' << A.c << endl;
return A;
}
int main() {
person A=fun();
cout << A.a << ' ' << A.b << ' ' << A.c << endl;
return 0;
}
1.4构造函数调用规则
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
int b;
int c;
person(int a1) {
a = a1;
cout << "person的有参构造函数" << endl;
}
~person() {
cout << "person的析构函数" << endl;
}
};
person fun() {
person A(10);
A.b = 90;
//person B; 报错
return A;
}
int main() {
fun();
return 0;
}
1.5深拷贝和浅拷贝
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
int* b;
person(int a1, int b2) {
a = a1;
b = new int(b2);
}
person(const person &p) { //浅拷贝
a = p.a;
b = p.b;
}
~person() { //浅拷贝
if (b != NULL) {
delete b;
b = NULL;
}
}
};
void fun() {
person a(10,90);
person b(a);
cout << b.a << ' ' << *b.b << endl;
}
int main() {
fun();
return 0;
}
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
int* b;
person(int a1, int b2) {
a = a1;
b = new int(b2);
}
person(const person &p) { //深拷贝
a = p.a;
b = new int(*(p.b));
}
~person() {
if (b != NULL) {
delete b;
b = NULL;
}
}
};
void fun() {
person a(10,90);
person b(a);
cout << b.a << ' ' << *b.b << endl;
}
int main() {
fun();
return 0;
}
1.6初始化列表
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
int b;
int c;
person(int a1,int b1,int c1) :a(a1), b(b1), c(c1) { //初始化列表
}
};
void fun() {
person p(30, 3,78);
cout << p.a << ' ' << p.b << ' ' << p.c << endl;
}
int main() {
fun();
return 0;
}