Defined header file:
Class girl is defined as a virtual base class. This will disable the generation of base class girl objects. At the same time, constructor: constructor without parameters girl (), with Parameters
Number constructor girl (string name, int height, int age, int weight) is placed in the protected scope, further emphasizing that it is prohibited to create
The base class object, of course, explicitly placing the copy constructor in the private domain is also advantageous.
Class beauty_girl: Public girl {}; defines a beauty_girl, derived from the base class girl public, and adds four new members,
Bool her_mettle; // temperament
Bool her_culture; // Cultivation
Bool her_kind_heart; // kind
Bool her_virgin;
At this time, the Public Member of the base class girl will be implicitly used as the Public Member of the derived class beauty_girl (note that it is used implicitly and can be understood as reflecting the corresponding scope from the base class to the derived class
In this case, the derived class object can directly call the Public inherited method or data member outside the class.
Similarly, the protected member of the base class girl will be implicitly used as the protected member of the derived class beauty_girl (over-emphasizing a problem may be a little redundant, but I think the comparison is implicit.
Hidden bugs that are hard to be found, so it is necessary [implicit can be understood as the ing from the base class to the corresponding scope of the derived class, at least I don't think it will be a direct copy, it is very influential
C ++ class encapsulation efficiency ]).
Of course, the private member of the base class girl will be implicitly used as the private member of the derived class beauty_girl. In this case, access to the private member (data or method) outside the class must pass the derived class
The corresponding interface (and public method) for indirect calls.
# Ifndef girl_h <br/> # define girl_h <br/> # include <string> <br/> # include <iostream> <br/> Using STD: string; <br/> Using STD: cout; <br/> Using STD: Endl; <br/> class girl // virtual base class <br/>{< br/> PRIVATE: <br/> string her_name; <br/> int her_height; <br/> int her_age; <br/> int her_weight; <br/> girl (const girl &); <br/> Public: </P> <p> virtual ~ GIRL () = 0; <br/> void get_name () const {cout <"name:" <per_name <Endl ;}< br/> void get_height () const {cout <"height:" <per_height <Endl ;}< br/> void get_age () const {cout <"age: "<per_age <Endl ;}< br/> void get_weight () const {cout <" Weight: "<per_weight <Endl ;}< br/> protected: <br/> girl (): her_name ("love_ling"), her_height (165), her_age (18), her_weight (85) <br/> {cout <"default constructor called" <Endl ;}< br/> girl (string name, int height, int age, int weight ): her_name (name), her_height (height), <br/> her_age (AGE), her_weight (weight) <br/> {cout <"Explicit constructor called" <Endl ;}< br/> void get_information () const; <br/> }; <br/> class beauty_girl: Public girl // drived class <br/>{< br/> PRIVATE: <br/> bool her_mettle; // temperament <br/> bool her_culture; // cultivation <br/> bool her_kind_heart; // kind <br/> bool her_virgin; <br/> beauty_girl (const beauty_girl &); <br/> Public: <br/> beauty_girl (): Girl (), her_mettle (true), her_culture (true ), her_kind_heart (true), her_virgin (true) {}< br/> beauty_girl (string name, int height, int age, int weight, bool mettle, bool culture, bool kind_heart, <br/> bool virgin): Girl (name, height, age, weight), her_mettle (mettle), her_culture (Culture), <br/> her_kind_heart (kind_heart ), her_virgin (virgin) {}< br/> void set_mettle (bool yes_or_no) <br/>{< br/> her_mettle = yes_or_no; <br/>}< br/> void set_culture (bool yes_or_no) <br/>{< br/> her_culture = yes_or_no; <br/>}< br/> void set_kind_heart (bool yes_or_no) <br/>{< br/> her_kind_heart = yes_or_no; <br/>}< br/> void set_virgin (bool yes_or_no) <br/>{< br/> her_virgin = yes_or_no; <br/>}< br/> void get_mettle () const; <br/> void get_culture () const; <br/> void get_kind () const; <br/> void if_virgin () const; <br/> void get_infor () const; <br/> void syn_judge (); <br/> protected: <br/> void judge () const; <br/>}; <br/> class sex_girl: Private beauty_girl <br/>{< br/> PRIVATE: <br/> int her_bust; <br/> int her_waist; <br/> int her_hip; <br/> sex_girl (const sex_girl &); <br/> public: <br/> sex_girl (): beauty_girl (), her_bust (33), her_waist (22), her_hip (33) {}< br/> sex_girl (string name, int height, int age, int weight, bool mettle, bool culture, bool kind_heart, <br/> bool virgin, int bust, int waist, int hip): beauty_girl (name, height, age, weight, mettle, culture, <br/> kind_heart, virgin), her_bust (bust), her_waist (waist), her_hip (HIP) {}</P> <p> void set_bust (INT bust) <br/> {her_bust = bust ;}< br/> void set_waist (INT waist) <br/> {her_waist = waist ;}< br/> void set_hip (INT hip) <br/> {her_hip = hip ;}< br/> void get_sex_infor () const; <br/> void set_sex_chara (bool mettle, bool culture, bool kind_heart, bool virgin, int bust, int waist, int hip); <br/> void syns_judge () const; <br/>}; <br/> # endif
Class sex_girl is derived from beauty_girl by private inheritance. At this time, the public/protected members of the base class beauty_girl will be used as
Private member. In this case, calling the base class public member directly through the derived class object is not allowed outside the class. The object of a derived class can use the public member method of the derived class outside the class.
To indirectly access the public/protected members of the base class.
# Include "stdafx. h "<br/> # include" girl. h "<br/> using std: endl; <br/> // Girl member function // <br/> Girl ::~ Girl () <br/>{< br/> cout <"Constructor of Girl called! "<Endl; <br/>}< br/> void Girl: Get_information () const <br/>{< br/> Get_name (); <br/> Get_height (); <br/> Get_age (); <br/> Get_weight (); <br/>}< br/> // Beauty_girl member function // <br/> void Beauty_girl: Get_mettle () const <br/> {<br/> if (her_mettle = true) <br/> cout <"Yes, you are a mettler" <endl; <br/> else <br/> cout <"Pity, you are a rude giel" <endl; <br/>}< br/> void Beauty_girl: Get_cult Ure () const <br/>{< br/> if (her_culture = true) <br/> cout <"Yes, you are a culture girl" <endl; <br/> else <br/> cout <"oh no, you are a dirty girl" <endl; <br/>}< br/> void Beauty_girl :: get_kind () const <br/>{< br/> if (her_kind_heart = true) <br/> cout <"yes, you are kind heart a girl "<endl; <br/> else <br/> cout <" shit, you are atrocity a girl "<endl; <br/>}< br/> void Beauty_girl: If_virgin () Const <br/>{< br/> if (her_virgin = true) <br/> cout <"Congratulations, You Still a Virgin" <endl; <br/> else <br/> cout <"God, you yet not a virgin" <endl; <br/>}< br/> void Beauty_girl :: get_infor () const <br/>{< br/> Girl: Get_information (); <br/> Get_mettle (); <br/> Get_culture (); <br/> Get_kind (); <br/> If_virgin (); <br/>}< br/> void Beauty_girl: Judge () const <br/>{< br/> if (her_kind_heart = True) <br/>{< br/> if (her_virgin = true) <br/>{< br/> if (her_mettle = true) <br/>{< br/> cout <"God Bless, You are a pure girl! "<Endl; <br/>}< br/> else <br/> {<br/> cout <" Jeez, you 'd better train you mettle! "<Endl; <br/>}< br/> else <br/>{< br/> cout <" Girl must feel good about herself! "<Endl; <br/>}< br/> else <br/>{< br/> cout <"kind heart is most precious in the world! "<Endl; <br/>}< br/> void Beauty_girl: Syn_judge () <br/>{ <br/> cout <"-------------- Synthesize Judge ------------" <endl; <br/> Judge (); <br/>}< br/> // Sex_girl member function // <br/> void Sex_girl: Get_sex_infor () const <br/>{< br/> Beauty_girl :: get_infor (); <br/> cout <"Bust:" <per_bust <endl; <br/> cout <"Waist:" <per_waist <endl; <br/> cout <"Hip:" <per_hip <endl; <br/>}< br/> void S Ex_girl: Set_sex_chara (bool mettle, bool culture, bool kind_heart, bool virgin, int bust, int waist, int hip) <br/>{< br/> Beauty_girl :: set_mettle (mettle); <br/> Beauty_girl: Set_culture (culture); <br/> Beauty_girl: Set_kind_heart (kind_heart); <br/> Beauty_girl :: set_virgin (virgin); <br/> Set_bust (bust); <br/> Set_waist (waist); <br/> Set_hip (hip ); <br/>}< br/> void Sex_girl: Syns_judge () Const <br/>{< br/> Beauty_girl: Judge (); <br/> if (her_bust/her_waist) >=1.2 & (her_bust/her_waist) <= 1.5) <br/> {<br/> if (her_hip/her_waist)> = 1.2 & (her_hip/her_waist) <= 1.5) <br/> {<br/> if (her_bust/her_hip)> = 0.9 & (her_bust/her_hip) <= 1.0) <br/>{< br/> cout <"you body-stature is perfect! "<Endl; <br/>}< br/> else <br/> {<br/> cout <" you body-stature is Sex! "<Endl; <br/>}</p> <p >}< br/> else <br/>{< br/> cout <"you must keep you body-stature! "<Endl; <br/>}< br/> else <br/>{< br/> cout <" SHIT, you must reduce you weight! "<Endl; <br/>}< br/>}
### Implementation of the class ###
### The following is the main () function ###
// Public inheritance [1]. cpp: Master project file. <Br/> # include "stdafx. h "<br/> # include" girl. h "<br/> # include <iostream> <br/>/* string name, int height, int age, int weight, bool mettle, bool culture, bool kind_heart, bool virgin */<br/> int main (array <System: String ^> ^ args) <br/>{< br/> Sex_girl kate; <br/> kate. get_sex_infor (); <br/> kate. syns_judge (); <br/>/* bool mettle, bool culture, bool kind_heart, bool virgin int bust, int waist, int hip */<br/> cout <endl; <br/> Sex_girl My_Gf; <br/> My_Gf.Set_sex_chara (true, true, 28, 20, 25); <br/> My_Gf.Get_sex_infor (); <br/> My_Gf.Syns_judge (); <br/> system ("pause"); <br/> return 0; <br/>}< br/>
Note: The Language is hard to interpret and the code is always generic.
# Running result: