C++鐨勮櫄缁ф壙 - 宸茶В鍐?- 鎼滄悳闂棶
C++鐨勮櫄缁ф壙鍦ㄤ粈涔堟儏鍐典笅浣跨敤
C++鐨勮櫄缁ф壙鍦ㄤ粈涔堟儏鍐典笅浣跨敤
答案:浠庢剰涔変笂鏉ョ湅锛屼竴涓猄leeperSofa娌℃湁娌欏彂鍜屽簥涓ょ閲嶉噺锛?濡傛鐨勭户鎵夸笉鏄湡瀹炵殑鐜板疄涓栫晫鎻忚堪銆傝繘涓€姝ュ垎鏋愬彲寰楋紝搴婂拰娌欏彂閮芥槸瀹跺叿鐨勪竴绉嶏紝鍑″鍏烽兘鏈夐噸閲忥紝鎵€浠ラ€氳繃鍒嗚В鏉ヨ€冨療鍏跺叧绯伙紝濡傚浘17鈥?鎵€绀恒€?
鍥?7-2 搴婂拰娌欏彂鐨勫垎瑙?銆€銆€銆€銆€//**********************
銆€銆€銆€銆€//** 銆€銆€ch17_2.cpp 銆€**
銆€銆€銆€銆€//**********************
銆€銆€銆€銆€#include<iostream.h>
銆€銆€銆€銆€class Furniture
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€Furniture(){}
銆€銆€銆€銆€銆€銆€void SetWeight(int i){ weight =i; }
銆€銆€銆€銆€銆€銆€int GetWeight(){ return weight; }
銆€銆€銆€銆€銆€protected:
銆€銆€銆€銆€銆€銆€int weight;
銆€銆€銆€銆€};
銆€銆€銆€銆€class Bed :public Furniture
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€Bed(){}
銆€銆€銆€銆€銆€銆€void Sleep(){ cout <<"Sleeping...\\n"; }
銆€銆€銆€銆€};
銆€銆€銆€銆€class Sofa :public Furniture
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€Sofa(){}
銆€銆€銆€銆€銆€銆€void WatchTV(){ cout <<"Watching TV.\\n"; }
銆€銆€銆€銆€};
銆€銆€銆€銆€class SleeperSofa :public Bed, public Sofa
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€SleeperSofa() :Sofa(), Bed(){}
銆€銆€銆€銆€銆€銆€void FoldOut(){ cout <<"Fold out the sofa.\\n"; }
銆€銆€銆€銆€};
銆€銆€銆€銆€void main()
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€SleeperSofa ss;
銆€銆€銆€銆€銆€ss.SetWeight(20); //缂栬瘧鍑洪敊锛佹ā绯婄殑SetWeight鎴愬憳
銆€銆€銆€銆€銆€Furniture* pF;
銆€銆€銆€銆€銆€pF =(Furniture*)&ss; //缂栬瘧鍑洪敊锛佹ā绯婄殑Furniture*
銆€銆€銆€銆€銆€cout <<pF->GetWeight() <<endl;
銆€銆€銆€銆€}鍥犱负SleeperSofa涓嶆槸鐩存帴缁ф壙Furniture锛岃€屾槸Bed鍜孲ofa鍚勮嚜缁ф壙Furniture锛屾墍浠ュ畬鏁寸殑SleeperSofa瀵硅薄鍐呭瓨甯冨眬濡傚浘17-3鎵€绀恒€?br>
鍥?7-3 瀹屾暣SleeperSofa瀵硅薄鍐呭瓨甯冨眬
杩欓噷涓€涓猄leepersofa鍖呮嫭涓€涓畬鏁寸殑Bed锛岄殢鍚庤繕鏈変竴涓畬鏁寸殑Sofa锛屽悗闈㈣繕鏈変竴涓?Sleepersofa鐗规湁鐨勪笢瑗裤€係leeperSofa涓殑姣忎竴涓瓙瀵硅薄閮芥湁瀹冭嚜宸辩殑Furniture閮ㄥ垎銆傚洜涓烘瘡涓瓙瀵硅薄缁ф壙Furniture锛屾墍浠ヤ竴涓猄leeperSofa鍖呭惈涓や釜Furniture瀵硅薄锛屽疄闄呬笂鐨勭户鎵垮眰娆″鍥?7鈥?鎵€绀恒€?br>
鍥?7-4 SleeperSofa鐨勫疄闄呯户鎵垮叧绯?br>
銆€銆€缂栬瘧chl7鈥?锛巆pp鏃讹紝涓嶇煡閬揝etWeight()灞炰簬鍝竴涓狥urniture鎴愬憳锛屾寚鍚慒urniture鐨勬寚閽堜篃涓嶇煡閬撶┒绔熸寚鍝竴涓狥urniture銆傝繖灏辨槸涓轰粈涔坈hl7_2.cpp缂栬瘧閫氫笉杩囩殑鍘熷洜銆?br>銆€銆€SleeperSofa鍙渶涓€涓狥umiture锛屾墍浠ユ垜浠笇鏈涘畠鍙惈涓€涓狥urniture鎷疯礉锛屽悓鏃跺張瑕佸叡浜獴ed鍜孲ofa鐨勬垚鍛樺嚱鏁颁笌鏁版嵁鎴愬憳锛孋++瀹炵幇杩欑缁ф壙缁撴瀯鐨勬柟娉曠О涓鸿櫄鎷熺户鎵?virtual inheritance)銆?br>銆€銆€涓嬮潰鏄櫄鎷熺户鎵跨殑浠g爜锛?銆€銆€銆€銆€//**********************
銆€銆€銆€銆€//** 銆€銆€ch17_3.cpp 銆€**
銆€銆€銆€銆€//**********************
銆€銆€銆€銆€#include<iostream.h>
銆€銆€銆€銆€class Furniture
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€Furniture(){}
銆€銆€銆€銆€銆€銆€void SetWeight(int i){ weight =i; }
銆€銆€銆€銆€銆€銆€int GetWeight(){ return weight; }
銆€銆€銆€銆€銆€protected:
銆€銆€銆€銆€銆€銆€int weight;
銆€銆€銆€銆€};
銆€銆€銆€銆€class Bed :virtual public Furniture
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€Bed(){}
銆€銆€銆€銆€銆€銆€void Sleep(){ cout <<"Sleeping...\\n"; }
銆€銆€銆€銆€};
銆€銆€銆€銆€class Sofa :virtual public Furniture
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€Sofa(){}
銆€銆€銆€銆€銆€銆€void WatchTV(){ cout <<"Watching TV.\\n"; }
銆€銆€銆€銆€};
銆€銆€銆€銆€class SleeperSofa :public Bed, public Sofa
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€public:
銆€銆€銆€銆€銆€銆€SleeperSofa() :Sofa(), Bed(){}
銆€銆€銆€銆€銆€銆€void FoldOut(){ cout <<"Fold out the sofa.\\n"; }
銆€銆€銆€銆€};
銆€銆€銆€銆€void main()
銆€銆€銆€銆€{
銆€銆€銆€銆€銆€SleeperSofa ss;
銆€銆€銆€銆€銆€ss.SetWeight(20);
銆€銆€銆€銆€銆€cout <<ss.GetWeight() <<endl;
銆€銆€銆€銆€}銆€杩愯缁撴灉涓猴細
銆€銆€銆€銆€20
銆€銆€鍦˙ed鍜孲ofa缁ф壙Furniture涓姞涓妚irtual鍏抽敭瀛楋紝杩欑浉褰撲簬璇达紝 鈥滃鏋滆繕娌℃湁Furniture绫伙紝鍒欏姞鍏ヤ竴涓狥urniture鎷疯礉锛屽惁鍒欏氨鐢ㄦ湁鐨勯偅涓€涓€濄€傛鏃朵竴涓猄leepersofa鍦ㄥ唴瀛樹腑鐨勫竷灞€瑙佸浘17-5銆?br>
鍥?7-5 铏氭嫙缁ф壙鐨凷leepeiSofa鍐呭瓨甯冨眬
鍦ㄨ櫄鎷熺户鎵跨殑鎯呭喌涓嬶紝搴旂敤绋嬪簭main()涓紩鐢℅etWeight()涓嶅啀妯$硦锛屾垜浠緱鍒颁簡鐪熸鐨勫浘17-2鎵€绀虹殑缁ф壙鍏崇郴銆?br>銆€銆€->铏氭嫙缁ф壙鐨勮櫄鎷熷拰铏氭嫙鍑芥暟鐨勮櫄鎷熸病鏈変换浣曞叧绯汇€?铏氭嫙缁ф壙鍦ㄤ竴鑸殑搴旂敤涓緢灏戠敤鍒帮紝鎵€浠ヤ篃寰€寰€琚拷瑙嗭紝杩欎篃涓昏鏄洜涓哄湪C++涓紝澶氶噸缁ф壙鏄笉鎺ㄨ崘鐨勶紝涔熷苟涓嶅父鐢紝鑰屼竴鏃︾寮€浜嗗閲嶇户鎵匡紝铏氭嫙缁ф壙灏卞畬鍏ㄥけ鍘讳簡瀛樺湪鐨勫繀瑕侊紙鍥犱负杩欐牱鍙細闄嶄綆鏁堢巼鍜屽崰鐢ㄦ洿澶氱殑绌洪棿锛屽叧浜庤繖涓€鐐癸紝鎴戣嚜宸辫繕娌℃湁澶娣卞埢鐨勭悊瑙o紝鏈夊叴瓒g殑鍙互鐪嬬綉缁滀笂鐧芥潹鐨勪綔鍝併€奟TTI銆佽櫄鍑芥暟鍜岃櫄鍩虹被鐨勫紑閿€鍒嗘瀽鍙婁娇鐢ㄦ寚瀵笺€嬶紝璇村疄璇濇垜鐩墠杩樻病鐪嬪緱寰堟槑鐧斤紝楂樹汉鍙互鎸囩偣涓嬫垜锛夈€?br>鍙﹀鍏充簬鍏舵湰璐ㄧ殑鎺㈣锛屽彲浠ュ弬鑰?http://www.cppblog.com/chemz/archive/2007/06/12/26135.html
上一个:设置对话框不能移动 C++
下一个:C++日期类编程题