C++中Inheritance的使用

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class,instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the "is a" relationship.

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes.

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

A derived class inherits all base class methods with the following exceptions:

(1)、Constructors,destructors and copy constructors of the base class.

(2)、Overloaded operators of the base class.

(3)、The friend functions of the base class.

(4)、Private members

Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

Private Inheritance:When deriving from a private base class, public and protected members of the base class become private members of the derived class.

A C++ class can inherit members from more than one class.

C++ implements inheritance through the mechanism of derivation. Derivation allows you to reuse code by creating new classes, called derived classes, that inherit properties from one or more existing classes, called base classes. A derived class inherits the properties, that includes data and function members, of its base class. You can also add new data members and member functions to the derived class. You can modify the implementation of existing member functions or data by overriding base class member functions or data in the newly derived class.

There are five different inheritances supported in C++: Simple/ Single;Multilevel;Hierarchical;Multiple;Hybrid.

继承有三种方式:公有(public)继承、私有(private)继承、保护(protected)继承。

派生类构造函数的执行顺序:先调用基类的构造函数,再调用派生类构造函数;基类构造函数的调用顺序是按照声明派生类时基类出现的顺序。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "inheritance.hpp"
#include <iostream>

///
// reference: http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
// Base class
class Shape
{
public:
	void setWidth(int w)
	{
		width = w;
	}
	void setHeight(int h)
	{
		height = h;
	}
protected:
	int width;
	int height;
};

// Derived class
class Rectangle : public Shape
{
public:
	int getArea()
	{
		return (width * height);
	}
};

int test_inheritance1()
{
	Rectangle Rect;

	Rect.setWidth(5);
	Rect.setHeight(7);

	// Print the area of the object.
	std::cout << "Total area: " << Rect.getArea() << std::endl;

	return 0;
}

///
// reference: http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
// Base class PaintCost
class PaintCost
{
public:
	int getCost(int area)
	{
		return area * 70;
	}
};

// Derived class
class Rectangle_2 : public Shape, public PaintCost
{
public:
	int getArea()
	{
		return (width * height);
	}
};

int test_inheritance2()
{
	Rectangle_2 Rect;
	int area;

	Rect.setWidth(5);
	Rect.setHeight(7);

	area = Rect.getArea();

	// Print the area of the object.
	std::cout << "Total area: " << Rect.getArea() << std::endl;

	// Print the total cost of painting
	std::cout << "Total paint cost: $" << Rect.getCost(area) << std::endl;

	return 0;
}

/
// reference: http://www.cplusplus.com/doc/tutorial/inheritance/
class Mother {
public:
	Mother()
	{
		std::cout << "Mother: no parameters\n";
	}
	Mother(int a)
	{
		std::cout << "Mother: int parameter\n";
	}
};

class Daughter : public Mother {
public:
	Daughter(int a)
	{
		std::cout << "Daughter: int parameter\n\n";
	}
};

class Son : public Mother {
public:
	Son(int a) : Mother(a)
	{
		std::cout << "Son: int parameter\n\n";
	}
};

int test_inheritance3()
{
	/* print: Mother: no parameters
		  Daughter: int parameters
	*/
	Daughter kelly(0);

	/* print: Mother: int parameters
		  Son: int parameters
	*/
	Son bud(0);

	return 0;
}

//
// reference: http://www.dev-hq.net/c++/16--inheritance-and-friends
class Fish
{
protected:
	int speed;
public:
	Fish() : speed(0) {}
	void speed_up() { speed += 10; }
	void slow_down() { speed -= 10; }
	void output() const { std::cout << speed << std::endl; }
};

class Cod : public Fish
{
public:
	Cod() { speed = 10; } //Cods start with a speed of 10
	void speed_up() { speed += 20; } //Overwrite base 'speed_up' member function
};

int test_inheritance4()
{
	Cod bob; //A new "Cod"
	bob.speed_up();
	bob.output(); // 10

	Fish freddie; //A new "Fish" (not a "Cod"!)
	freddie.speed_up();
	freddie.output(); // 30

	return 0;
}

GitHubhttps://github.com/fengbingchun/Messy_Test

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值