C++-面向对象(四)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_42528266/article/details/102751176
命名空间的嵌套

有个默认的全局命名空间,我们创建的命名空间默认都嵌套在它里面

继承

继承,可以让子类拥有父类的所有成员(变量\函数)

  • 关系描述
    • Student是子类(subclass,派生类)
    • Person是父类(superclass,超类)
    • C++中没有像Java、Objective-C的基类
      • Java:java.lang.Object
      • Objective-C:NSObject
#include <iostream>
using namespace std;

//struct Person {
//	int m_age;
//	void run() {
//		cout << "run()" << endl;
//	}
//};
//
//struct Student : Person {
//	int m_score;
//	void study() {
//		cout << "study()" << endl;
//	}
//};
//
//struct Worker : Person {
//	int m_salary;
//	void work() {
//		cout << "work()" << endl;
//	}
//};

// Java:所有的Java对象最终都继承自java.lang.Object这个类
// OC:所有的OC对象最终都继承自NSObject这个类

void test() {
	/*Student student;
	student.m_age = 18;
	student.m_score = 100;
	student.run();
	student.study();*/
}

struct Person {
	int m_age;
};

struct Student : Person {
	int m_no;
};

struct GoodStudent : Student {
	int m_money;
};

int main() {
	// 12
	GoodStudent gs;
	gs.m_age = 20;
	gs.m_no = 1;
	gs.m_money = 2000;

	cout << &gs << endl;
	cout << &gs.m_age << endl;
	cout << &gs.m_no << endl;
	cout << &gs.m_money << endl;

	// 4
	Person person;

	// 8
	Student stu;

	cout << sizeof(Person) << endl;
	cout << sizeof(Student) << endl;
	cout << sizeof(GoodStudent) << endl;

	getchar();
	return 0;
}
成员访问权限
  • 成员访问权限、继承方式有3种 public:公共的,任何地方都可以访问(struct默认)
    • protected:子类内部、当前类内部可以访问
    • prvate:私有的,只有当前类内部可以访问(class默认)
  • 子类内部访问父类成员的权限,是以下2项中权限最小的那个
    • 成员本身的访问权限
    • 上一级父类的继承方式
  • 开发中用的最多的继承方式是public,这样能保留父类原来的成员访问权限
  • 访问权限不影响对象的内存布局
#include <iostream>
using namespace std;

//class Person {
//public:
//	int m_age;
//	void run() {
//
//	}
//};
//
//class Student : public Person {
//	int m_no;
//	void study() {
//		m_age = 10;
//	}
//};
//
//class GoodStudent : public Student {
//	int m_money;
//	void work() {
//		m_age = 10;
//	}
//};

class Person {
private:
	int m_age;
public:
	int m_no;
};

int main() {
	Person person;
	person.m_no = 20;

	getchar();
	return 0;
}
初始化列表
  • 特点
    • 一种便捷的初始化成员变量的方式
    • 只能用在构造函数中
    • 初始化顺序只跟成员变量的声明顺序有关
#include <iostream>
using namespace std;

int myAge() {
	cout << "myAge()" << endl;
	return 30;
}

int myHeight() {
	cout << "myHeight()" << endl;
	return 180;
}

//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;
}
初始化列表和默认参数
#include <iostream>
using namespace std;

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

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/102751176
今日推荐