First, the package
encapsulation is the abstraction of the data and behavior of the combination, fortunately once an organic whole, the source code of the data are organically combined to form a class. The purpose of encapsulation is to enhance security and simplify programming, so that the user does not have to understand the specifics of the implementation, but only uses the members of the class through the external interface, with specific access rights. Encapsulation can hide implementation details and make the code modular.
#include <iostream>using namespace Std;class student{public: void setage (int _age) {age = _age; } int getage () { return age; } Private: int age; string name;};
The above is the encapsulation of the member variable age in the Student class
Ii. inheritance
C + + supports inheritance through the class derivation mechanism. The inherited type is called the base class, and the newly generated class is a derived class living subclass. The process of constructing a new class that preserves the attributes of an existing class is called inheritance. The process of creating a new class based on an existing class is called derivation. The purpose of inheritance and derivation is to preserve the attributes of an existing class and construct a new class. The purpose of inheritance: to implement code reuse. Purpose of derivation: Implement code enrichment. Three ways of inheriting: public, protected, private.
The constructor for the Inheritance: (1), the constructor of the base class cannot be inherited, and the derived class needs to declare its own constructor;
(2), when declaring a constructor, only the new members of this class need to be initialized, the inheritance of the base class member initialization, automatically call the base class constructor complete;
(3), the constructor of the derived class needs to pass parameters to the constructor of the base class;
(4), when the base class declares a default form of a constructor or undeclared constructor, the derived class constructor can not pass parameters to the base class constructor;
(5), the Destructors class does not declare the constructor, in the derived class can also not declare, all using the default form constructor;
(6), when the base class declares a constructor with a formal parameter, the derived class should also declare the constructor with the formal parameter and pass the argument to the base class constructor;
(7), the call order of the constructor: A, call the base class constructor, the order in which they are declared when they are inherited (from left to right), B, the constructor of the calling member object, the order in which they are declared in the class, and the contents of the constructor body of the derived class.
The destructor of the Inheritance: (1), destructors are not inherited, derived classes declare themselves;
(2), the Declaration method and the general (No inheritance relationship) class of the same destructor;
(3), do not need to display the destructor of the base class, the system will automatically invoke the implicit;
(4), the call order of the destructor is the opposite of the constructor function.
Hidden rule with the same name: when the derived class has the same member as the base class: (1), if it is not forcibly named, the derived class object is used with the same name as the member in the derived class;
(2) If you want to access a member of the same name overridden by a derived class object in the base class, you should use the base class name:: data member name.
Virtual base class: function: (1), mainly used to solve the multiple inheritance of the same base class can be inherited multiple times, resulting in two of the question of ambiguity;
(2), providing a unique base class member for the farthest derived class without repeating multiple copies.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
encapsulation, inheritance in C + +