-
Notifications
You must be signed in to change notification settings - Fork 33
/
18.26.cpp
59 lines (53 loc) · 1.05 KB
/
18.26.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Exercise 18.26: Given the hierarchy in the box on page 810, why is the
* following call to print an error? Revise MI to allow this call to print to
* compile and execute correctly.
*
* MI mi;
* mi.print(42);
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <string>
#include <vector>
struct Base1 {
void print(int) const
{ std::cout << "Base1::print(int)\n"; }
protected:
int ival;
double dval;
char cval;
private:
int *id;
};
struct Base2 {
void print(double) const
{ std::cout << "Base2::print(double)\n"; }
protected:
double fval;
private:
double dval;
};
struct Derived : public Base1 {
void print(std::string) const
{std::cout << "Derived::print(std::string)\n"; }
protected:
std::string sval;
double dval;
};
struct MI : public Derived, public Base2 {
void print(std::vector<double>) const
{ std::cout << "MI::print(vector<double>)\n"; }
void print(int) const
{ std::cout << "MI::print(int)\n"; }
protected:
int *ival;
std::vector<double> dvec;
};
int main()
{
MI mi;
mi.print(42);
return 0;
}