Created
January 30, 2015 00:33
-
-
Save c0ldlimit/b1baeb7efd2d3ad698f1 to your computer and use it in GitHub Desktop.
#lecture4 cpp
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
class State; // forward declare a class - it exists but not tell you what's in it | |
// must declare it so that we can use it in Node but State uses Node so can't put them both first | |
class Node { | |
public: | |
Node(State *s) : state(s) {} | |
void process(); | |
State *state; // always need to work with pointers or references to objects with members - | |
//could be a question state or an answer state - don't know how big the state object will be - | |
// can't just copy objects because presumably something may inherit from that class | |
// A a; says there is a object of type A right on the stack | |
// A a = b; slicing copy on the stack | |
// whenever you don't know the real type of an object you need to pass it around by reference or address - don't want to passs | |
// it around by value | |
}; | |
class State { | |
public: | |
virtual void process(Node * node) = 0; // pure virtual method -abstract class - cannot instantiate | |
virtual ~State() {} // designed for inheritance | |
} | |
// design pattern called Delegation "prefer delegation to inheritance" | |
// Node delegates processing to state processor | |
// why use getline? | |
// >> "extraction" treats space as a delimeter | |
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
#include <iostream> | |
struct Greeter { | |
public: | |
Greeter() { | |
cout << "Hello world" << endl; | |
} | |
} | |
Greeter greeter; | |
int main() { | |
return 0; | |
} |
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
#include "pure_virtual.h" | |
// A's constructor will call A's f method indirectly through g() | |
// what happens is if you look in the table - sticks fake pointers in that throws up a fatal error |
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
struct A { | |
A(); | |
virtual void f() = 0; | |
void g(); | |
} | |
// there is a time in constructing B that you have A object but no B object | |
// have to create A subobject - for that instant there is an A subobject | |
// | |
struct B : public A { | |
void f() { | |
cout << "B's f" << endl; | |
} | |
} |
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
struct Nth_Power { | |
Nth_Power(int x) : n(x) {} | |
double operator()(double d) { | |
return pow(d, n); | |
} | |
private: | |
int n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment