C C + + (V) inheritance and derivation

Source: Internet
Author: User
Tags access properties inheritance

In actual programming, we encounter one of the following scenarios:

We already have a Class A:

Class Student
{
private:  int num;
     Char name[30];
     char sex;

Public:                             
    void display ()
    {
       cout<< "num:" <<num<<endl;
       cout<< "Name:" <<name<<endl;
       cout<< "Sex:" <<sex<<endl;
    } 
;  
At this point, we need a second Class B:

Class Studend1
{   
private: 
    int num;          This trip originally had
    char name[20];       This trip originally had
    char sex;         The line originally had an
    int age;
    Char addr[20];

Public:            
   void display ()         //This line originally had
    {
        cout<< "num:" <<num<<endl;    This trip originally had
        cout<< "name:" <<name<<endl;//this trip originally had
        cout<< "sex:" <<sex<< Endl;      This trip was originally
        cout<< "Age:" <<age<<endl;
        cout<< "Address:" <<ADDR<<ENDL;}                 
By comparing classes A and B, we can see that many of the code in Class B is multiplexed with Class A, and Class B simply adds a few related members and changes the method relative to Class A.

Inheritance and derivation are meant to solve this scenario. It can improve the reuse of code.

  The so-called "inheritance" in C + + is the creation of a new class on the basis of an existing class. The existing class is called the base class or the parent class (Father Class).
So, by the following form, we can achieve our goal:

Class Student1:public student//declares that the base class is Student
{
private:
    int age;    The newly added data member
    string addr;  Newly added data member public

:
   void Display_1 ()  ///Newly added member function
   {  
        cout<< "Age:" <<age<<endl ; 
        cout<< "Address:" <<addr<<endl;
   }   
;
Through the inheritance mechanism, new data members are defined using the old existing data members. New data members have not only newly defined data members, but also older data members.

The subclass derived from the parent class is as follows:

Class  Classname:<access>baseclassname
The values for access are public, protect, and private. The following:

Depending on the access value, the derivation is divided into public-derived (publicly), protected-derived (protect), and private derived (private). Different derivation methods inherit the data members of the parent class are also different.


Public derivation:

All members in the base class maintain access to individual members in the derived class.
As shown in the following illustration:

Private derivation:

  Public and protected members in a base class become private in derived classes, and these members can still be used directly in derived classes, and private members in the base class cannot be used directly in derived classes.
As shown in the following illustration:

Protection derivation:

When protection is derived, public and protected members in a base class are protected and private in derived classes, and these members can still be used directly in derived classes, and private members in the base class cannot be used directly in derived classes.
As shown in the following illustration:

Summary:

    In general, we are more concerned with derived classes. And the derived class must also use some data from the base class, or we derive a class that is not very meaningful. Therefore, we also care about the access
of the derived class to the data members of the base class. From the above, derived classes can be accessed directly within the class, except for the private data members of the base class. As to the access properties of the data members inheriting from the base class, you can use the following précis-writers: "Public unchanged, protection degraded, private not to ask."


Here is an example of C + +:

Source:

#include <iostream> using namespace std;
	Class BaseClass {private:unsigned int age;
BOOL sex;
		Public:baseclass (unsigned int a,bool s) {age = A;
	sex = s;
	} ~baseclass () {std::cout << "~base Class" << Std::endl;
	} unsigned int basegetage (void);	
BOOL Basegetsex (void);

};

unsigned int baseclass::basegetage (void) {return age;}

BOOL Baseclass::basegetsex (void) {return sex;} Class Deriveclass:public BaseClass {private:unsigned int score,number; public:deriveclass (unsigned int a,bool se,unsi
		gned int sc,unsigned int nu): BaseClass (a,se) {score = SC;
	Number = Nu;
	} ~deriveclass () {std::cout << "~derive Class" << Std::endl;
	} unsigned int derivegetage (void);
	BOOL Derivegetsex (void);
	unsigned int derivegetscore (void);
unsigned int derivegetnumber (void);

};

unsigned int deriveclass::d erivegetage (void) {return basegetage ();}

BOOL Deriveclass::d erivegetsex (void) {return basegetsex ();} unsigned int deriveclErivegetscore::d (void) {return score;}

unsigned int deriveclass::d erivegetnumber (void) {return number;}
	int main (void) {Deriveclass *cls = NULL;	

	CLS = new Deriveclass (18,0,100,26);
	Std::cout << "age =" << cls->derivegetage () << Std::endl;
	Std::cout << "Sex =" << cls->derivegetsex () << Std::endl;
	Std::cout << "score =" << cls->derivegetscore () << Std::endl;

	Std::cout << "number =" << cls->derivegetnumber () << Std::endl;
	Delete cls;

	CLS = NULL;
return 0; }
Compile run:

root@se7en-lifebook-lh531:~/learn/cpp_program# g++ class.cpp-o classtest
root@se7en-lifebook-lh531:~/learn/ cpp_program#./classtest Age 
=
0
score = Number = +
~derive class
~base class
The knowledge points involved:

1. Inheritance and derivation of programming ideas;
2.new object and release;
3. Use the constructor of the derived class to implement the constructor of the base class.

Abstract class:

Definition of abstract class:

When a class is defined, the class can only be used as a base class to derive a new class, and the class is not defined as an abstract class when it is not used to define an object. In short, the access rights of a class's constructor or destructor are defined as protected or
a class containing one or more pure virtual functions. This class is an abstract class. It should be noted that abstract classes are not used to define objects.


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.