Skip to content

Commit a802acd

Browse files
author
csern
committed
added drill01-07 for chapter 12
1 parent 34b75c0 commit a802acd

File tree

27 files changed

+370
-0
lines changed

27 files changed

+370
-0
lines changed

Chapter12/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 12: Class Design
2+
3+
- [Drill](#drill-1)
4+
- [Review](#review-1)
5+
- [Terms](terms.txt)
6+
- [Exercises](#exercise-1)
7+
8+
## [Drill 1](drill/01)
9+
Define a class `B1` with a virtual function `vf()` and a non-virtual function `f()`. Define both of these functions within class `B1`. Implement each function to output its name (e.g., `B1::vf()`). Make the functions `public`. Make a `B1` object and call each function.
10+
11+
## [Drill 2](drill/02)
12+
Derive a class `D1` from `B1` and override `vf()`. Make a `D1` object and call `vf()` and `f()` for it.
13+
14+
## [Drill 3](drill/03)
15+
Define a reference to `B1` (a `B1&`) and initialize that to the `D1` object you just defined. Call `vf()` and `f()` for that reference.
16+
17+
## [Drill 4](drill/04)
18+
Now define a function called `f()` for `D1` and repeat 1–3. Explain the results.
19+
20+
## [Drill 5](drill/05)
21+
Add a pure virtual function called `pvf()` to `B1` and try to repeat 1–4. Explain the result.
22+
23+
## [Drill 6](drill/06)
24+
Define a class `D2` derived from `D1` and override `pvf()` in `D2`. Make an object of class `D2` and invoke `f()`, `vf()`, and `pvf()` for it.
25+
26+
## [Drill 7](drill/07)
27+
Define a class `B2` with a pure virtual function `pvf()`. Define a class `D21` with a `string` data member and a member function that overrides `pvf()`; `D21::pvf()` should output the value of the `string`. Define a class `D22` that is just like `D21` except that its data member is an `int`. Define a function `f()` that takes a `B2&` argument and calls `pvf()` for its argument. Call `f()` with a `D21` and a `D22`.

Chapter12/drill/01/B1.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef B1_H
2+
#define B1_H
3+
4+
#include <iostream>
5+
6+
struct B1 {
7+
virtual void vf() const {
8+
std::cout << "B1::vf()\n";
9+
}
10+
void f() const {
11+
std::cout << "B1::f()\n";
12+
}
13+
};
14+
15+
#endif

Chapter12/drill/01/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "B1.h"
2+
3+
int main() {
4+
B1 b;
5+
6+
b.vf();
7+
b.f();
8+
}

Chapter12/drill/02/B1.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef B1_H
2+
#define B1_H
3+
4+
#include <iostream>
5+
6+
struct B1 {
7+
virtual void vf() const {
8+
std::cout << "B1::vf()\n";
9+
}
10+
void f() const {
11+
std::cout << "B1::f()\n";
12+
}
13+
};
14+
15+
#endif

Chapter12/drill/02/D1.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef D1_H
2+
#define D1_H
3+
4+
#include "B1.h"
5+
6+
struct D1 : public B1 {
7+
void vf() const override {
8+
std::cout << "D1::vf()\n";
9+
}
10+
};
11+
12+
#endif

Chapter12/drill/02/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "D1.h"
2+
3+
int main() {
4+
D1 d;
5+
6+
d.vf();
7+
d.f();
8+
}

Chapter12/drill/03/B1.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef B1_H
2+
#define B1_H
3+
4+
#include <iostream>
5+
6+
struct B1 {
7+
virtual void vf() const {
8+
std::cout << "B1::vf()\n";
9+
}
10+
void f() const {
11+
std::cout << "B1::f()\n";
12+
}
13+
};
14+
15+
#endif

Chapter12/drill/03/D1.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef D1_H
2+
#define D1_H
3+
4+
#include "B1.h"
5+
6+
struct D1 : public B1 {
7+
void vf() const override {
8+
std::cout << "D1::vf()\n";
9+
}
10+
};
11+
12+
#endif

Chapter12/drill/03/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "D1.h"
2+
3+
int main() {
4+
D1 d;
5+
6+
d.vf();
7+
d.f();
8+
9+
B1& bref = d;
10+
bref.vf();
11+
bref.f();
12+
}

Chapter12/drill/04/B1.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef B1_H
2+
#define B1_H
3+
4+
#include <iostream>
5+
6+
struct B1 {
7+
virtual void vf() const {
8+
std::cout << "B1::vf()\n";
9+
}
10+
void f() const {
11+
std::cout << "B1::f()\n";
12+
}
13+
};
14+
15+
#endif

0 commit comments

Comments
 (0)