Skip to content

Commit 1a9ff1e

Browse files
committed
update chapter16 codes
1 parent e2d9e05 commit 1a9ff1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2016
-1
lines changed

chapter14/.DS_Store

0 Bytes
Binary file not shown.

chapter14/assignment/.DS_Store

2 KB
Binary file not shown.

chapter14/assignment/14.7.4assign.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ class BadDude: public Gunslinger, public PokerPlayer
8282
void Set();
8383
};
8484

85-
#endif
85+
#endif

chapter15/context/15.12error5.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <string>
4+
#include "exc_mean.h"
5+
6+
7+
class demo
8+
{
9+
private:
10+
std::string word;
11+
public:
12+
demo(const std::string & str)
13+
{
14+
word = str;
15+
std::cout << "demo " << word << " created" << std::endl;
16+
}
17+
18+
~demo()
19+
{
20+
std::cout << "demo " << word << " destoryed" << std::endl;
21+
}
22+
23+
void show() const
24+
{
25+
std::cout << "demo " << word << " lives!" << std::endl;
26+
}
27+
};
28+
29+
double hmean(double a, double b);
30+
double gmean(double a, double b);
31+
double means(double a, double b);
32+
33+
34+
int main()
35+
{
36+
using std::cout;
37+
using std::cin;
38+
using std::endl;
39+
40+
double x, y, z;
41+
{
42+
demo d1("found in block in main()");
43+
cout << "Enter two numbers: ";
44+
while(cin >> x >> y)
45+
{
46+
try{
47+
z = means(x, y);
48+
cout << "The mean mean of " << x << " and " << y << " is " << z << endl;
49+
cout << "Enter next pair: ";
50+
}
51+
catch(bad_hmean & bg)
52+
{
53+
bg.mesg();
54+
cout << "Try again. \n";
55+
continue;
56+
}
57+
catch(bad_gmean & hg)
58+
{
59+
cout << hg.mesg();
60+
cout << "Values used: " << hg.v1 << ", " << hg.v2 << endl;
61+
cout << "Sorry, you don't get to play any more." << endl;
62+
break;
63+
}
64+
}
65+
66+
d1.show();
67+
}
68+
69+
cout << "Bye!" << endl;
70+
cin.get();
71+
cin.get();
72+
return 0;
73+
74+
}
75+
76+
77+
double hmean(double a, double b)
78+
{
79+
if (a == -b)
80+
throw bad_hmean(a, b);
81+
return 2.0 * a * b / (a + b);
82+
}
83+
84+
85+
double gmean(double a, double b)
86+
{
87+
if (a < 0 || b < 0)
88+
throw bad_gmean(a, b);
89+
return std::sqrt(a * b);
90+
}
91+
92+
93+
double means(double a, double b)
94+
{
95+
double am, hm, gm;
96+
demo d2("found in means()");
97+
am = (a + b) / 2.0;
98+
try
99+
{
100+
hm = hmean(a, b);
101+
gm = gmean(a, b);
102+
}
103+
catch (bad_hmean & bg)
104+
{
105+
bg.mesg();
106+
std::cout << "Caught in means()" << std::endl;
107+
throw;
108+
}
109+
110+
d2.show();
111+
return (am + hm + gm) / 3.0;
112+
}

chapter15/context/15.17rtti1.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <ctime>
4+
5+
6+
using std::cout;
7+
8+
class Grand
9+
{
10+
private:
11+
int hold;
12+
public:
13+
Grand(int h = 0): hold(h) {}
14+
virtual void Speak() const {cout << "I'm a grand class!\n";}
15+
virtual int Value() const {return hold; }
16+
17+
};
18+
19+
20+
class Superb: public Grand
21+
{
22+
public:
23+
Superb(int h = 0): Grand(h) {}
24+
void Speak() const { cout << "I am a superb class!!\n"; }
25+
virtual void Say() const
26+
{cout << "I hold the superb value of " << Value() << "!\n";}
27+
28+
};
29+
30+
31+
class Magnificent: public Superb
32+
{
33+
private:
34+
char ch;
35+
public:
36+
Magnificent(int h = 0, char c = 'A'): Superb(h), ch(c){}
37+
void Speak() const {cout << "I an a magnificent class !!!\n";}
38+
void Say() const { cout << "I hold the character " << ch << " and the integer " << Value() << "!\n"; }
39+
};
40+
41+
42+
Grand * GetOne();
43+
44+
int main()
45+
{
46+
std::srand(std::time(0));
47+
Grand * pg;
48+
Superb * ps;
49+
for(int i = 0; i < 5; i++)
50+
{
51+
pg = GetOne();
52+
pg->Speak();
53+
if (ps = dynamic_cast<Superb * > (pg))
54+
ps->Say();
55+
}
56+
57+
return 0;
58+
}
59+
60+
61+
Grand * GetOne()
62+
{
63+
Grand * p;
64+
switch(std::rand() % 3)
65+
{
66+
case 0:
67+
p = new Grand(std::rand() % 100);
68+
break;
69+
case 1:
70+
p = new Superb(std::rand() % 100);
71+
break;
72+
case 2:
73+
p = new Magnificent(std::rand() % 100, 'A' + std::rand() % 26);
74+
break;
75+
76+
}
77+
78+
return p;
79+
}

chapter15/context/15.18rtti2.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <ctime>
4+
#include <typeinfo>
5+
6+
7+
using std::cout;
8+
9+
class Grand
10+
{
11+
private:
12+
int hold;
13+
public:
14+
Grand(int h = 0): hold(h) {}
15+
virtual void Speak() const {cout << "I'm a grand class!\n";}
16+
virtual int Value() const {return hold; }
17+
18+
};
19+
20+
21+
class Superb: public Grand
22+
{
23+
public:
24+
Superb(int h = 0): Grand(h) {}
25+
void Speak() const { cout << "I am a superb class!!\n"; }
26+
virtual void Say() const
27+
{cout << "I hold the superb value of " << Value() << "!\n";}
28+
29+
};
30+
31+
32+
class Magnificent: public Superb
33+
{
34+
private:
35+
char ch;
36+
public:
37+
Magnificent(int h = 0, char c = 'A'): Superb(h), ch(c){}
38+
void Speak() const {cout << "I an a magnificent class !!!\n";}
39+
void Say() const { cout << "I hold the character " << ch << " and the integer " << Value() << "!\n"; }
40+
};
41+
42+
43+
Grand * GetOne();
44+
45+
int main()
46+
{
47+
std::srand(std::time(0));
48+
Grand * pg;
49+
Superb * ps;
50+
for(int i = 0; i < 5; i++)
51+
{
52+
pg = GetOne();
53+
std::cout << "Now processing type " << typeid(*pg).name() << ".\n";
54+
pg->Speak();
55+
if (ps = dynamic_cast<Superb * > (pg))
56+
ps->Say();
57+
if(typeid(Magnificent) == typeid(*pg))
58+
std::cout << "Yes, you're really magnificent.\n";
59+
}
60+
61+
return 0;
62+
}
63+
64+
65+
Grand * GetOne()
66+
{
67+
Grand * p;
68+
switch(std::rand() % 3)
69+
{
70+
case 0:
71+
p = new Grand(std::rand() % 100);
72+
break;
73+
case 1:
74+
p = new Superb(std::rand() % 100);
75+
break;
76+
case 2:
77+
p = new Magnificent(std::rand() % 100, 'A' + std::rand() % 26);
78+
break;
79+
80+
}
81+
82+
return p;
83+
}

chapter15/context/15.19constcast.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
using std::endl;
5+
6+
void change(const int * pt, int n);
7+
8+
9+
int main()
10+
{
11+
int pop1 = 38282;
12+
const int pop2 = 2000;
13+
14+
cout << "pop1, pop2: " << pop1 << ", " << pop2 << endl;
15+
change(&pop1, -103);
16+
change(&pop2, -103);
17+
18+
cout << "pop1, pop2: " << pop1 << ", " << pop2 << endl;
19+
}
20+
21+
22+
void change(const int * pt, int n)
23+
{
24+
int *pc;
25+
pc = const_cast<int *>(pt);
26+
*pc += n;
27+
}

chapter15/context/15.1tv.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include "15.1tv.h"
3+
4+
5+
bool Tv::volup()
6+
{
7+
if(volume < MaxVal)
8+
{
9+
volume++;
10+
return true;
11+
}
12+
else
13+
return false;
14+
}
15+
16+
17+
bool Tv::voldown()
18+
{
19+
if(volume > MinVal)
20+
{
21+
volume--;
22+
return true;
23+
}
24+
else
25+
return false;
26+
}
27+
28+
void Tv::chanup()
29+
{
30+
if(channel < maxchannel)
31+
channel++;
32+
else
33+
channel = 1;
34+
}
35+
36+
37+
void Tv::chandown()
38+
{
39+
if(channel > 1)
40+
channel--;
41+
else
42+
channel = maxchannel;
43+
}
44+
45+
46+
void Tv::settings() const
47+
{
48+
using std::cout;
49+
using std::endl;
50+
51+
cout << "Tv is " << (state == Off ? "Off": "On") << endl;
52+
53+
if(state == On)
54+
{
55+
cout << "Volume setting = " << volume << endl;
56+
cout << "Channel setting = " << channel << endl;
57+
cout << "Mode = " << (mode == Antenna ? "antenna": "cable") << endl;
58+
cout << "Input = " << (input == TV ? "TV": "DVD") << endl;
59+
}
60+
}

0 commit comments

Comments
 (0)