C++ Review------ Polymorphism

1.

–Base-pointer to base-object, derived-pointerto derived
•Straightforward
–Base-pointer to derived object
•Can only call base-class functions( But if thebase class function isdeclared virtual, then the derivedclass version can be called.)
–Derived-pointer to base-object
•Compiler error
•Allowed if explicit cast made (more insection 10.9)


2.
Abstract class
–Sole purpose: to be a base class (called abstract base classes)
–Incomplete
•Derived classes fill in "missingpieces"
–Cannot instantiate objects from an abstract class
•However, can have pointers and references toclass.

–Need one or more "pure" virtualfunctions

virtual void draw() const =0;

–Pure virtual functions
•No implementation, must be overridden, we can provide a definition for pure virtual function, but still we have to provide a new version for each derived class.

Can't be substantiated, but can create pointer or reference to it. Have no constructor and a virtual destructor.


3. class A
class B : public class A

A a; 
B b;
a = b;//ok, but have slicing problem
b = a; // error, because derived class is a base class but base class is not a derived class
It is the same situation when use pointer and reference;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值