Inherited characteristics [C ++] class and achieve

Object-oriented programming is the most important concept is inherited. Another class inheritance allows us to define a class basis, which makes creating and maintaining an application easier. In doing so, also reached the reuse of code functionality and improve the efficiency of the effect.

When you create a class, you do not need to re-write new data members and member functions, simply specify the new class inherits members to an existing class. The existing class is called the base class , the new class is called the derived class .

Inheritance represents is a relationship. For example, the mammal is an animal, a dog is a mammal, therefore, the dog is an animal, and so on.

Base class & derived class

A class can be derived from more than one class, which means that it can inherit data and functions from multiple base classes. The definition of a derived class, we use a derived class list to specify the base class. Class derivation list of one or more base classes named in the following form:

class derived-class: access-specifier base-class

Among them, the access modifier access-specifier is public, protected or private One, base-class is the name of a previously defined through the class. If no access modifier access-specifier, the default is private.

Suppose there is a base class for the Shape , the Rectangle its derived classes, as follows:

Examples

  . 1    2 // jet.h / jet.cpp   // derived from the base class inherits airplan airplan and data interfaces   . 3 #pragma Once
   . 4 #ifndef __jet_h__
   . 5 #define __jet_h__
   . 6 #include <the iostream>
   . 7 #include " airplane.h "   . 8 namespace apdance
   . 9 {
 10 class Jet: public Airplane // inherited from Airplane . 11 {
 12 is public :
 13 is     Jet ();
 14      ~ Jet ();
 
   
   
    15     void set_weapons(int w);
 16     int get_weapons();
 17         
 18 private:
 19     int weapons;
 20 };
 21  22 }
 23 #endif
 24  25 #include "stdafx.h"
 26 #include <iostream>
 27 #include "jet.h"
 28  29 namespace apdance
 30 {
 31     jet::jet()
 32     {
 33         weapons = 1;
 34         std::cout << "jet take off!\n";
 35     }
 36     jet::~jet()
 37     {
 38         std::cout << "jet down!may day may day!";
 39     }
 40     void jet::set_weapons(int w)
 41     {
 42         weapons = w;
 43     }
 44     int jet::get_weapons()
 45     {
 46         return weapons;
 47     }
 48 }//namespace apdance
 49  50  51 //airplan.h / airplan.cpp 
 52 #pragma once
 53 #ifndef __AIRPLANE_H__
 54 #define __AIRPLANE_H__
 55 #include <iostream>
 56 namespace apdance //定义命名空间
 57 {
 58 class airplane //创建基类airplane
 59 {
 60 public:
 61     airplane()
62 is      {
 63 is          Wheels = . 3 ;
 64          WINGS = 2 ;
 65          Engines = . 1 ;
 66          STD :: COUT << " created objects \ n- " ;
 67      }
 68      ~ Airplane ()
 69      {
 70          STD :: COUT << " Object deleted \ n- " ;
 71 is      }
 72  public :
 73 is      void set_wings ( int W);
 74      int get_wings ();
 75     void set_wheels(int w);
 76     int get_wheels();
 77     void set_engines(int w);
 78     int get_engines();
 79     void fly();
 80 private:
 81     int wings;
 82     int wheels;
 83     int engines;
 84 };
 85 }//namespace
 86 #endif // !___AIRPLANE_H__
 87 #include "stdafx.h"
 88 #include <iostream>
 89 #include "airplane.h"
 90 namespace apdance
 91 {
 92 void airplane::set_wings(int w)
 93 {
 94     wings = w;
 95 }
 96 int airplane::get_wings()
 97 {
 98     return wings;
 99 }
100 void airplane::set_wheels(int w)
101 {
102     wheels = w;
103 }
104 int airplane::get_wheels()
105 {
106     return wheels;
107 }
108 void airplane::set_engines(int w)
109 {
110     engines = w;
111 }
112 int airplane::get_engines()
113 {
114     return engines;
115 }
116 void airplane::fly()
117 {
118     std::cout << "iam fly now!\n";
119 }
120 }// namespace 
121  122 123 // cpp_learning.cpp: Defines the entry point console application. Test No problem 124 #include " the stdafx.h " 125 #include " the iostream " 126 #include " airplane.h " 127 #include " jet.h " 128 int main ()
 129 {
 130.     apdance Jet X ::;
 131 is      X. set_engines ( 100 );
 132      STD :: << x.get_engines COUT () << ' \ n- '  




   ;
133     return 0;
134 }

 

Access control and inheritance

Derived class can access all non-private members of the base class. So you do not want the base class member function is a member of the derived class access should be declared as private in the base class.

We can summarize the different access rights according to the type of access, as follows:

access public protected private
The same class yes yes yes
Derived class yes yes no
Outside of class yes no no

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

  • Base class constructor, destructor, and copy constructor.

  • Operator overloading the base class.

  • Friend base class function.

Type of inheritance

When a class derived from the base class, the base class can be inherited as public, protected , or private types. Through the above explanation is inherited type of access modifier to specify the access-specifier.

We hardly use protected or private inheritance is usually used public inheritance. When different types of inheritance, follow the following rules:

  • Public inheritance (public): When a class is derived from the public base class, the base class's public member of the derived class public members of the base class to protect a member of the derived class to protect members of the base class's private members can not be directly derived class access, but you can call the base class public and to protect access to members.

  • Protected inheritance (protected): When a class is derived from protecting base class, the base class public and protection of the members of the derived class will be to protect members.

  • Private inheritance (private): When a class is derived from the private base class, the base class public and protection of the members of the derived class will become private members.

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 members from more than one class, the syntax is as follows:

class <derived class name>: <Inheritance Embodiment 1> <1 base class name>, <2 inheritance> <base class name 2>, ... 
{
<derived class class body>
};

Wherein inheritance is the access modifier public, protected , or private wherein one, each used to modify the base class, separated by commas base class, as shown above. Now let us look at the following example with:

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 (); 
   
   // area output target 
   COUT << " the Total Area: " << Rect.getArea () << endl; 
 
   // output total cost 
   COUT << " the 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

Guess you like

Origin www.cnblogs.com/zeolim/p/12335338.html