Skip to content

Instantly share code, notes, and snippets.

@c0ldlimit
Created January 30, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c0ldlimit/b1baeb7efd2d3ad698f1 to your computer and use it in GitHub Desktop.
Save c0ldlimit/b1baeb7efd2d3ad698f1 to your computer and use it in GitHub Desktop.
#lecture4 cpp
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
#include <iostream>
struct Greeter {
public:
Greeter() {
cout << "Hello world" << endl;
}
}
Greeter greeter;
int main() {
return 0;
}
#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
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;
}
}
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