[問題] OOP繼承

看板C_and_CPP (C/C++)作者 (我們都活在這個城市裡面)時間16年前 (2009/05/07 23:07), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/2 (看更多)
下面程式碼 class Apple:public Fruit 中的修飾詞public 我知道是存取權限多少的關係 class中的public成員,private成員,我理解 但public的類別(Fruit)要怎麼解釋與子類別(Apple)的關係最好呢??? 感謝 ---------------------------------------------- class Fruit { private: double weight; char *color; public: Fruit(double,char *); vitual ~Fruit(); }; Fruit::Fruit(double w,char *col) { weight=w; color=new char[strlen(col)+1]; strcpy(color,col) } Fruit::~Fruit() { delete []color; } class Apple:public Fruit { private: char *variety; public: Apple(double,char *, char*); ~Apple(); } Apple::Apple(double w,char *col,char *var):Fruit(w,col) { vareity=new char[strlen(var)+1]; strcpy(variety,var); } Apple::~Apple(){delete variety;} main() { Fruit *basket[20]; int i; double weight; char color[128];variety[128]; for(i=0;i<20;i++) basket[i]=new Apple(weight,color,variety); for(i=0;i<20;i++) delete backet[i]; } ---------------------------------------------- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.137.1.110

05/08 07:40, , 1F
Apple is-a Fruit
05/08 07:40, 1F
文章代碼(AID): #1A0laLC1 (C_and_CPP)
討論串 (同標題文章)
以下文章回應了本文
完整討論串 (本文為第 1 之 2 篇):
0
1
文章代碼(AID): #1A0laLC1 (C_and_CPP)