Skip to content

Commit 63e278f

Browse files
committed
polymorphism
1 parent e8f42e4 commit 63e278f

6 files changed

Lines changed: 168 additions & 0 deletions

File tree

helloWorld/classTemplate.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TEMPLATE_H
2+
#define TEMPLATE_H
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
class TEMPLATE {
8+
public:
9+
TEMPLATE();
10+
11+
protected:
12+
13+
private:
14+
};
15+
16+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef ARITHPROGRESSION_H
2+
#define ARITHPROGRESSION_H
3+
4+
#include "Progression.h"
5+
#include <iostream>
6+
using namespace std;
7+
8+
class ArithProgression : public Progression {
9+
public:
10+
ArithProgression(long increment = 1) : Progression(), increment(increment) {}
11+
12+
protected:
13+
long increment;
14+
15+
protected:
16+
virtual long nextValue() {
17+
current += increment;
18+
return current;
19+
}
20+
};
21+
22+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef FibonacciProgression_H
2+
#define FibonacciProgression_H
3+
4+
#include "Progression.h"
5+
#include <iostream>
6+
using namespace std;
7+
8+
class FibonacciProgression : public Progression {
9+
public:
10+
FibonacciProgression(long first = 0, long second = 1)
11+
: Progression(first), second(second), previous(second - first)
12+
{}
13+
14+
protected:
15+
long second;
16+
long previous;
17+
18+
protected:
19+
virtual long firstValue() {
20+
current = first;
21+
previous = second - first;
22+
return current;
23+
}
24+
25+
virtual long nextValue() {
26+
long temp = previous;
27+
previous = current;
28+
current += temp;
29+
return current;
30+
}
31+
};
32+
33+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef GEOMPROGRESSION_H
2+
#define GEOMPROGRESSION_H
3+
4+
#include "Progression.h"
5+
#include <iostream>
6+
using namespace std;
7+
8+
class GeomProgression : public Progression {
9+
public:
10+
GeomProgression(long base = 2) : Progression(1), base(base) {}
11+
12+
protected:
13+
long base;
14+
15+
protected:
16+
virtual long nextValue() {
17+
return current *= base;
18+
}
19+
};
20+
21+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef PROGRESSION_H
2+
#define PROGRESSION_H
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
class Progression {
8+
public:
9+
Progression(long first = 0) : first(first), current(first) {}
10+
11+
virtual ~Progression() {}
12+
13+
void printProgression(int n) {
14+
cout << firstValue();
15+
for (size_t i = 2; i <= n; i++) {
16+
cout << ' ' << nextValue();
17+
}
18+
cout << endl;
19+
}
20+
21+
protected:
22+
long first;
23+
long current;
24+
25+
protected:
26+
virtual long firstValue() {
27+
current = first;
28+
return current;
29+
}
30+
virtual long nextValue() {
31+
return ++current;
32+
}
33+
};
34+
35+
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
1+
#include "Progression.h"
2+
#include "ArithProgression.h"
3+
#include "GeomProgression.h"
4+
#include "FibonacciProgression.h"
15
#include <iostream>
26

37
using namespace std;
48

59
int main(int argc, char const *argv[]) {
10+
Progression* prog;
611

12+
// test Progression
13+
cout << "Base progression with default increment:" << endl;
14+
prog = new Progression();
15+
prog->printProgression(10);
16+
cout << "Base progression with increment 5:" << endl;
17+
prog = new Progression(5);
18+
prog->printProgression(10);
19+
cout << endl;
20+
21+
// test ArithProgression
22+
cout << "Arithmetic progression with default increment:" << endl;
23+
prog = new ArithProgression();
24+
prog->printProgression(10);
25+
cout << "Arithmetic progression with increment 5:" << endl;
26+
prog = new ArithProgression(5);
27+
prog->printProgression(10);
28+
cout << endl;
29+
30+
// test GeomProgression
31+
cout << "Geometric progression with default base:\n";
32+
prog = new GeomProgression();
33+
prog->printProgression(10);
34+
cout << "Geometric progression with base 3:\n";
35+
prog = new GeomProgression(3);
36+
prog->printProgression(10);
37+
cout << endl;
38+
39+
// test FibonacciProgression
40+
cout << "Fibonacci progression with default start values:\n";
41+
prog = new FibonacciProgression();
42+
prog->printProgression(10);
43+
cout << "Fibonacci progression with start values 4 and 6:\n";
44+
prog = new FibonacciProgression(4, 6);
45+
prog->printProgression(10);
46+
cout << endl;
47+
748
return 0;
849
}

0 commit comments

Comments
 (0)