The concept of inheritance

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/dxd_123456/article/details/78066877

1, before the first of several talk inheritance relationships between classes
1) comprises the relationship, to describe a class composed of a plurality of "class members." Implemented class member has-A represents the relationship between data members that is a class is defined in another class.
2) a class using another class part. By mutual contact between the class member functions, member of the Friends of the definition or object parameter passing to achieve.
3) mechanism called "inheritance." Transitive relationship, no symmetry.
And we want to say emphatically inherited today.
2, brief succession:
Inheritance is a concept which is object-oriented software technology, polymorphic, a total of three basic features of object-oriented package. Subclasses can inherit having such properties and methods of the parent class, or redefine, the additional properties and methods.
Inheritance is the relationship between class and class, is a very simple and very intuitive concept, the real world is similar to inherit, such as the son inherited his father's property.
Inheritance (English: inheritance) is a concept of an object-oriented software technology which, inheritance can be understood as a class member variables and member functions to obtain from another class process. For example, class B inherits from class A, then B will have A member variables and member functions. Inherited class is called the parent class or a base class inherit the class is called a derived class, or subclass.
In addition to members of the derived class has a base class, you can also define your own new members to enhance the functionality class.

3. The following are two typical scenarios using inheritance:
1) When you create a new class with existing classes similar, but more of when a number of member variables or member functions, you can use inheritance, this will not only reduce the amount of code, and the new class will have all the features of the base class.
2) When you need to create multiple classes, they have many similarities when member variables or member functions, you can use inheritance. Common members of these classes can be extracted, is defined as the base class, and inherited from the base class, the code can be saved, but also facilitate subsequent modifications members.
4, inheritance syntax:
Class derived class: the base class inheritance (access rights)
{
derive new class declaration
}
5, inherited some properties:
1) the subclass has all the member variables and member functions of the parent class
2) sub class can have methods and properties of the parent class no
3) sub-category is a special kind of parent class
4) subclass object can be used as a parent class object uses
the following simple with a case to elaborate on the concept of inheritance:

#include <stdio.h>
#include <string.h>

class Animal
{
public:
    void setName(char *name)
    {
        strcpy(this->name, name);
    }

    void Sleep()
    {
        printf("%s睡觉了!\n", this->name);
    }

public:
    char name[20];
    int age;
};

class Panda : public Animal
{
public:
    void eatBanboo()
    {
        printf("%s啃竹子!\n", this->name);
    }
};

class Dog :public Animal
{
public:
    void catchHouse()
    {
        printf("%s看家!\n", this->name);
    }
};

int main()
{
    Panda p;
    p.setName("熊猫");
    p.Sleep();
    p.eatBanboo();

    Dog d;
    d.setName("狗");
    d.Sleep();
    d.catchHouse();

    return 0;
}

In this case, the base class is a class of animals, it has two member variables name and age, and sleep this member methods, it is clear also can use these two variables and methods in its class and derived classes panda dog class it is that they inherit from the base class over, so you do not need to define all the members of the same class, at the same time, the derived class has added two new members of the methods, such as pandas can have their own the new method "eating bamboo", and the dog "housekeeping." This case can simply set forth the concept of inheritance, of course, more knowledge about the succession to the point that we need to learn.

Guess you like

Origin blog.csdn.net/dxd_123456/article/details/78066877