[C/C++]继承

公有继承 class Worker:public Person

练习题:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

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

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

int main(void)
{
    // 创建Soldier对象
	Soldier soldier;
    // 给对象属性赋值
    soldier.m_strName = "Jim";
	soldier.m_strCode = "592";
    // 打印对象属性值
	cout << soldier.m_strName << endl;
	cout << soldier.m_strCode << endl;
    // 调用对象方法
	soldier.eat();
	soldier.attack();

	return 0;
}


保护继承 class Worker:protected Person


私有继承 class Worker:private Person


继承中的特殊关系

隐藏 (相当于 JAVA 的 重写)

注意:子类函数与父类名称相同即构成隐藏,跟参数没有关系

练习题:

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

多重继承

class Person {}
class Worker:public Person{}
class Soldier:public Worker{}

多继承

class Person{}
class Worker{}
class Soldier : public Person, public Worker {}

练习题:
#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 *pc = new ChildLabourer("小明", 12);
    // 通过童工对象调用父类的work()和play()方法
	pc->work();
    pc->play();
    // 释放
    delete pc;
    pc = NULL;

	return 0;
}

虚继承 virtual

class A : virtual public B {}
class C : virtual public B {}

解决父类被多个子类继承,造成重定义问题的方法:
#ifndef PERSON_H
#define PERSON_H
#pragma once
#include <string>
using namespace std;

class Person
{
public:
	Person();
	~Person();
	void eat();
	string m_strName;
	string m_iAge;
};

#endif // !PERSON_H

#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("alex", 12);
    // 调用童工类对象各方法。
	p->eat();
	p->work();
	p->play();
	delete p;
	p = NULL;

	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值