forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExer18_27.cpp
More file actions
54 lines (54 loc) · 1.16 KB
/
Copy pathExer18_27.cpp
File metadata and controls
54 lines (54 loc) · 1.16 KB
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
#include <string>
#include <vector>
struct Base1 {
void print(int) const; // public by default
protected:
int ival;
double dval;
char cval;
private:
int *id;
};
struct Base2 {
void print(double) const; // public by default
protected:
float fval;
private:
double dval;
};
struct Derived : public Base1 {
void print(std::string) const; // public by default
protected:
std::string sval;
double dval;
};
struct MI : public Derived, public Base2 {
void print(std::vector<double>) const; // public by default
void foo(double);
protected:
int *ival;
std::vector<double> dvec;
};
int ival;
double dval;
void MI::foo(double cval)
{
int dval;
// assign local dval the sum of Base1::dval and Derived::dval
dval = Base1::dval + Derived::dval;
// assign Base2::fval the last element in dvec
if(!dvec.empty())
Base2::fval = dvec.back();
// assign cval from Base1 to the first character in sval from Derived
if(!Derived::sval.empty()) {
Derived::sval.push_back(Base1::cval);
} else {
Derived::sval.front() = Base1::cval;
}
}
int main()
{
MI mi;
mi.foo(0);
return 0;
}