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;}

原创粉丝点击