Examples of inheritance and derivation definitions and related program invocation

Source: Internet
Author: User

First, Object-Oriented programming features: Abstraction, encapsulation, inheritance, polymorphism.

Second, 3 Types of member access qualifiers and their inheritance relationships:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/C7/wKioL1cItPmglf2AAABIbKUEQmA608.png "title=" Picture 1.png "alt=" Wkiol1citpmglf2aaabibkueqma608.png "/>

Questions :

Private/protected qualifiers are limited to direct access, what is the difference between them?

Solution: In this class,private and protected functions are the same, but protected can be inherited, and Private but not.

The changes in the access relationships of the base class members under the three inheritance relationships in the derived class are shown in the following table:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/C7/wKioL1cItT2xVygrAAD9bB4T17s458.png "title=" Picture 2.png "alt=" Wkiol1citt2xvygraad9bb4t17s458.png "/>

Summarize:

1. a Private member of a base class cannot be accessed in a derived class, and if some base class members do not want to be accessed directly by the base class object but need to be accessible in the derived class, they are defined as protected members. You can see that the protection member qualifier occurs because of inheritance.

2.public inheritance is an interface inheritance, and the members that are available to each parent class are also available to the child class, because each subclass object is also a parent class object.

3.protetced/private Inheritance is an implementation inheritance, some members of the base class are not completely part of the subclass interface, so the two inheritance relationships are not used in special cases, and are used in most scenarios as public inheritance.

4. Regardless of the inheritance method, the public and protected members of the base class can be accessed inside the derived class, but the private members of the base class exist but are not visible in the subclass (inaccessible).

5. the default inheritance method when using the keyword class is private, and The default inheritance method when using a struct is public , but it is best to show the way to write the inheritance , in the actual use of general is the public inheritance, Protetced/private inheritance is only used in very few scenarios .

Iii. examples of inheritance and derivation-related definitions

Class Inheritance: A new class obtains its existing attributes from existing classes.

Class: Produces a new subclass from an existing class (the parent Class).

A derived class is a materialization of a base class, which is an abstraction of a derived class.

3 categories of inheritance:

Single inheritance: A derived class derives from only one base class.

Multiple inheritance: A derived class has two or more base classes.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/C7/wKioL1cItWfyvj_OAABBpCR6AjI368.png "title=" Picture 3.png "alt=" Wkiol1citwfyvj_oaabbpcr6aji368.png "/>

Diamond Inheritance:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/C7/wKioL1cItaKDyhkQAABjdHb7hTE094.png "title=" Picture 4.png "alt=" Wkiol1citakdyhkqaabjdhb7hte094.png "/>

Declares the general form of a derived class:

Class derived classes name : [ Inheritance Method ] base class name

{

Newly added members of derived classes

}

Example 1. using the common inheritance method, including the input data function in the program, the running program input num,name,sex,age,addr value, output the above 5 data value.

Solution: Program:

#include <iostream>

using namespace Std;

Class Student

{

Public

void Get_value ()//value of 3 private members of the input base class

{

CIN >> num >> name >> sex;

}

void display ()//value of 3 private members of the output base class

{

cout << "num:" << num << Endl;

cout << "Name:" << name << Endl;

cout << "Sex:" << sex << Endl;

}


Private

int num;

Char name[10];

char sex;

};


Class Student1:public student//defines a common derived class Student1

{

Public

void Get_value_1 ()//function is to enter the value of 5 data members

{

Get_value ();//Call function, enter the value of 3 private data members of the base class

Cin >> Age >> addr;//Enter values for two private data members of a derived class

}

void Display_1 ()//value of 2 private data members of the output derived class

{

cout << ' Age: ' << age << Endl;

cout << "Address:" << addr << Endl;

}

Private

int age;

Char addr[30];

};


int main ()

{

Student1 stud1;//defines an object Student1 a common derived class stud1

Stud1.get_value_1 ();//Input 5 data

Stud1.display ();//value of 3 private data members of the output base class

Stud1.display_1 ();//Output derived class 2 values for private data members

System ("pause");

return 0;

}

Results:

1111 Yao M Xi ' an

num:1111

Name:yao

Sex:m

Age:20

Address:xi ' an

Please press any key to continue ...

Example 2. using the private inheritance method, including the input data function in the program, run the program input num,name,sex,age,addr value, output the above 5 data value.

Solution: Program:

#include <iostream>

using namespace Std;

Class Student

{

Public

void Get_value ()

{

CIN >> num >> name >> sex;

}

void display ()

{

cout << "num:" << num << Endl;

cout << "Name:" << name << Endl;

cout << "Sex:" << sex << Endl;

}


Private

int num;

Char name[10];

char sex;

};


Class Student1:private student//defines a private derived class Student1

{

Public

void Get_value_1 ()

{

Get_value ();

CIN >> age >> addr;

}

void Display_1 ()

{

Display ();

cout << ' Age: ' << age << Endl;

cout << "Address:" << addr << Endl;

}

Private

int age;

Char addr[30];

};


int main ()

{

Student1 STUD1;

Stud1.get_value_1 ();

Stud1.display_1 ();//Just call once Stud1.display_1 ()

System ("pause");

return 0;

}

Results:

1111 Yao M Xi ' an

num:1111

Name:yao

Sex:m

Age:20

Address:xi ' an

Please press any key to continue ...

Example 3. using the protection inheritance method, including the input data function in the program, the running program input num,name,sex,age,addr value, output above 5 data value.

Solution: Program:

#include <iostream>

using namespace Std;

Class student//Declaration base class

{

public://Common members of the base class

void Get_value ();

void display ();

PROTECTED://base class Protection members

int num;

Char name[10];

char sex;

};


void Student::get_value ()

{

CIN >> num >> name >> sex;

}

void Student::d isplay ()

{

cout << "num:" << num << Endl;

cout << "Name:" << name << Endl;

cout << "Sex:" << sex << Endl;

}


Class student1:protected student//declares a protected derived class Student1

{

Public

void Get_value_1 ();

void Display_1 ();

Private

int age;

Char addr[30];

};


void Student1::get_value_1 ()

{

Get_value ();

CIN >> age >> addr;

}

void Student1::d isplay_1 ()

{

cout << "num:" << num << Endl;

cout << "Name:" << name << Endl;

cout << "Sex:" << sex << Endl;

cout << ' Age: ' << age << Endl;

cout << "Address:" << addr << Endl;

}


int main ()

{

Student1 STUD1;

Stud1.get_value_1 ();

Stud1.display_1 ();

System ("pause");

return 0;

}

Results:

1111 Yao M Xi ' an

num:1111

Name:yao

Sex:m

Age:20

Address:xi ' an

Please press any key to continue ...

Iv. Inheritance and Transformation - assignment compatibility rules:

1. subclass objects can be assigned to a parent class object, and the parent object cannot be assigned to a subclass object;

2. A pointer / reference to a parent class can point to a subclass object, and a pointer / reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)

V. Scope in the Inheritance system:

1. in the inheritance system, both the base class and the derived class have separate scopes.

2. Subclasses and parent classes have members of the same name, and child class members will mask direct access to members of the parent class. (in subclass member functions, you can use the base class :: base class member access)-- Hide

Note: In practice, it is best not to define a member with the same name in the inheritance system.

Six, 6 default member functions for a derived class

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/C7/wKioL1cItfLi4xPaAAB13OO6s74782.png "title=" Picture 5.png "alt=" Wkiol1citfli4xpaaab13oo6s74782.png "/>


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1762089

Examples of inheritance and derivation definitions and related program invocation

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.