{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Educn 1524 class storable class radio public public

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 划的设计决策,可以如下解决 class A{ public: int f(int); char f(char); }; class B{ public: double f(double); }; class AB:public A, public B{ public: using A::f; void g(AB& ab){ using B::f; ab.f(1); // A::f(int) char f(char); ab.f('a');// AB::f(char) // 遮蔽A::f(char) ab.f(2.0);// B::f(double) AB f(AB); ab.f(ab); // AB::f(AB) }; } Beijing University of Posts & Telecommunications Beijing [email protected] 15.2.3 重复的基类 一个类两次作为基类是可能的,例如: Task和Displayed都是从Link派生而来,在 一个Satallite中就会出现两个Link 当引用Link中的元素时,注意必须加以限定 struct Link { Link *next; }; void mess_with_links(Satellite* p){ p->next = 0; // 错误,歧义性 p->Link::next = 0; // 错误,歧义性 p->Task::next = 0; // ok p->Displayed::next = 0; // ok } Beijing University of Posts & Telecommunications Beijing [email protected] 覆盖 重复基类中的虚函数可以由派生类里的函数 覆盖 class Storable{ public: virtual void write() = 0; virtual void read()=0; virtual const char* get_file() = 0; }; class Transimitter: public Storable{ public: void write();}; class Receiver:public Storable{ public: void write(); }; class Radio:public Transimitter, public Receiver{ void write(); }; void Radio::write(){ Transmitter::write(); Receiver::write(); }// 一种典型应用方式,覆盖函数首先调用基类中的版本,然后做 // 有关派生类的特殊工作 Beijing University of Posts & Telecommunications Beijing [email protected] 15.2.4 虚基类 class Storable{ class Radio: public: public Transmitter, Storable(const char* s); public Receiver{ virtual void read() = 0; public: virtual void write() = 0; void write(); virtual ~Storable(); }; private: const char* store; Storable稍加修 Storable(const Storable&); 改,继承方式需要 Storable& operator=(const Storable&); 改变,以便能处理 }; 一个对象的存储 class Transmitter:public virtual Storable{ 了多个副本的共享 p...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online