HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Object-oriented programming is the most important concept is inherited. Inheritance allows us to define a class to another class basis, which makes creating and maintaining an application easier. To do so, also reached the reuse of code functionality and improve the execution time results.
When you create a class, you do not need to re-write new data members and member functions, simply specify the new class inherits an existing member of the class can be. The existing class is calledthe base class, the new class is called the derived class.
Inheritanceis a representative of the relationship.For example, the mammal is an animal, a dog is a mammal, therefore, a dog is an animal, and so on.
A class can be derived from more than one class, which means that it can inherit data and functions from multiple base classes. Defines a derived class, we use a derived class list to specify a base class. Class derivation list to one or more base class named the following form:
class derived-class: access-specifier base-class
Wherein the access modifier access-specifier ispublic, protected or privateone, base-class is defined before the name of a class. If no access modifier access-specifier, the default is private.
Suppose you have a base classShape, Rectangleits derived classes, as follows:
#include <iostream> using namespace std; // 基类 class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // 派生类 class Rectangle: public Shape { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // 输出对象的面积 cout << "Total area: " << Rect.getArea() << endl; return 0; }
When the above code is compiled and executed, it produces the following results:
Total area: 35
Derived class can access the base class for all non-private members. So if you want to be a member of a base class member functions of the derived class access should be declared in the base class is private.
We can summarize the different access rights according to the type of access, as follows:
访问 | public | protected | private |
---|---|---|---|
同一个类 | yes | yes | yes |
派生类 | yes | yes | no |
外部的类 | yes | no | no |
A derived class inherits all the base class method, except in the following circumstances:
When a class is derived from the base class, the base class can be inherited aspublic, protected or privatetypes. Through the above explanation is inherited type of access modifier access-specifier to specify.
We hardly useprotected or privateinheritance is usually usedpublicinheritance. When different types of inheritance, follow the following rules:
Multiple inheritance that is, a subclass can have more than one parent class that inherits the characteristics of more than one parent class.
C ++ class can inherit from more than one class member the following syntax:
class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,… { <派生类类体> };
Wherein the access modifier inheritance ispublic, protected or privateone, used to decorate each base class, the base class between each separated by a comma, as shown above. Now let us look at the following examples:
#include <iostream> using namespace std; // 基类 Shape class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // 基类 PaintCost class PaintCost { public: int getCost(int area) { return area * 70; } }; // 派生类 class Rectangle: public Shape, public PaintCost { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // 输出对象的面积 cout << "Total area: " << Rect.getArea() << endl; // 输出总花费 cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; }
When the above code is compiled and executed, it produces the following results:
Total area: 35 Total paint cost: $2450