在这一章节开始前,我们先回顾一下上一节
什么是对象?
对象,是一个特定“类”的具体实例。
对象和普通变量有什么区别?
一个对象,就是一个特殊的变量,但是有着丰富的功能和用法。
//借用我们上一节的类来举例
//对象的第一种使用方法
int main(void) {
Human h1; // 通过自定义的特殊数据类型“Human”类, 来创建一个“对象”
// 合法使用
h1.eat();
h1.play();
h1.sleep();
// 非法使用
//cout << "年龄" << h1.age << endl; //直接访问私有成员,将无法通过编译
//正确使用
cout << "年龄" << h1.getAge() << endl;
system("pause");
}
/*
总结:
1.“.”的使用
2.调用方法时,方法名后需要带一对圆括号()
3.通过对象,只能调用这个对象的public 方法/数据
分析:
多个不同的对象都有自己的数据,彼此无关。
*/
//对象的第二种使用方法
int main(void) {
Human h1; // 通过自定义的特殊数据类型“Human”类, 来创建一个“对象”
Human *p;
p = &h1;
// 合法使用
p->eat();
p->play();
p->sleep();
// 非法使用
//cout << "年龄" << p->age << endl;
//直接访问私有成员,将无法通过编译
//正确使用
cout << "年龄" << p->getAge() << endl;
system("pause");
}
/*
小结:
-> 的使用(类似C语言的结构体用法)
*/
在以上两个例子中我们都通过 Human h1 这样的方式来构建一个对象,那么对象的生成原理是怎么一回事呢?
“生而不同”之构造函数
构造函数的作用
在创建一个新的对象时,自动调用的函数,用来进行“初始化”工作:
对这个对象内部的数据成员进行初始化。
构造函数的特点
1)自动调用(在创建新对象时,自动调用)
2)构造函数的函数名,和类名相同
3)构造函数没有返回类型
4)可以有多个构造函数(即函数重载形式)
构造函数的种类
1 )默认构造函数
2 )自定义的构造函数
3 )拷贝构造函数
4 )赋值构造函数
默认构造函数
没有参数的构造函数,称为默认构造函数。构造函数又分两类:一类是手动定义的默认构造函数,另一类是在我们未手动定义构造函数时编译器自动合成的默认构造函数。
合成的默认构造函数
在没有手动定义默认构造函数时,编译器自动为这个类定义一个构造函数。
1)如果数据成员使用了“类内初始值”,就使用这个值来初始化数据成员。【C++11】
2)否则,就使用默认初始化(实际上,不做任何初始化,所有数据都是随机值)
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
// 定义一个“人类”
class Human {
public: //公有的,对外的
string getName();
int getAge();
int getSalary();
private:
string name;
int age = 18;
int salary;
};
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
int main(void) {
Human h1; // 使用合成的默认初始化构造函数
cout << "年龄: " << h1.getAge() << endl; //使用了类内初始值
cout << "薪资:" << h1.getSalary() << endl; //没有类内初始值
system("pause");
return 0;
}
运行结果:
注意:
只要手动定义了任何一个构造函数,编译器就不会生成“合成的默认构造函数”一般情况下,都应该定义自己的构造函数,不要使用“合成的默认构造函数”【仅当数据成员全部使用了“类内初始值”,才宜使用“合成的默认构造函数”】
手动定义的默认构造函数
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
// 定义一个“人类”
class Human {
public: //公有的,对外的
Human(); //手动定义的“默认构造函数”
string getName();
int getAge();
int getSalary();
private:
string name;
int age = 18;
int salary;
};
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
Human::Human() {
name = "无名氏";
age = 18;
salary = 30000;
}
int main(void) {
Human h1; // 使用手动的默认初始化构造函数
cout << "姓名:" << h1.getName() << endl; //无名氏
cout << "年龄: " << h1.getAge() << endl; //18
cout << "薪资:" << h1.getSalary() << endl; //30000
system("pause");
return 0;
}
说明:
如果某数据成员使用类内初始值,同时又在构造函数中进行了初始化,
那么以构造函数中的初始化为准。
相当于构造函数中的初始化,会覆盖对应的类内初始值。
自定义的重载构造函数
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
// 定义一个“人类”
class Human {
public: //公有的,对外的
Human(); //手动定义的“默认构造函数”
Human(int age, int salary);//自定义的构造函数重载
string getName();
int getAge();
int getSalary();
private:
string name;
int age = 18;
int salary;
};
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
Human::Human() {
name = "无名氏";
age = 18;
salary = 30000;
}
Human::Human(int age, int salary) {
this->age = age; //this是一个特殊的指针,指向这个对象本身
this->salary = salary;
name = "无名";
}
int main(void) {
Human h1(22,15000); // 使用手动的默认初始化构造函数
cout << "姓名:" << h1.getName() << endl; //无名氏
cout << "年龄: " << h1.getAge() << endl; //22
cout << "薪资:" << h1.getSalary() << endl; //15000
system("pause");
return 0;
}
拷贝构造函数
拷贝构造函数也分两种:手动定义的和编译器自动合成的
手动定义的拷贝构造函数
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
// 定义一个“人类”
class Human {
public: //公有的,对外的
Human(); //手动定义的“默认构造函数”
Human(int age, int salary);//自定义的构造函数重载
Human(const Human&); //手动定义的拷贝构造函数
string getName();
int getAge();
int getSalary();
private:
string name;
int age = 18;
int salary;
};
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
Human::Human() {
name = "无名氏";
age = 18;
salary = 30000;
}
Human::Human(int age, int salary) {
this->age = age; //this是一个特殊的指针,指向这个对象本身
this->salary = salary;
name = "无名";
}
Human::Human(const Human& man) {
cout << "调用自定义的拷贝构造函数" << endl;
name = man.name;
age = man.age;
salary = man.salary;
}
int main(void) {
Human h1(22,15000); // 使用手动的默认初始化构造函数
Human h2(h1); // 使用自定义的拷贝构造函数
cout << "姓名:" << h2.getName() << endl; //无名
cout << "年龄: " << h2.getAge() << endl; //22
cout << "薪资:" << h2.getSalary() << endl; //15000
system("pause");
return 0;
}
合成的拷贝构造函数
不手动定义拷贝构造函数,编译器会生成“合成的拷贝构造函数”
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
// 定义一个“人类”
class Human {
public: //公有的,对外的
Human(); //手动定义的“默认构造函数”
Human(int age, int salary);//自定义的构造函数重载
const char* getName();
int getAge();
int getSalary();
void setName(const char *name); //对name进行重新赋值
private:
char* name;
int age;
int salary;
};
const char* Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
Human::Human(int age, int salary) {
this->age = age; //this是一个特殊的指针,指向这个对象本身
this->salary = salary;
name = new char[64]; //开辟一块内存
strcpy_s(name,64,"无名");
}
void Human::setName(const char *newname){
if(!newname){
return;
}
strcpy_s(name,64,newname);
}
int main(void) {
Human h1(22,15000); // 使用手动的默认初始化构造函数
Human h2(h1); // 使用合成的拷贝构造函数
cout << "姓名:" << h2.getName() << endl; //无名
h1.setName("剑圣");
cout << "姓名:" << h1.getName() << endl; //剑圣
cout << "姓名:" << h2.getName() << endl; //剑圣
system("pause");
return 0;
}
说明:
合成的拷贝构造函数的缺点: 使用“浅拷贝”,这时拷贝对象(h2)的数据成员并不是独立的会受被拷贝对象(h1)的影响,所以我们如果没有特殊需求一般情况下都使用手动拷贝。
什么时候调用拷贝构造函数
1.调用函数时,实参是对象,形参不是引用类型
如果函数的形参是引用类型,就不会调用拷贝构造函数
2.函数的返回类型是类,而且不是引用类型
3.对象数组的初始化列表中,使用对象。
#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>
using namespace std;
// 定义一个“人类”
class Human {
public:
Human();
Human(int age, int salary);
Human(const Human&);
string getName();
int getAge();
int getSalary();
void setAddr(const char *newAddr);
const char* getAddr();
private:
string name;
int age;
int salary;
};
Human::Human() {
name = "无名氏";
age = 18;
salary = 30000;
}
Human::Human(int age, int salary) {
cout << "调用自定义的构造函数" << endl;
this->age = age; //this是一个特殊的指针,指向这个对象本身
this->salary = salary;
name = "无名";
}
Human::Human(const Human &man) {
cout << "调用自定义的拷贝构造函数" << "参数:" << &man
<< " 本对象:" << this << endl;
age = man.age; //this是一个特殊的指针,指向这个对象本身
salary = man.salary;
name = man.name;
}
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
void test(Human man) {
cout << man.getSalary() << endl;
}
void test2(Human &man) { //不会调用拷贝构造函数,此时没有没有构造新的对象
cout << man.getSalary() << endl;
}
Human test3(Human &man) {
return man;
}
Human& test4(Human &man) {
return man;
}
int main(void) {
Human h1(25, 35000); // 调用默认构造函数
Human h2(h1); // 调用拷贝构造函数
Human h3 = h1; // 调用拷贝构造函数
test(h1); // 调用拷贝构造函数
test2(h1); // 不会调用拷贝构造函数
test3(h1); // 创建一个临时对象,接收test3函数的返回值,调用1次拷贝构造函数
Human h4 = test3(h1); // 仅调用1次拷贝构造函数,返回的值直接作为h4的拷贝构造函数的参数
test4(h1); // 因为返回的是引用类型,所以不会创建临时对象,不会调用拷贝构造函数
Human men[] = { h1, h2, h3 }; //调用3次拷贝构造函数
system("pause");
return 0;
}
赋值构造函数
#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>
using namespace std;
// 定义一个“人类”
class Human {
public:
Human();
Human(int age, int salary);
Human(const Human&);
Human& operator=(const Human &); //赋值构造,也是一个运算符的重载
string getName();
int getAge();
int getSalary();
void setAddr(const char *newAddr);
const char* getAddr();
private:
string name;
int age;
int salary;
};
Human::Human() {
name = "无名氏";
age = 18;
salary = 30000;
}
Human::Human(int age, int salary) {
cout << "调用自定义的构造函数" << endl;
this->age = age; //this是一个特殊的指针,指向这个对象本身
this->salary = salary;
name = "无名";
}
Human::Human(const Human &man) {
cout << "调用自定义的拷贝构造函数" << "参数:" << &man
<< " 本对象:" << this << endl;
age = man.age; //this是一个特殊的指针,指向这个对象本身
salary = man.salary;
name = man.name;
}
string Human::getName() {
return name;
}
int Human::getAge() {
return age;
}
int Human::getSalary() {
return salary;
}
void test(Human man) {
cout << man.getSalary() << endl;
}
void test2(Human &man) { //不会调用拷贝构造函数,此时没有没有构造新的对象
cout << man.getSalary() << endl;
}
Human test3(Human &man) {
return man;
}
Human& test4(Human &man) {
return man;
}
Human& Human::operator=(const Human &man) {
cout << "调用" << __FUNCTION__ << endl;
if (this == &man) {
return *this; //检测是不是对自己赋值:比如 h1 = h1;
}
// 处理其他数据成员
name = man.name;
age = man.age;
salary = man.salary;
// 返回该对象本身的引用, 以便做链式连续处理,比如 a = b = c;
return *this;
int main(void) {
Human h1(25, 35000); // 调用默认构造函数
// 特别注意,此时是创建对象h2并进行初始化,调用的是拷贝构造函数,
// 不会调用赋值构造函数
Human h2 = h1;
h2 = h1; //调用赋值构造函数
h2 = test3(h1); //调用赋值构造函数
Human h3 = test3(h1); //调用拷贝构造函数
system("pause");
return 0;
}
注意:
如果没有定义赋值构造函数,编译器会自动定义“合成的赋值构造函数”,
与其他合成的构造函数,是“浅拷贝”(又称为“位拷贝”)。