Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Interfaces/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*Pure virtual function in C++ is the key feature that lets you create interfaces (or abstract classes that behave like interfaces).
What is a Pure Virtual Function?
A pure virtual function is a virtual function that:

Has = 0 at the end
Has no implementation in the base class (or sometimes just a dummy one)
Forces every derived (child) class to provide its own implementation
we cant instantiate the interface class [only sub classes we can and have to]
Interfaces are just classes in c++ with unimplemented methods acting as template of source unlike other languages as java or c#*/


#include <iostream>
#include <string>

class Printable

{
public :
virtual std::string GetClassName() = 0;//now every class have to contain GetClassName function to implement

};

class Entity : public Printable//made a sub class
{
public :
virtual std::string GetName() { return "Entity"; }//by removing body and equating to 0 makes a pure virtal funtion
std::string GetClassName() override { return "Entity"; }

};

class Player : public Entity //no need of printable
{
private :
std::string m_Name;
public :
Player(const std::string& name)
: m_Name(name) {}

std::string GetName() override { return m_Name; }
std::string GetClassName() override { return "Player"; }
};

void PrintName(Entity* entity)
{
std::cout << entity->GetName() << std::endl;
}

class A : public Printable
{
public :
std::string GetClassName() override {return "A"; }

};


void Print(Printable* obj)//this is for printing names of class and type which has GetClassName funtion and make Printable a pointer
{
std::cout << obj->GetClassName() << std::endl;
}
int main()
{

Entity* e = new Entity();//cant instantiate the Entity class if it is pure virtual funtion and we need to give a sub class where the funtion is implemented eg Player("")
// PrintName(e);


Player* p = new Player("Suhas");
//PrintName(p);

Print(e);
Print(p);
Print(new A());//it is a memory leak



std::cin.get();
}
//we can only instantiate the class if it actually has all those pure virtual functions implemented[all funtions implemented]
Binary file added Interfaces/main.exe
Binary file not shown.
50 changes: 50 additions & 0 deletions Virtual functions/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <string>

class Entity
{
public :
virtual std::string GetName() { return "Entity"; }//getName is going to return string

};

class Player : public Entity
{
private :
std::string m_Name;//stores a name
public :
Player(const std::string& name)//constructor allows to specify a name
: m_Name(name) {}

std::string GetName() override { return m_Name; }//Mark the overriden function with keyword override and this helps the funtion came from the base class
};

void PrintName(Entity* entity)
{
std::cout << entity->GetName() << std::endl;
}


int main()
{

Entity* e = new Entity();
PrintName(e);
//std::cout << e->GetName() <<std::endl;

Player* p = new Player("Suhas");
PrintName(p);

//std::cout << p->GetName() << std::endl;


//Entity* entity = p;//p is pointer to a player type but this is a instance in the player class
//std::cout << entity->GetName();//we expect player to be printed

std::cin.get();
}

//v table contain a mapping for all the virtual functions in our base class so we can map it to correct overwritten function at runtime
//if i want to override a funtion you need to mark the base funtion in the base class as virtual
//virtual funtions need extra memory to store v table to dispatch to the right funtion includes member pointer in the base class
//and if we call virtual funtion we have to go through table to see which matches [additional performace penalty] ]
45 changes: 45 additions & 0 deletions Virtual functions/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//binding means which functions to call
//if not virtual it is static binding : The compiler decides at compile time which function to call, based only on the type of the pointer/reference, not the actual object it points to.
//if it is virtual it is dynamic or runtime binding means the decision of which function to call happens at runtime, based on the actual type of the object.
//Made possible by virtual functions and the hidden vtable mechanism

//virtual functions allows up to ovverride the methods in the sub classes
//if parent class has some virtual function then sub class can ovverride it to change the method

#include <iostream>
#include <string>

class Entity
{
public :
std::string GetName() { return "Entity"; }//getName is going to return string

};

class Player : public Entity
{
private :
std::string m_Name;//stores a name
public :
Player(const std::string& name)//constructor allows to specify a name
: m_Name(name) {}

std::string GetName() { return m_Name; }//return the name member
};


int main()
{

Entity* e = new Entity();
std::cout << e->GetName() <<std::endl;//created a entity and try to print the getname

Player* p = new Player("Suhas");
std::cout << p->GetName() << std::endl;
//no need to bothering of objects as program terminates it anyway

std::cin.get();
}

//everything goes bad whenever i start introduce polymorphism
//referring player like its not entity
Binary file added Visibility/.gitignore
Binary file not shown.