The concept and syntax of C + + learning 14 inheritance

Source: Internet
Author: User

Inheritance is the relationship between classes and classes, and is a very simple and intuitive concept, similar to inheritance in the real world (such as a son inheriting a father's property).

Inheritance (inheritance) can be understood as a procedure for a class to get member variables and member functions from another class. For example, Class B inherits from Class A, then B has member variables and member functions of a. an inherited class is called a parent class or a base class , and an inherited class is called a subclass or derived class .

In addition to having members of a base class, derived classes can also define their own new members to enhance the functionality of the class.

The following are two typical scenarios for using inheritance:
1) When you create a new class similar to an existing class, you can use inheritance only if you have more than one member variable or member function, which will not only reduce the amount of code, but the new class will have all the functionality of the base class.

2) You can also use inheritance when you need to create multiple classes that have many similar member variables or member functions. The common members of these classes can be extracted, defined as base classes, and then inherited from the base class, saving both code and subsequent modifications to members.

Here we define a base class people, and then derive the Student class:

#include <iostream>using namespacestd;//base class--pelpleclasspeople{Private:    Char*name; intAge ; Public:    voidSetName (Char*); voidSetage (int); Char*GetName (); intgetage ();};voidPeople::setname (Char*name) { This->name =name;}voidPeople::setage (intAge) { This->age =Age ;}Char* People::getname () {return  This-name;}intPeople::getage () {return  This-Age ;}//derived class--studentclassStudent: Publicpeople{Private:    floatscore; Public:    voidSetScore (float); floatGetscore ();};voidStudent::setscore (floatScore) { This->score =score;}floatStudent::getscore () {returnscore;}intMain () {Student stu; Stu.setname ("Xiao Ming"); Stu.setage ( -); Stu.setscore (95.5f); cout<<stu.getname () <<"The Age is"<<stu.getage () <<", the results are"<<stu.getscore () <<Endl; return 0;}

In this case, people is the base class and Student is a derived class. The Student class inherits the members of the People class and also adds its own member variables score and member functions SetScore, Getscore.

Please observe the code line 21st carefully:

class  Public People

This is the syntax for declaring derived classes. The "Student" behind class is a new derived class, and "people" after the colon is the base class that already exists. before "People" there is a key public, which is used to indicate that it is publicly inherited.

This concludes that the general syntax for inheritance is:

class derived class name: [Inheritance mode] base class Name {    derived class newly added member};

The inheritance method includes public, private (private), and protected (protected), which is optional and, if not, the default is private. We'll explain in more detail in the next section.

The concept and syntax of C + + learning 14 inheritance

Related Article

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.