15.6. Class Scope under Inheritance
来源:互联网 发布:图书软件项目计划书 编辑:程序博客网 时间:2024/05/22 10:36
//15.6. Class Scope under Inheritanceclass Quote {public: Quote() = default; Quote(const string &book, double sales_price): bookNo(book), price(sales_price) { } string isbn() const { return bookNo; } virtual double net_price(size_t n) const { return n * price; } virtual ~Quote() = default;private: string bookNo;protected: double price = 0.0;};class Disc_quote : public Quote {public: Disc_quote() = default; Disc_quote(const string &b, double p, size_t qty, double disc) : Quote(b, p), quantity(qty), discount(disc) { } pair<size_t, double> discount_policy() const { return { quantity, discount }; } virtual double net_price(size_t n) const = 0;protected: size_t quantity = 0; double discount = 0.0;};class Bulk_quote : public Disc_quote {public: Bulk_quote() = default; Bulk_quote(const string &book, double p, size_t qty, double dis) : Disc_quote(book, p, qty, dis) { } double net_price(size_t cnt) const override { if(cnt >= quantity) return cnt * (1 - discount) * price; else return cnt * price; }};class Limited_quote : public Disc_quote {public: Limited_quote() = default; Limited_quote(const string &book, double p, size_t qty, double dis): Disc_quote(book, p, qty, dis) { } double net_price(size_t n) const override { if(n <= quantity) return n*(1-discount) * price; else return n * price; }};double print_total(ostream &os, const Quote &item, size_t n) { double ret = item.net_price(n); os << "ISBN: " << item.isbn() << " # sold: " << n << " total due: " << ret << endl; return ret;}int main() { Bulk_quote bulk; Bulk_quote *bulkP = &bulk; Quote *itemP = &bulk; bulkP->discount_policy(); //error: itemP has type Quote* //the type of itemP is a pointer to Quote, which means that the search for discount_policy starts in class Quote //itemP->discount_policy(); return 0;}//the reference to mem inside get_mem is resoloved to the name inside Derivedstruct Base { Base() : mem(0) { }protected: int mem;};struct Derived : Base { Derived(int i) : mem(i) { } int get_mem() { return mem; }protected: int mem;};int main() { Derived d(42); cout << d.get_mem() << endl; return 0;}//use a hidden base-class member by using the scope operatorstruct Base { Base() : mem(0) { }protected: int mem = 0;};struct Derived : Base { Derived(int i) : mem(i) { } int get_mem() { return Base::mem; }protected: int mem;};int main() { Derived d(42); cout << d.get_mem() << endl; return 0;}//name lookup happens before type checking, once the name is found, the compiler looks no furtherstruct Base { int memfcn() { }};struct Derived : Base { int memfcn(int) { }};int main() { Derived d; Base b; b.memfcn(); d.memfcn(10); //error: memfcn with no arguments is hidden //d.memfcn(); d.Base::memfcn(); return 0;}class Base {public: virtual int fcn() { cout << "Base::fcn\n"; }};class D1 : public Base {public: //hides fcn in the base int fcn(int) { cout << "D1::fcn(int)\n"; } virtual void f2(){ cout << "D1::f2()\n"; }};class D2 : public D1 {public: //hides fcn D1::fcn(int) int fcn(int) { cout << "D2::fcn(int)\n";} //overrides virtual fcn from Base int fcn() { cout << "D2::fcn(int)\n"; } //overrides virtual f2 from D1 void f2() { cout << "D2::f2()\n"; }};int main() { Base bobj; D1 d1obj; D2 d2obj; Base *bp1 = &bobj, *bp2 = &d1obj, *bp3 = &d2obj; //dynamic binding bp1->fcn(); bp2->fcn(); bp3->fcn(); D1 *d1p = &d1obj; D2 *d2p = &d2obj; //bp2->f2(); // error: base has no member named f2() d1p->f2(); d2p->f2(); Base *p1 = &d2obj; D1 *p2 = &d2obj; D2 *p3 = &d2obj; //no dynamic binding // p1->fcn(42); p2->fcn(42); p3->fcn(42); return 0;}Exercises Section 15.6//Exercise 15.23: Assuming class D1 on page 620 had intended to override//its inherited fcn function, how would you fix that class? Assuming you fixed//the class so that fcn matched the definition in Base, how would the calls in//that section be resolved?//remove the parameter int.class Base {public: virtual int fcn() { cout << "Base::fcn\n"; return 0; }};class D1 : public Base {public: int fcn() override { cout << "D1::fcn(int)\n"; return 0; } virtual void f2(){ cout << "D1::f2()\n"; }};class D2 : public D1 {public: int fcn(int) { cout << "D2::fcn(int)\n"; return 0; } int fcn() { cout << "D2::fcn(int)\n"; return 0; } void f2() { cout << "D2::f2()\n"; }};int main() { Base bobj; D1 d1obj; D2 d2obj; Base *bp1 = &bobj, *bp2 = &d1obj, *bp3 = &d2obj; bp1->fcn(); bp2->fcn(); bp3->fcn(); //virtual call D1 *d1p = &d1obj; D2 *d2p = &d2obj; d1p->f2(); d2p->f2(); return 0;}
阅读全文
0 0
- 15.6. Class Scope under Inheritance
- Java - Class Inheritance 090906
- inheritance & class variable DEMO
- CSharp OOP - Inheritance (Class inheritance, Interface implementation)
- release class under ARC
- perl---inheritance of one class
- Prevent class inheritance in C++
- class & inheritance in python demo
- python 学习之---class (inheritance)
- swift class inheritance * some codes
- class之間有三種關係,inheritance,implementation和association
- Class versus Interface Inheritance 类接口继承
- Python object new style class inheritance
- Kotlin-08.类和继承(class/Inheritance)
- 程序段的Class Scope
- Inheritance
- Inheritance
- Inheritance
- eclipse安装JAVA反编译插件
- 阅读笔记(文学)
- 8086/8088 CPU寄存器组
- Spring MVC中关于重定向的路径问题
- ObjectUtils(对象、流相互转换)
- 15.6. Class Scope under Inheritance
- 消息中间件 ActiveMQ基础
- 详解网络安全中防火墙和IDS的作用
- poj 1985 树的直径模板题
- activemq点对点模式
- 准确率 召回率 F值
- URL转Drawable之 Android中获取网络图片的三种方法
- Unity3D手游开发中所有特殊的文件夹
- js 小知识点 (以前的笔记,有点乱)