-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class FirstClass { | ||
public: | ||
void print1() { | ||
std::cout << "FirstClass::print1()" << std::endl; | ||
} | ||
virtual void print2() { | ||
std::cout << "FirstClass::print2()" << std::endl; | ||
} | ||
void print3() { | ||
print1(); | ||
print2(); | ||
} | ||
}; | ||
|
||
class SecondClass : public FirstClass { | ||
public: | ||
void print1() { | ||
std::cout << "SecondClass::print1()" << std::endl; | ||
} | ||
virtual void print2() { | ||
std::cout << "SecondClass::print2()" << std::endl; | ||
} | ||
}; | ||
|
||
class ThirdClass : public SecondClass { | ||
public: | ||
void print1() { | ||
std::cout << "ThirdClass::print1()" << std::endl; | ||
} | ||
virtual void print2() { | ||
std::cout << "ThirdClass::print2()" << std::endl; | ||
} | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
FirstClass x; | ||
x.print1(); | ||
x.print2(); | ||
x.print3(); | ||
|
||
std::cout << std::endl; | ||
|
||
SecondClass y; | ||
y.print1(); | ||
y.print2(); | ||
y.print3(); | ||
|
||
std::cout << std::endl; | ||
|
||
ThirdClass z; | ||
z.print1(); | ||
z.print2(); | ||
z.print3(); | ||
|
||
return 0; | ||
} | ||
|
||
// FirstClass::print1() | ||
// FirstClass::print2() | ||
// FirstClass::print1() | ||
// FirstClass::print2() | ||
|
||
// SecondClass::print1() | ||
// SecondClass::print2() | ||
// FirstClass::print1() | ||
// SecondClass::print2() | ||
|
||
// ThirdClass::print1() | ||
// ThirdClass::print2() | ||
// FirstClass::print1() | ||
// ThirdClass::print2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class IntegerMapping { | ||
public: | ||
virtual int maps(int x) const = 0; | ||
}; | ||
|
||
class Translation : public IntegerMapping { | ||
public: | ||
Translation(int l) : k(l) {} | ||
int maps(int x) const { | ||
return x+k; | ||
} | ||
private: | ||
int k; | ||
}; | ||
|
||
class Negate : public IntegerMapping { | ||
public: | ||
Negate() {} | ||
int maps(int x) const { | ||
return -x; | ||
} | ||
}; | ||
|
||
class Compose : public IntegerMapping { | ||
public: | ||
Compose(IntegerMapping &n1, IntegerMapping &n2) : m1(&n1), m2(&n2) {} | ||
int maps(int x) const { | ||
return m1->maps(m2->maps(x)); | ||
} | ||
private: | ||
IntegerMapping *m1; | ||
IntegerMapping *m2; | ||
}; | ||
|
||
int weird(IntegerMapping *m, int a, int b) { | ||
return m->maps(a) * m->maps(b); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
Translation t(5); | ||
Negate n; | ||
Compose c(&t, &n); | ||
cout << weird(&c, 15, 16) << endl; | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class IntegerMapping { | ||
public: | ||
virtual int maps(int x) const = 0; | ||
}; | ||
|
||
class Negate : public IntegerMapping { | ||
public: | ||
Negate() {} | ||
int maps(int x) const { | ||
return -x; | ||
} | ||
}; | ||
|
||
class VectorMaping { | ||
public: | ||
virtual int *maps(int *c, int s) const = 0; | ||
}; | ||
|
||
class Shift : public VectorMaping { | ||
public: | ||
Shift(int l) : k(l) {} | ||
int *maps(int *c, int s) const { | ||
int *result = new int[s]; | ||
for (int j = 0; j < s; j++) { | ||
result[j] = c[(j+k)%s]; | ||
} | ||
return result; | ||
} | ||
private: | ||
int k; | ||
}; | ||
|
||
class MetaMaping : public VectorMaping { | ||
public: | ||
MetaMaping(const IntegerMapping &m) : im(&m) {} | ||
int *maps(int *c, int s) const { | ||
int *result = new int[s]; | ||
for (int k = 0; k < s; k++) { | ||
result[k] = im->maps(c[k]); | ||
} | ||
return result; | ||
} | ||
private: | ||
const IntegerMapping *im; | ||
}; | ||
|
||
void print(const VectorMaping &vm, int *c, int s) { | ||
int *t = vm.maps(c, s); | ||
for (int j = 0; j < s; j++) { | ||
std::cout << t[j]; | ||
if (j < s-1) { | ||
std::cout << " "; | ||
} else { | ||
std::cout << std::endl; | ||
} | ||
} | ||
delete[] t; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
int v[] = {1, 2, 3, 4}; | ||
print(Shift(3), v, sizeof(v)/sizeof(int)); | ||
print(MetaMaping(Negate()), v, sizeof(v)/sizeof(int)); | ||
|
||
return 0; | ||
} | ||
|
||
|