day6 黑马二十四期

1.继承的引出

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
#include<stdlib.h>//用system("cls")清屏
#include<conio.h>//用于键盘操作
#include<stack>//栈
#include<queue>
#include <ctime>
#include <Windows.h>
using namespace std;
class Person {//人类
private:
	int age;
	string sex;
public:
	Person() {
		age = 18;
		sex = "man";
	}
	Person(int a, string b) {
		this->age = a;
		this->sex = b;
	}
	void information() {//打印个人信息
		cout << age << endl;
		cout << sex << endl;
	}
};
class student:public Person//公有继承自Person类
{
public:
	student() {
		number = "123456";//学号
	}
	void shownumber() {
		cout << number << endl;
	}
private:
	string number;
};

int main() {
	student s1;
	s1.information();
	s1.shownumber();
}

2.子类与父类中有同名属性的调用办法

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
#include<stdlib.h>//用system("cls")清屏
#include<conio.h>//用于键盘操作
#include<stack>//栈
#include<queue>
#include <ctime>
#include <Windows.h>
using namespace std;
class Person {//人类
private:
	int age;
	string sex;
public:
	Person() {
		age = 18;
		sex = "man";
	}
	Person(int a, string b) {
		this->age = a;
		this->sex = b;
	}
	void information() {//打印个人信息
		cout << age << endl;
		cout << sex << endl;
	}
	int number = 123;
	void shownumber() {
		cout << number << endl;
	}
};
class student:public Person//公有继承自Person类
{
public:
	student() {
		number = "123456";//学号
	}
	void shownumber() {
		cout << number << endl;
	}
private:
	string number;
};

int main() {
	student s1;
	s1.information();
	s1.shownumber();
	s1.Person::shownumber();
	cout << s1.Person::number;//同名属性
}

3.多继承方式

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
#include<stdlib.h>//用system("cls")清屏
#include<conio.h>//用于键盘操作
#include<stack>//栈
#include<queue>
#include <ctime>
#include <Windows.h>
using namespace std;
class B1 {
public:
	int m1;
};
class B2 {
public:
	int m2;
};
class A1 :public B1, public B2 {
public:
	int m3;
};
int main() {
	A1* a = new A1;
	a->m1 = 1;
	a->m2 = 2;
	a->m3 = 3;
}

4.菱形继承问题的解决

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
#include<stdlib.h>//用system("cls")清屏
#include<conio.h>//用于键盘操作
#include<stack>//栈
#include<queue>
#include <ctime>
#include <Windows.h>
using namespace std;
class A
{
public:
	int _num;
};
class B1 : virtual public A
{

};
class B2 : virtual public A
{

};
class C : public B1, public B2
{

};
int main()
{
	C c;
	cout << sizeof(c) << endl;
	c.B1::_num = 1;
	c.B2::_num = 2;
	cout << "c.B1::_num:" << c.B1::_num << endl;
	cout << "c.B2::_num:" << c.B2::_num << endl;
	cout << "c._num:" << c._num << endl;
	return 0;
}
发布了70 篇原创文章 · 获赞 39 · 访问量 2232

猜你喜欢

转载自blog.csdn.net/weixin_45221477/article/details/105310878