Study Notes for week 3 (3-1)

Source: Internet
Author: User

15th days and 16th days

The process of getting a new class from the existing class inheritance is called derivation. Derivation is the process of inheritance.
Use: Separate the derived class and the base class. The derived class inherits all the public members of the base class. (Except constructors and destructor)
Do not design a base class with many private members.

Protected: protected member, which can be used by the base class and derived class. Generally, the base class only includes public and protected members.
If you want non-class programs, including derived classes, to have no permission for data in the category class, define it as private.

Class base: private emp // The default setting mode provided by VC ++ when inheriting is private. That is, all the Members inherited from the base class are private members in the derived class.

Class base: protected emp // The inherited protected members and public members are all protected members in the derived class.

This is the most common one:
Class base: public emp // The protected members in the base class are still protected members in the derived class, and the public members in the base class are still public members in the derived class.

The internal default constructor does not have the ability to process constants. Therefore, you need to customize the constructor when a constant member appears in the class. In addition, you must use the constructor to construct and initialize a table, instead of constructing and assigning values.

Class c {
Int I;
Char;
Float B;
Public:
C (int I, char a, float B): // here, the colon is used to declare the constructor prototype. It can be in one row with the initialization table.
I (I), a (A), B (B) {}; // here is the initialization table
}

Note: character array members and pointers are usually stored in the heap. Therefore, a value must be assigned to the function.
Class, you should use the initialization table to complete all initialization work.

Why inherit:

By using inheritance, the Code Compiled by myself is used again, which not only maintains data protection, but also is a favorable tool for developing programs. If you have bought a class tool library, you can even derive a new class without implementing part of the source code. When you need to get a different window class than the Class Library provides, you can get a slightly different window through inheritance.

One advantage of inheritance is that you can write new code based on the code you have already understood, and it is easy to derive a new class from the classes you have already compiled and debugged. Inheritance reuse code and data is safer and more effective than reuse in non-OOP programming.

Where is the protected access permission used?

Adding protected access permissions to VC ++ aims to complete inheritance while maintaining data protection. The derived class cannot use private members in the base class.
Some members in the base class are defined to protect members from access by programs outside the class and allow access by Derived classes. The protected members are still invisible to the remaining programs, but all the derived classes can access it. If the access permission is not protected, the data cannot be protected (private) and the derived class can be accessed.
Obtain the derived class from a base class and make the changes.
You can add data members and function functions in a derived class. Subclasses are always more powerful than parent classes.

Why is one-way inheritance better than multi-way inheritance?

One-way inheritance code is easier to write than multi-way inheritance code, which can speed up programming and reduce errors.

Why do Derived classes have to define access permissions?

Because all the derived classes in VC ++ inherit the base class by default, private inheritance is used. This restriction will cause inconvenience.
Defining the access permission as protected will enable the inherited public members and protected members to be protected members in the derived class.
Defining access permission as public will make the inherited public members remain public members and the protected members remain protected members. This is the most common method.

After the initialization table is constructed, in addition to the constant member object, the constructor can also be simplified. The constructor is responsible for constructing the derived class.
The character array must be assigned a value in the function body. Character array members and pointer members are usually stored in the heap, so they must be assigned a value in the constructor.

Example program:

# Include
# Include
Class Parent {
Protected:
Char name [25];
Int age;
Public:
Parent (char [], int );
~ Parent (){};
Void disparent (void );
};
Parent: Parent (char N [], int A): age (){
Strcpy (name, N );
}
Void Parent: disparent (void ){
Cout <"Parents name is:" <cout <"Parents age is:" <}
Class Son: Parent {
Int yrInSchool;
Public:
Void dispSon (void );
Son (char [], int, int );
};
Son: Son (char N [], int A, int Y): Parent (N, A), yrInSchool (Y ){
}
Void Son: dispSon (void ){
Cout <"Sons name is:" <cout <"Sons age is:" <cout <"Son year IN school is:" <}
Class Daughter: Parent {
Int yrInSchool;
Char friendsName [25];
Public:
Void dispDaughter (void );
Daughter (char [], int, int, char []);
};
Daughter: Daughter (char N [], int A, int Y, char F []): Parent (N, A), yrInSchool (Y)
{
Strcpy (friendsName, F );
}
Void Daughter: dispDaughter (void ){
Cout <"Daughters name is:" <cout <"Daughters age is:" <cout <"Daughter year IN school is:" <cout <"Daughters FriendsName is: "<};

Main (){
Parent mom ("Betty", 58 );
Parent dad ("tom", 60 );
Mom. disparent ();
Dad. disparent ();
Son boy ("smalltom", 17,11 );
Boy. dispSon ();
Daughter girl ("ketey", 18, 12, "June ");
Girl. dispDaughter ();
Return 0;
}

When a subclass constructs a base class, the parameter sequence of the initialization table is the same as that of the parent class. In this way, the correct value can be passed to the constructor of the parent class.
First, construct the base class object to construct the derived class object.
To restrict or change the function of inheriting to members, define data members or member functions with the same name in the derived class.
VC ++ automatically parses the inherited objects from the lowest derived class to the base class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.