Skip to content

Instantly share code, notes, and snippets.

@vishal-keshav
Last active January 26, 2019 16:00
Show Gist options
  • Save vishal-keshav/e5ed97bb611599091b5af07410c47104 to your computer and use it in GitHub Desktop.
Save vishal-keshav/e5ed97bb611599091b5af07410c47104 to your computer and use it in GitHub Desktop.
On Object oriented programming and design patterns in C++ part 12

Design Principles Part 12

Vtables

Vtables are the feature of C++. Vtables are used for late binding, and constains some extra information about the created class.

#include "stdafx.h"


using namespace std;

/*
Class Information (this is used by dynamic_cast)
----- I1-Vtable -----
&I1::F1  -at offset 0
&I1::F2 - at offset 8
---------------------
*/

class I1
{
private:
	//void *vptr is added automatically, once there is one virtual function // this is instance data memeber added by compiler
	int x, y;
public:
	// I1(this) {this->vptr = &I1_Vtable;} // This is constructor call added by compiler
	virtual void F2(){}
};


/*
Class Information (this is used by dynamic_cast)
----- I1-Vtable -----
&I1::F1  -at offset 0
&A::F2 - at offset 8
---------------------
*/

class A : public I1
{
private:
	int z;
public:
	// A(this) {this->vptr = &A_Vtable}
	void F2(){

	}
};

int main()
{
	I1 *p = new A();
	p->F1();
	// Get the value of p => address of the object
	// Get vptr from the object => address of the vtable
	// Add offset 0 to the address of VTable => new address
	// get 4 bytes : get the address of function
	// jump address of function (address of the A object)
	p->F2();
		// Get the value of p => address of the object
	// Get vptr from the object => address of the vtable
	// Add offset 0 to the address of VTable => new address
	// get 4 bytes : get the address of function
	// jump address of function (address of the I1 object)
	return 0;
}

State design pattern

Below code represents a case of state pattern.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

using namespace std;

/*class NotepadWindow
{
public:
	void Close(){
		// If new file - Mode/States
			// If new text is typed by user
				// Show prompt - Save, Dont Save, Cancel
				// If Cancel
					// Close prompt and go back to notepad window
				// If Dont save
					// Close prompt and close notepad window
				// If save
					// Show save dialogue - Savc and cancel
					// If cancel
						// close save dialogue and go back to notepad window
					// If save
						// Save data to file and close save dialogu and go to notepad window
			// else
				// Close the window
		// If Existing File - Mode/States
			// If new text is typed by user
				// Show prompt - Save, Dont Save, Cancel
				// If Cancel
					// Close prompt and go back to notepad window
				// If Dont save
					// Close prompt and close notepad window
				// If save
					// Save data to file and close save dialogu and go to notepad window
	}
};*/

// State desing pattern

class State
{
public:
	virtual void close() = 0;
};

class NewFileState : public State
{
	NotepadWindow *np;
public:



	void close(){

	}
};

class ExistingFileState : public State
{
public:
	void close(){

	}
};



class NotepadWindow
{
public:
	NewFileState nfs;
	ExistingFileState efs;
	State *currentState;
private:
	NotepadWindow(){
		currentState = &nfs;
	}
	void Close(){
		currentState->close();
	}
	void ChangeToNew(){
		currentState = &nfs;
	}
	void ChangeToExisting(){
		currentState = &efs;
	}
};

int main()
{
	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment