C/c++:c++ how to construct a parent class

Source: Internet
Author: User

C + + Executes the construction method of the parent class. This is written on the basis of the inheritance of the original class.


First, we create a project. Then build our parent class.


People.h:

<span style= "FONT-SIZE:18PX;" > #ifndef people_h#define people_h#include<iostream>using namespace Std;class people{public    :        People ();//people default constructor method        people (int age, int sex);//Constructs a method without returning the type        int get_age ();//Get ages        int get_sex ();// Get gender    private:        int age;        int sex;//Specify 1 for male, 0 for female;}; #endif//People_h</span>

Then we have a CPP file in our parent class to specifically write our construction method:


<span style= "FONT-SIZE:18PX;" > #include "People.h" People::P eople () {This--age    =;    This, sex = 1;} People::P eople (int age, int sex) {This--age    = age;    This, sex = sex;} Constructs a method without returning the type int people::get_age () {return this--age    ;} int People::get_sex () {    return this, sex;} </span>

Well, the parent class is ready, and we'll create a subclass:


<span style= "FONT-SIZE:18PX;" > #ifndef main_h#define main_h#include "People.h" class Main:public people{public    :    private:};//Inherits parent class people #endif//Main_h</span>

In this way our subclass inherits the parent class people.


Then, it's our main file:

<span style= "FONT-SIZE:18PX;" > #include "Main.h" int main () {    main *p = new Main ();    cout << p->get_age () << Endl;    cout << p->get_sex () << Endl;    Delete p;    return 0;} </span>

Now it's ready to run.

And the result is 18 and 1; Why is this result? Because, now our subclass main just inherits the People class,

Did not write its own construction method, the data passed to a parent class. That is, the default constructor that executes the people class.

Good. Now, let's write the subclass main's constructor method to pass in the data to the parent class;


Main.h:

<span style= "FONT-SIZE:18PX;" > #ifndef main_h#define main_h#include "People.h" class Main:public people{public    :        MAIN (int age, int sex);};/ /Inherit parent class People#endif//Main_h</span>

now that the subclass constructor is written, how do we do it to pass the data to the parent class?

<span style= "FONT-SIZE:18PX;" > #include "Main.h" main::main (int age, int sex):P eople (age, Sex) {}</span>

is to use ":" this. To pass the data of the constructed method in our subclass back to the parent class.


OK, let's change the main function to see the effect:


<span style= "FONT-SIZE:18PX;" > #include "Main.h" int Main () {    int a = +;    int b = 0;    Main *p = new Main (a, b);    cout << p->get_age () << Endl;    cout << p->get_sex () << Endl;    Delete p;    return 0;} </span>


At this point, we are outputting the result is 16 and 0.



PS: One small step per day.






C/c++:c++ how to construct a parent 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.