Latest web development tutorials

C ++ inheritance

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.

& Derived class base class

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

Access control and inheritance

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:

访问publicprotectedprivate
同一个类yesyesyes
派生类yesyesno
外部的类yesnono

A derived class inherits all the base class method, except in the following circumstances:

  • Base class constructor and destructor and copy constructor.
  • The base class's overloaded operators.
  • Friend of the base class function.

Type of inheritance

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:

  • Public inheritance (public): protectedmembers when a class is derived from apublicbase class,publicmembers of the base class is derivedpublicmembers of a class,protectedmembers of the base class is the derived class,the privatemembers of a base class can not be directly derived class access, but can be accessed by callingthepublic andprotectedmembers of the base class.
  • Protected inheritance (protected): When a class derived from the base class protection,the public andprotectedmembers of the base class will becomeprotectedmembersofthe derived class.
  • Private inheritance (private): When a class is derived from a base class private,public andprotectedmembers of the base class will beprivatemembers of the derived class.

Multiple Inheritance

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