Created
May 25, 2016 11:37
-
-
Save sivabudh/b8c733022e4dd5e19caaf5849de3458b to your computer and use it in GitHub Desktop.
Tutorial session with Game about inheritance
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> | |
// An example of function | |
void someFunction(char * data) { | |
int a; | |
char * b; | |
printf(data); | |
} | |
// What is a class? | |
// A class is simply variables + functions (methods) | |
class File { | |
FILE* file; | |
// Method example | |
// What is a method? A method is simply a function that resides within a class | |
void open(char * fileName, char * mode) | |
{ | |
this->file = fopen(fileName, mode); | |
} | |
// Another method example | |
void printf(char * stringToWrite) | |
{ | |
fprintf(this->file, "%s", stringToWrite); | |
} | |
// Yet another method example | |
void close() | |
{ | |
fclose(this->file); | |
} | |
}; | |
void printHelloWorld() | |
{ | |
prinftf("Hello World!"); | |
} | |
// Point 1 | |
int x1; | |
int y1; | |
int x2; | |
int y2; | |
struct Point { | |
int x; | |
int y; | |
}; | |
Point a, b; | |
a.x, a.y; | |
b.x, b.y; | |
class Animal { | |
// attributes | |
int numLegs; | |
Animal() { | |
this->numLegs = 0; | |
} | |
char * walk(); | |
int fly() { | |
return this->numLegs++; | |
} | |
void mutate() { | |
this->numLegs++; | |
} | |
}; | |
Animal * animal = new Animal(); | |
animal->mutate(); | |
printf(animal->numLegs); // 1 | |
animal->mutate(); | |
printf(animal->numLegs); // 2 | |
// | |
// Inheritance | |
// | |
class Bird : public Animal { | |
void sleep() { | |
printf("I'm sleeping"); | |
} | |
}; | |
class Monkey : public Animal { | |
}; | |
class Sparrow : public Bird { | |
// Method override | |
void sleep() { | |
printf("I'm sleeping a very long time"); | |
} | |
}; | |
Bird * bird = new Bird(); | |
bird->walk(); | |
bird->fly(); | |
bird->mutate(); | |
bird->sleep() // I'm sleeping; | |
Animal * animal2 = new Animal(); | |
animal->sleep(); | |
Sparrow * sp = new Sparrow(); | |
sp->sleep(); // I'm sleeping a very long time | |
class Animal; | |
class Bird : Animal; | |
class Tiger : Animal; | |
class StrangeCreature : Bird, Tiger; | |
using namespace std; | |
int main(int argc, char *argv[]) { | |
int a = 0; | |
File * f1 = new File(); | |
File * f2 = new File(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment