c++类和对象---继承

2023-05-16

继承是面向对象三大特性之一

1、继承的基本语法

语法:

class 子类名 :继承方式 父类名

子类 也叫 派生类

父类 也叫 基类

class basepage
{
public:
	void header()
	{
		cout << "首页,公开课、登录、注册..." << endl;
	}
	void footer()
	{
		cout << "帮助、交流、地图..." << endl;
	}
	void left()
	{
		cout << "java、c++、python..." << endl;
	}
};
class java : public basepage   //继承,减少重复代码
{
public:
	void content()
	{
		cout << "java学科" << endl;
	}
};


int main()
{
	
	
	system("pause");
	return 0;
}

2、继承方式

继承方式有三种

1、公共继承

2、保护继承

3、私有继承

 

class basepage1
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};
class basepage2
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class basepage3
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class son1 : public basepage1  //继承,减少重复代码
{
public:
	void func()
	{
		m_A = 10;
		m_B = 20;
		//m_C = 30;//报错,父类私有属性不可访问
	}
};

class son2 : protected basepage2  //继承,减少重复代码
{
public:
	void func()
	{
		m_A = 10;
		m_B = 20;
		//m_C = 30;//报错,父类私有属性不可访问
	}
};

class son3 : private basepage3  //继承,减少重复代码
{
public:
	void func()
	{
		m_A = 10;
		m_B = 20;
		//m_C = 30;//报错,父类私有属性不可访问
	}
};

void test01()
{
	son1 s1;
	s1.m_A = 100;//子类公共继承父类公共属性,可访问
	//s1.m_B = 200;报错,子类继承父类保护属性,不可访问
}

void test02()
{
	son2 s1;
	//s1.m_A = 100;//子类保护继承父类公共属性,不可访问
	//s1.m_B = 200;报错,子类保护继承父类保护属性,不可访问
}
void test03()
{
	son2 s1;
	//s1.m_A = 100;//子类私有继承父类公共属性,不可访问
	//s1.m_B = 200;//报错,子类私有继承父类保护属性,不可访问
}


int main()
{
	
	
	system("pause");
	return 0;
}

 3、继承中的对象模型

从父类继承过来的成员,那些属于子类对象?

class basepage1
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class son1 : public basepage1  //父类中所有的非静态成员属性都会被子类继承下去,包括私有成员
							   //只是被编译器隐藏了,不可以访问,但是会继承;
{
public:
	int m_D;
};

void test01()
{
	cout << "size of son1 = " << sizeof(son1) << endl;//sizeof(son1) = 16
}



int main()
{
	test01();
	
	system("pause");
	return 0;
}

4、继承中构造和析构顺序

继承中,先调用父类构造函数,再调用子类构造函数,然后调用子类析构函数,最后调用父类析构函数。

class basepage1
{
public:
	basepage1()
	{
		cout << "base构造函数" << endl;
	}
	~basepage1()
	{
		cout << "base析构函数" << endl;
	}
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class son1 : public basepage1  
{
public:
	son1()
	{
		cout << "son构造函数" << endl;
	}
	~son1()
	{
		cout << "son析构函数" << endl;
	}
public:
	int m_D;
};

void test01()
{
	 son1 s1;
}



int main()
{
	test01();
	
	system("pause");
	return 0;
}
base构造函数
son构造函数
son析构函数
base析构函数
请按任意键继续. . .

5、继承同名成员处理方式

1、访问子类同名成员,直接访问

2、访问父类同名成员,加作用域

class basepage1
{
public:
	basepage1()
	{
		m_A = 100;
		m_B = 300;
	}
	int m_A;
	int m_B;
	void func()
	{
		cout << "base func" << endl;
	}
protected:
	
private:
	int m_C;
};

class son1 : public basepage1  
{
public:
	son1()
	{
		m_A = 200;
	}
	void func()
	{
		cout << "son func" << endl;
	}
	
public:
	int m_A;
};

void test01()
{
	 son1 s1;
	 cout << "son m_A =" << s1.m_A << endl;
	 cout << "base m_A = " << s1.basepage1::m_A << endl;
	 cout << "son m_B = " << s1.basepage1::m_B << endl;
}
void test02()
{
	son1 s2;
	s2.func();
	s2.basepage1::func();

}


int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
son m_A =200
base m_A = 100
son m_B = 300
son func
base func
请按任意键继续. . .

6、继承中同名静态成员处理方式

静态成员和非静态成员出现同名,处理方式一样,只不过有两种访问方式,一种通过类名访问

一种通过对象访问。

class basepage1
{
public:
	
	static int m_A;
	int m_B;
	static void func()
	{
		cout << "base func" << endl;
	}
protected:
	
private:
	int m_C;
};
int basepage1::m_A = 100;

class son1 : public basepage1  
{
public:
	son1()
	{
		m_A = 200;
	}
	static void func()
	{
		cout << "son func" << endl;
	}
	
public:
	static int m_A;
};
int son1::m_A = 200;

void test01()
{
	//通过类名访问
	cout << son1::m_A << endl;
	cout << son1::basepage1::m_A << endl;
	//通过对象访问
	 son1 s1;
	 cout << "son m_A =" << s1.m_A << endl;
	 cout << "base m_A = " << s1.basepage1::m_A << endl;
}
void test02()
{
	//通过类名访问
	son1::func;
	son1::basepage1::func;
	//通过对象访问
	son1 s2;
	s2.func();
	s2.basepage1::func();

}


int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

7、多继承语法

c++允许一个类继承多个类

语法:class 子类名 :继承方式 父类1,继承方式 父类2,....

多继承可能会引发父类中有同名成员出现,需要加作用域区分

c++实际开发中不建议用多继承

class basepage1
{
public:
	
	int m_A = 200;
	int m_B;
	void func()
	{
		cout << "base1 func" << endl;
	}
protected:
	
private:
	int m_C;
};
class basepage2
{
public:

	int m_A = 300;
	int m_B;
	void func()
	{
		cout << "base2 func" << endl;
	}
protected:

private:
	int m_C;
};


class son1 : public basepage1,public basepage2
{
public:
	son1()
	{
		m_A = 100;
	}
	void func()
	{
		cout << "son func" << endl;
	}
	
public:
	int m_A;
};


void test01()
{
	 son1 s1;
	 cout << "son m_A =" << s1.m_A << endl;
	 cout << "base1m_A = " << s1.basepage1::m_A << endl;
	 cout << "base2m_A = " << s1.basepage2::m_A << endl;
}
void test02()
{
	
	son1 s2;
	s2.func();
	s2.basepage1::func();
	s2.basepage2::func();

}


int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

8、菱形继承

菱形继承概念:
两个派生类继承同一个基类

又有某个类同时继承两个派生类

这种继承被称为菱形继承,或者钻石继承

class animal
{
public:
	int a_age;	
};
//继承之前加virtual关键字后,变为虚继承
//此时公共的父类animal称为虚基类
class sheep : virtual public animal
{
	
};
class tuo : virtual public animal
{
	
};


class sheepTuo : public sheep,public tuo
{

};

void test01()
{
	//此时st中的a_age都是同一个
	sheepTuo st;
	st.sheep::a_age = 100;
	st.tuo::a_age = 200;
	cout << "st.sheep::a_age = " << st.sheep::a_age << endl;
	cout << "st.tuo::a_age = " << st.tuo::a_age << endl;
	cout << "st.a_age = " << st.a_age << endl;
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c++类和对象---继承 的相关文章

随机推荐