Skip to content

Commit

Permalink
Merge pull request Mooophy#496 from eipi10ydz/master
Browse files Browse the repository at this point in the history
add ex18.25.cpp
  • Loading branch information
Mooophy authored Sep 25, 2017
2 parents 8c28a18 + c98de4b commit 4bf55ae
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 20 deletions.
22 changes: 2 additions & 20 deletions ch18/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,7 @@ delete pb;//Ok, part of interface
>define a virtual member named print and a virtual destructor. From these base
>classes we derive the following classes, each of which redefines the print function.
```cpp
class D1 : public Base1 {/* ... */};
class D2 : public Base2 {/* ... */};
class MI : public D1, public D2 {/* ... */};

//Using the following pointers, determine which funciton is used in each call:

Base1 *pb1 = new MI;
Base2 *pb2 = new MI;
D1 *pd1 =new MI;
D2 *pd2 = new MI;
```
(a), (b), and (c) will all call the MI version of print.
All of the pointer class types contain virtual versions of print so, they will look for derived definitions of print.
(d) The MI destructor is called then the base1 destructor is called.
(e) The MI destructor, then the D1 destructor, then the base 1 destructor is called.
(f) The MI destructor, then the D2 destructor, then the Base2 destructor is called.
[cpp](./ex18.25.cpp "Exercise 18.25")

## Exercise 18.26

Expand Down Expand Up @@ -319,4 +301,4 @@ protected:
char cval;//need to qualify ambiguous with other cval.
};
class VMI : public Derived1, public Derived2 { };
```
```
105 changes: 105 additions & 0 deletions ch18/ex18.25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/***************************************************************************
* @file ex18.25.cpp
* @author eipi10
* @date 15 Aug 2016
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 18.25
// Assume we have two base classes, Base1 and Base 2, each of which define a
// virtual member named print and a virtual destructor. From these base classes
// we derive the following classes, each of which redefines the print function.
// Answer:
// (a)MI
// (b)MI
// (c)MI
// (d)MI, D2, Base2, D1, Base1
// (e)MI, D2, Base2, D1, Base1
// (f)MI, D2, Base2, D1, Base1
// (d), (e), (f) will call all the destructor, for "Destructors are always
// invoked in the reverse order from which the constructors are run." in
// Page 805

// gcc 6.1.0 x86_64-pc-linux-gnu
// results:
// pb1 print...........
// Print from MI
// pd1 print...........
// Print from MI
// pd2 print...........
// Print from MI
// delete pb2...........
// MI
// D2
// Base2
// D1
// Base1
// delete pd1...........
// MI
// D2
// Base2
// D1
// Base1
// delete pd2...........
// MI
// D2
// Base2
// D1
// Base1

#include <iostream>

struct Base1
{
/* ... */
virtual void print(){std::cout << "Print from Base1" << std::endl;}
virtual ~Base1(){std::cout << "Base1" << std::endl;}
};
struct Base2
{
/* ... */
virtual void print(){std::cout << "Print from Base2" << std::endl;}
virtual ~Base2(){std::cout << "Base2" << std::endl;}
};

struct D1 : public Base1
{
/* ... */
void print() override {std::cout << "Print from D1" << std::endl;}
~D1() override {std::cout << "D1" << std::endl;}
};
struct D2 : public Base2
{
/* ... */
void print() override {std::cout << "Print from D2" << std::endl;}
~D2() override {std::cout << "D2" <<std::endl;}
};
struct MI : public D1, public D2
{
/* ... */
void print() override {std::cout << "Print from MI" << std::endl;}
~MI() override {std::cout << "MI" << std::endl;}
};

//Using the following pointers, determine which funciton is used in each call:

int main()
{
Base1 *pb1 = new MI;
Base2 *pb2 = new MI;
D1 *pd1 =new MI;
D2 *pd2 = new MI;
std::cout << "pb1 print..........." << std::endl;
pb1 -> print();
std::cout << "pd1 print..........." << std::endl;
pd1 -> print();
std::cout << "pd2 print..........." << std::endl;
pd2 -> print();
std::cout << "delete pb2..........." << std::endl;
delete pb2;
std::cout << "delete pd1..........." << std::endl;
delete pd1;
std::cout << "delete pd2..........." << std::endl;
delete pd2;
}

0 comments on commit 4bf55ae

Please sign in to comment.