-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter1Book.cpp
More file actions
73 lines (63 loc) · 1.28 KB
/
Chapter1Book.cpp
File metadata and controls
73 lines (63 loc) · 1.28 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <array>
using namespace std;
//page 21
class Class1{
public:
virtual void f(){
cout << "function f() in class1" << endl;
}
void g(){
cout << "function g() in class1" << endl;
}
};
class Class2{
public:
virtual void f(){
cout << "function f() in class2" << endl;
}
void g(){
cout << "function g() in class2" << endl;
}
};
class Class3{
public:
virtual void h(){
cout << "function h() in class3" << endl;
}
};
double f(double x){
return 2*x;
}
double *f1(double i){
//returns a pointer
}
double sum(double (*f)(double), int n, int m){
double result = 0;
for(int i=n; i<=m; i++)
result += f(i);
return result;
}
int main(){
// cout << f(3); // pointer to function
//cout << (*f)(7); // function call
// cout << *f; // the function itself
//cout << sum(f,1,5) << endl;
//page 21 Objects and polymorphism
Class1 object1, *p;
Class2 object2;
Class3 object3;
p= &object1;
p->f();
p->g();
p = (Class1*) &object2;
p->f();
p->g();
//possible abnormal behaviour with class3
p = (Class1*) &object3;
p->f(); //abnormal behaviour, function h from class 3 is called.
p->g();
//p->h(); //there is no h function in class1
}