10.C ++ class inheritance and derived

A succession

Inheritance is an object-oriented programming is the most important concept. Inheritance allows us to define another class based on a class, to function code reuse effect.

When you create a class, class and another class if there is to be created by some common features, programmers do not need to rewrite all the member variables and member functions, you can simply specify inherit another class, called the inherited class base or parent class, the new class is called a derived class or subclass.

Second, the base and derived classes

Define a derived class needs to specify its base class, the following syntax:

class <派生类名>:<继承方式> <基类名>
{
// 派生类类体
};

Inheritance is public, protected or private one of the base class is defined before had the name of a class. If inheritance is not specified, the default is
private.

After the draft, selected Super Girl became maids and princess, ladies and Princess inherited from the Super Girl, where the name, age, height, build, color values ​​and other characteristics, but also has some new features, if a princess, there is the title, place of residence palace, Feng 䘵, eunuchs and maids of honor for her service number and so on. Also, the Princess of age, size and color values ​​are not just discussed, it will lose his head.

Example (book235.cpp)

/*
 * 程序名:book235.cpp,此程序用于演示C++类的继承与派生。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

class CGirl    // 定义超女类
{
public:
  char m_name[50];  // 姓名
  int  m_age;       // 年龄
  int  m_height;    // 身高,单位:厘米cm
  char m_sc[30];    // 身材,火辣;普通;飞机场。
  char m_yz[30];    // 颜值,漂亮;一般;歪瓜裂枣。

  int  Show()       // 显示超女基本信息的成员函数体 
  {
    printf("姓名:%s,年龄:%d,身高:%d,身材:%s,颜值:%s\n",m_name,m_age,m_height,m_sc,m_yz);
  } 
};

class CKCon:public CGirl   // 定义王妃类,从超女类继承
{
public:
  char m_ch[50];      // 称号
  char m_palace[50];  // 居住的宫殿
  int  m_sal;         // 奉禄

  int  Show()     // 显示王妃的称号、宫殿和奉禄
  {
    printf("姓名:%s,称号:%s,栖:%s,奉禄:%d两银子。\n",m_name,m_ch,m_palace,m_sal);
  }
};

int main()
{
  CKCon KCon;   // 实例化一个KCon对象

  strcpy(KCon.m_name,"杨玉环");
  KCon.m_age=28;      
  KCon.m_height=168;
  strcpy(KCon.m_sc,"火辣");
  strcpy(KCon.m_yz,"漂亮");
  strcpy(KCon.m_ch,"杨贵妃");
  strcpy(KCon.m_palace,"华清宫");
  KCon.m_sal=10000;

  KCon.Show();   // 如果子类中没有Show函数,就会调用父类的Show函数。
}

running result

Here Insert Picture Description

Third, inheritance

When a class derived from the base class, the base class can be inherited as public, protected, or private ways, when using different types of inheritance, follow the following rules:

1) public inheritance (public): When a class derived inheritance to public way, public members of the base class is derived public members of the class, protected members of the base class is derived to protect members of the class, the private members of the base class can not be directly derived class to access, but can be accessed by calling the public and protected members of the base class.

2) protected inheritance (protected): When a derived class inherits in a protected way, the public and protected members of the base class become protected members of the derived class.

3) private inheritance (private): When a derived class to inherit private way, the public and protected members of the base class become private members of the derived class.

We hardly use protected or private inheritance is usually used public inheritance.

Fifth, the base class and a derived class pointer

Base class pointer can point to an object base class, a derived class can also point to an object, but can not be accessed by members of the derived class pointer to the base class.

Pointer can point to a derived class of the derived class objects, but not a base class object.

Sixth, multiple inheritance

Multiple Inheritance i.e., a derived class may have a plurality of base class that inherits the characteristics of the plurality of base classes.

C ++ class members can inherit from multiple base classes, syntax is as follows:

class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,{
// 派生类类体
};

Wherein inheritance is public, protected, or private wherein one, each used to modify the base class, separated by commas base class.

Seven, application experience

Inheritance and most often in the derived class with windows development environment, with less than in the Linux environment, so many years, I only occasionally use it, as long as you understand the basic usage of inheritance and derived on the line, if in the actual development really to use class inheritance, studies come too late, that no technical difficulty.

Eight, Homework

Write the sample program, this section describes the knowledge of all the demo again, the demo program can deepen understanding and mapping.

Nine, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 53 original articles · won praise 6 · views 1803

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104673808