C++ for the MFE class at UC BerkeleySession #2.ppt
《C++ for the MFE class at UC BerkeleySession #2.ppt》由会员分享,可在线阅读,更多相关《C++ for the MFE class at UC BerkeleySession #2.ppt(28页珍藏版)》请在麦多课文档分享上搜索。
1、C+ for the MFE class at UC Berkeley Session #2,Yuli Kaplunovsky MFE, MBA, MS-Tax, CFA yuliFinancialS (408) 884 5965,2,Inheritance: derive one class from another,class Mammal public:Mammal();Mammal();int GetAge();void SetAge(int);int GetWeight();void SetWeight();void Speak();void Sleep();protected:in
2、t itsAge;int itsWeight; ;,enum BREED SHETLAND, DOBERMAN, LAB ;class Dog : public Mammal public:Dog();Dog();BREED GetBreed() const;void SetBreed(BREED);protected:BREED itsBreed; ;,void main() Dog fido;fido.SetAge(3);fido.SetBreed( SHETLAND );cout “Fido is “ fido.GetAge() “ years oldn“; ,3,Inheritance
3、: Overriding Functions,class Mammal public:/ constructorsMammal() cout “Mammal constructor.n“; Mammal() cout “Mammal destructor.n“; void Speak() cout “Mammal sound!n“; void Sleep() cout “shhh. Im sleeping.n“; protected:int itsAge;int itsWeight; ;,class Dog : public Mammal public:Dog() cout “Dog cons
4、tructor.n“; Dog() cout “Dog destructor.n“; void WagTail() cout “Tail wagging.n“; void BegForFood() cout “Begging for food.n“; void Speak() cout “Woof!n“; private:BREED itsBreed; ;,int main() Mammal bigAnimal;Dog fido;bigAnimal.Sleep();bigAnimal.Speak();fido.Sleep();fido.Speak();return 0; Mammal cons
5、tructor. Mammal constructor. Dog constructor. Shh. Im sleeping. Mammal sound! Shh. Im sleeping. Woof! Dog destructor. Mammal destructor. Mammal destructor.,4,Pointer to a class,void PrintDogsAge( Dog * P ) printf(“The age of the ”“Dog is: %d n“, P-GetAge(); ,void main() Dog *pFido = new Dog;pFido-Se
6、tAge( 4 );PrintDogsAge( pFido );Dog *pMyDog;pMyDog = pFido;PrintDogsAge( pMyDog );Dog Barky;Barky.SetAge(3);pMyDog = ,Exercise: Write the output of this program,5,Virtual,class Mammal public:Mammal():itsAge(1) cout “Mammal constructor.n“; Mammal() cout “Mammal destructor.n“; void Move() cout “Mammal
7、 move one stepn“; virtual void Speak() cout “Mammal speak!n“; protected:int itsAge; ;,class Dog : public Mammal public:Dog() cout “Dog Constructor.n“; Dog() cout “Dog destructor.n“; void WagTail() cout “Wagging Tail.n“; void Speak() cout “Woof!n“; void Move() cout “Dog moves 5 steps.n“; ;,int main()
8、 Mammal *pDog = new Dog;pDog-Move();pDog-Speak(); Mammal constructor. Dog Constructor. Mammal move one step Woof!,Dog Fido;Fido.Move();Fido.Speak();Mammal constructor. Dog Constructor. Dog moves 5 steps Woof! Dog destructor Mammal destructor,6,Virtual Why do we need it?,For example, you could create
9、 many different types of windows, including dialog boxes, scrollable windows, and list boxes, and give them each a virtual draw() method. By creating a pointer to a window and assigning dialog boxes and other derived types to that pointer, you can call draw() without regard to the actual run-time ty
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CFORTHEMFECLASSATUCBERKELEYSESSION2PPT
