day08_类的初始化

初始化列表

  • 特点
    • 一种便捷的初始化成员变量的方式
    • 只能用在构造函数中
    • 初始化顺序只跟成员变量的声明顺序有关
struct Person {
	int m_age;
	int m_height;

	Person() {
		this->m_age = 0;
		this->m_height = 0;
	}

	// 初始化列表 :m_age(age), m_height(height)
	/*Person(int age, int height) :m_height(height), m_age(m_height)   {

	}*/
	Person(int age, int height) :m_height(myHeight()), m_age(myAge()) {

	}

	void display() {
		cout << "m_age is " << this->m_age << endl;
		cout << "m_height is " << this->m_height << endl;
	}
};


struct Person {
	int m_age;
	int m_height;

	//Person() :Person(0, 0) {
	//	cout << "Person() " << this << endl;

	//	// 直接调用构造函数,会创建一个临时对象,传入一个临时的地址值给this指针
	//	// Person(0, 0);
	//}

	Person() :Person(0, 0) { }
	Person(int age, int height) :m_age(age), m_height(height) { }

	/*Person(int age, int height) {
		cout << "Person(int age, int height) " << this << endl;

		this->m_age = age;
		this->m_height = height;
	}*/

	void display() {
		cout << "m_age is " << this->m_age << endl;
		cout << "m_height is " << this->m_height << endl;
	}
};

int main() {
	Person person;
	// person.Person();
	person.display();

	/*Person person2(10, 20);
	person2.display();*/


	/*Person person1;
	person1.display();

	cout << "-----------------" << endl;*/


	getchar();
	return 0;
}

初始化列表与默认参数配合使用

  • 如果函数声明和实现是分离的
    • 初始化列表只能写在函数的实现中
    • 默认参数只能写在函数的声明中
class Person {
	int m_age;
	int m_height;
public:
	/*Person() :Person(0, 0) {

	}

	Person(int age) :Person(age, 0) {

	}*/

	// 默认参数只能写在函数的声明中
	Person(int age = 0, int height = 0);
};

// 构造函数的初始化列表只能写在实现中
Person::Person(int age, int height) :m_age(age), m_height(height) {

}

int main() {
	Person person;
	Person person2(10);
	Person person3(20, 180);

	getchar();
	return 0;
}

父类的构造函数

  • 子类的构造函数默认会调用父类的无参构造函数
  • 如果子类的构造函数显式地调用了父类的有参构造函数,就不会再去默认调用父类的无参构造函数
  • 如果父类缺少无参构造函数,子类的构造函数必须显式调用父类的有参构造函数
//class Person {
//	int m_age;
//public:
//	Person() {
//		cout << "Person()" << endl;
//	}
//	Person(int age) :m_age(age) {
//		cout << "Person(int age)" << endl;
//	}
//};
//
//class Student : public Person {
//	int m_score;
//public:
//	Student() {
//		cout << "Student()" << endl;
//	}
//	Student(int age, int score) :m_score(score), Person(age) {
//		cout << "Student(int age, int score)" << endl;
//	}
//};

class Person {
	int m_age;
public:
	Person(int age) :m_age(age) {
		cout << "Person(int age)" << endl;
	}
};

class Student : public Person {
	int m_score;
public:
	Student() :Person(0) {

	}
};

int main() {
	Student student;

	getchar();
	return 0;
}

构造、析构顺序

  • 构造和析构顺序相反

父类指针

  • 父类指针可以指向子类对象,是安全的,开发中经常用到(继承方式必须是public)
  • 子类指针指向父类对象是不安全的
using namespace std;

class Person {
public:
	int m_age;
};

// Student是一个Person
class Student : public Person {
public:
	int m_score;
};

int main() {
	// 学生是一个人
	//Person *stu = new Student();
	//stu->m_age = 10;
	
	/*Student *p = (Student *) new Person();
	p->m_age = 10;
	p->m_score = 20;*/

	getchar();
	return 0;
}

posted @ 2021-04-18 16:09  AAAAAAAAA123123  阅读(41)  评论(0编辑  收藏  举报