C++學習雜記

來源:互聯網
上載者:User

定義的標頭檔:

class Girl 定義成虛擬基類,這樣將禁止產生基類Girl的對象,同時把建構函式:不帶參數的建構函式Girl( )   、 帶參

 

數的建構函式  Girl( string name , int height , int age, int weight ) 放在了protected範圍,進一步強調禁止在Girl類外建立基於

 

 

基類的對象,當然顯式的把複製建構函式放在private域,也是有利無弊的。



class Beauty_girl  :  public Girl { }; 定義了一個Beauty_girl,從基類Girl公有派生而來,添加了四個新增加成員,

 

bool her_mettle ;  //氣質

bool her_culture ;  //修養

bool her_kind_heart ; // 善良

bool her_virgin ;   

 

此時基類Girl 的 public成員將隱式作為衍生類別Beauty_girl 的Public成員(注意,用的是隱式,可以理解為從基類到衍生類別對應範圍的映

 

射),此時衍生類別對象可在類外部直接調用公有繼承的方法或資料成員。

 

 

同樣基類Girl的protected成員將隱式作為衍生類別Beauty_girl的protected成員(過分強調一個問題,可能略顯多餘,但我覺得對比犯下隱

 

晦的難以發現的bug,這樣還是有必要的 【隱式可以理解為從基類到衍生類別對應範圍的映射,至少我認為不會是直接的copy,那樣是很影

 

響C++類封裝的效率的】) 。

 

 

當然基類Girl的private成員將隱式作為衍生類別Beauty_girl的private成員,這時在類外訪問private成員(資料或者方法)必須通過衍生類別

 

對應的介面(及public方法)來間接調用。

 

#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 : "<<her_name<<endl ; }<br />void Get_height( )const{ cout<<"Height : "<<her_height<<endl ; }<br />void Get_age( )const{ cout<<"Age : "<<her_age<<endl ; }<br />void Get_weight( )const{ cout<<"Weight : "<<her_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 ; //氣質<br />bool her_culture ; //修養<br />bool her_kind_heart ; // 善良<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 通過私人繼承Beauty_girl派生而來,此時基類Beauty_girl的public/protected成員都將作為衍生類別Sex_girl的

 

私人(private)成員,此時在類外,通過衍生類別對象直接調用基類public成員是不被允許的。衍生類別對象可在類外通過衍生類別public成員方法

 

來間接訪問基類的public/protected成員。

 

 

#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_culture( )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 />}<br />else<br />{<br />cout<<"Girl must feel good about herself ! "<<endl;<br />}<br />}<br />else<br />{<br />cout<<"kind heart is most precious in the world ! "<<endl;<br />}<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 : "<<her_bust<<endl;<br />cout<<"Waist : "<<her_waist<<endl;<br />cout<<"Hip : "<<her_hip<<endl;<br />}<br />void Sex_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 />}<br />else<br />{<br />cout<<"SHIT , you must reduce you weight ! "<<endl;<br />}<br />} 

###上面是類的實現###

 

 

###下面是main()函數###

 

// 公有繼承【1】.cpp: 主專案檔案。<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, true, true , 28 , 20 , 25 );<br />My_Gf.Get_sex_infor( );<br />My_Gf.Syns_judge( );<br />system("pause");<br /> return 0;<br />}<br /> 

 

 

附註:語言難以闡釋的,代碼總是通用的。

 

##運行結果:

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.