C++学习笔记之继承

1、父类中的私有成员,被子类公有继承之后,父类的私有成员在子类中被隐藏,是不可见的,所以无法在子类中的成员函数中直接访问父类的私有成员数据,当然更无法通过实例化对象来访问了。

父类中的成员属性,通过公有继承方式被子类继承:

private--》无法访问

protected--》protected

public--》public

父类中的成员属性,通过私有继承方式被子类继承:

private--》无法访问

protected--》private

public--》private

父类中的成员属性,通过保护继承方式被子类继承:

private--》无法访问

protected--》protected

public--》protected//protected类只能在类中通过成员函数进行访问,在子类实例化为对象之后是是无法再访问该成员的,因为已经从public转为protected。

2、父类和子类中同名的成员函数,则在子类中将父类的同名函数进行隐藏。若想访问父类的同名函数,子类对象名.父类名::同名函数()。

注意,即使两个同名函数的参数不同,也会在子类中对该同名函数进行隐藏,欲访问父类的同名函数,还是需要通过::进行访问的。同名函数和同名变量是一样的。

例子代码如下:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

/**
 * 定义人类: Person
 * 数据成员: m_strName
 * 成员函数: attack()
 */
class Person
{
public:
    string m_strName;
	void attack()
	{
		cout << "attack" << endl;
	}
};

/**
 * 定义士兵类: Soldier
 * 士兵类公有继承人类
 * 数据成员: m_strName
 * 成员函数: attack()
 */
class Soldier:public Person
{
public:
	string m_strName;
	void attack()
	{
		cout << "fire!!!" << endl;
	}
};

int main(void)
{
    // 实例士兵对象
	Soldier soldier;
    // 向士兵属性赋值"tomato"
    soldier.m_strName = "tomato";
    // 通过士兵对象向人类属性赋值"Jim"
	soldier.Person::m_strName = "Jim";
    // 打印士兵对象的属性值
	cout << soldier.m_strName << endl;
    // 通过士兵对象打印人类属性值
	cout << soldier.Person::m_strName << endl;
    // 调用士兵对象方法
	soldier.attack();
    // 通过士兵对象调用人类方法
    soldier.Person::attack();//注意这里

	return 0;
}

2、多继承

代码:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

/**
 * 定义工人类: Worker
 * 数据成员: m_strName
 * 成员函数: work()
 */
class Worker
{
public:
    Worker(string name)
	{
		m_strName = name;
		cout << "Worker" << endl;
	}
	~Worker()
	{
		cout << "~Worker" << endl;
	}
	void work()
	{
		cout << m_strName << endl;
		cout << "work" << endl;
	}
protected:
	string m_strName;
};

/**
 * 定义儿童类: Children
 * 数据成员: m_iAge
 * 成员函数: play()
 */
class Children
{
public:
	Children(int age)
	{
		m_iAge = age;
		cout << "Children" << endl;
	}	
	~Children()
	{
		cout << "~Children" << endl;
	}	
	void play()
	{
		cout << m_iAge << endl;
		cout << "play" << endl;
	}
protected:
	int m_iAge;
};

/**
 * 定义童工类: ChildLabourer
 * 公有继承工人类和儿童类
 */
class ChildLabourer :public Worker,public Children
{
public:
	ChildLabourer(string name, int age):Worker(name),Children(age)
	{
		cout << "ChildLabourer" << endl;
	}

	~ChildLabourer()
	{
		cout << "~ChildLabourer" << endl;
	}	
};

int main(void)
{
    // 使用new关键字创建童工类对象
	ChildLabourer *p=new ChildLabourer("Jim",10);
    // 通过童工对象调用父类的work()和play()方法
	p->work();
    p->play();
    // 释放
    delete p;
    p=NULL;
	return 0;
}
运行结果:



3、虚继承

解决的多继承中数据的冗余问题。当多继承中存在菱形继承关系时,需要注意使用虚继承。

class work:public Person

{...}

class farmer:public Person

{...}

class farmerwork:public work,public farmer

{....}

这样在farmerwork中存在两份的person,为了保险起见,需要采用虚基类的定义方式如下:

class work:virtual public Person

{。。。}

class farmer:virtual public Person

{。。。}

示例代码:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

/**
 * 定义人类: Person
 */
class Person
{
public:
    Person()
	{
		cout << "Person" << endl;
	}
	~Person()
	{
		cout << "~Person" << endl;
	}
	void eat()
	{
		cout << "eat" << endl;
	}

};

/**
 * 定义工人类: Worker
 * 虚继承人类
 */
class Worker :virtual public Person
{
public:
	Worker(string name)
	{
		m_strName = name;
		cout << "Worker" << endl;
	}
	~Worker()
	{
		cout << "~Worker" << endl;
	}
	void work()
	{
		cout << m_strName << endl;
		cout << "work" << endl;
	}
protected:
	string m_strName;
};

/**
 * 定义儿童类:Children
 * 虚继承人类
 */
class Children : virtual public Person
{
public:
	Children(int age)
	{
		m_iAge = age;
		cout << "Children" << endl;
	}	
	~Children()
	{
		cout << "~Children" << endl;
	}	
	void play()
	{
		cout << m_iAge << endl;
		cout << "play" << endl;
	}
protected:
	int m_iAge;
};

/**
 * 定义童工类:ChildLabourer
 * 公有继承工人类和儿童类
 */
class ChildLabourer: public Worker,public Children
{
public:
	ChildLabourer(string name, int age):Worker(name),Children(age)
	{
		cout << "ChildLabourer" << endl;
	}

	~ChildLabourer()
	{
		cout << "~ChildLabourer" << endl;
	}	
};

int main(void)
{
    // 用new关键字实例化童工类对象
	ChildLabourer *p=new ChildLabourer("Jim",10);
    // 调用童工类对象各方法。
	p->eat();
	p->work();
	p->play();
	delete p;
	p = NULL;

	return 0;
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值