diff --git a/Arrays/Heap.cpp b/Arrays/Heap.cpp new file mode 100644 index 0000000..5460ede --- /dev/null +++ b/Arrays/Heap.cpp @@ -0,0 +1,56 @@ +//there is no way of knowing the size of array in c++;but there is a way cause when you delete this heap memry compiler needs to know the size of array but its comprimising + +#include +#include + +class Entity +{ + public : + + static const int exampleSize = 5;//constexpr will explained later + int* example[exampleSize];//suppose we changed it to a heap we first get the Entity memory adress which leads to the array its a jumping across the memory so [better to switch it to stack] + + std::array another; + + Entity() + { + + + int a[5]; + + int count = sizeof(a) / sizeof(int);//size means bytes ,count means no of elements + + //example->size();//we cant do like this like some other languages for heap in Entity class + for(int i = 0; i < another.size(); i++) + example[i] = 2;//constructor which initializes + + } +}; + +int main() +{ + + Entity e; + + /*int example[5]; + for(int i = 0; i < 5; i++) + example[i] = 2;*///we moved into the Entity class + + + //int* another = new int[5]; + //for(int i = 0; i < 5; i++) + //another[i] = 2; + + + //delete[] another; + + + std::cin.get(); +} +//this is based on heap allocated memory with new keyword +//we need to manually delete this +//stack created it will ends when we get out of curly bracket int example[5] +//heap created more over the stack cause of the life times most probably suppose if we need to return the array inside of our funtion + +//in c++ 11 there is called standard array which has inbuilt datastructures used rather than the normal raw array +//it includes bounds checking and it keeps tracking size of arrray \ No newline at end of file diff --git a/Arrays/Heap.exe b/Arrays/Heap.exe new file mode 100644 index 0000000..af48917 Binary files /dev/null and b/Arrays/Heap.exe differ diff --git a/Arrays/Log.cpp b/Arrays/Log.cpp new file mode 100644 index 0000000..af4a080 --- /dev/null +++ b/Arrays/Log.cpp @@ -0,0 +1,23 @@ +#include + +int main() +{ + int example[5]; + int* ptr = example; + + + for (int i = 0; i < 5; i++) + //example[i] = 2;//this is a text an offset to this memory of array + + + + example[2] = 5;//for index 2 its start at 9th byte we are writting an offset of 8 bytes from a pointer + *(ptr + 2) = 6;//we are dereferencing this pointer way of writting and changing value from 5 to 6 + *(int*)((char*)ptr + 8) = 6;//this is the way of writting in single bit[char] and even in int data type of 4 bytes + + + std::cin.get(); +} + +//arrays are always stored in rows ; each integer of 4 bytes total 20 bytes; +//array is just a pointer here it is a integer pointer \ No newline at end of file diff --git a/Arrays/Log.exe b/Arrays/Log.exe new file mode 100644 index 0000000..4ca6081 Binary files /dev/null and b/Arrays/Log.exe differ diff --git a/Arrays/main.cpp b/Arrays/main.cpp new file mode 100644 index 0000000..3e80083 --- /dev/null +++ b/Arrays/main.cpp @@ -0,0 +1,27 @@ +//in java we have no modifier = default modifier +//in c# we have such as internal[these are topics in visibility] +/*Arrays in C++ are a collection of multiple elements of the same type stored in contiguous memory locations (one after another).[in rows] +They are one of the most basic and important data structures in C++.*/ +//lua language starts from index 1;c++ is 0 +//its like having multiple variables in a single variable +//arrays mostly work with for loops[indexable loops];without it we need to set manually the example array + +#include + +int main() +{ + int example[5]; //array type and name mentioned its contain 5 integers + example[0] = 2; + example[4] = 4;//last index + + example[-1] = 5; + example[17] = 21;//we get memory access violation [in debug mode we get errors not in release mode] + + + std::cout << example[0] << std::endl;//with mentioning index, we get underlying data type + std::cout << example << std::endl;//actual array print memory adress cause its a pointer type + + + + std::cin.get(); +} diff --git a/Interfaces/main.cpp b/Interfaces/main.cpp new file mode 100644 index 0000000..b0b1fbd --- /dev/null +++ b/Interfaces/main.cpp @@ -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 +#include + + 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] \ No newline at end of file diff --git a/Interfaces/main.exe b/Interfaces/main.exe new file mode 100644 index 0000000..cd09c06 Binary files /dev/null and b/Interfaces/main.exe differ diff --git a/Virtual functions/Log.cpp b/Virtual functions/Log.cpp new file mode 100644 index 0000000..4bdae24 --- /dev/null +++ b/Virtual functions/Log.cpp @@ -0,0 +1,50 @@ +#include +#include + + 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() <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] ] \ No newline at end of file diff --git a/Virtual functions/main.cpp b/Virtual functions/main.cpp new file mode 100644 index 0000000..b33a416 --- /dev/null +++ b/Virtual functions/main.cpp @@ -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 +#include + + 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() <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 \ No newline at end of file diff --git a/Visibility/.gitignore b/Visibility/.gitignore new file mode 100644 index 0000000..14419a1 Binary files /dev/null and b/Visibility/.gitignore differ diff --git a/Visibility/Log.cpp b/Visibility/Log.cpp new file mode 100644 index 0000000..2638ba5 --- /dev/null +++ b/Visibility/Log.cpp @@ -0,0 +1,42 @@ +#include +#include + +class Entity +{ + protected : + int X, Y; + + + void Print(){} + + +public : + Entity() + { + X = 0; + Print(); + } + +}; + +class Player : public Entity//Player is a subclass of Entity +{ + public : + Player() + { + X = 2; + Print(); + } +}; + +int main() +{ + Entity e; + e.X = 2; + e.Print();//its completly different funtion and is outside of class and not even part of sub class + + std::cin.get(); +} + +//protected means parent and sub classes along can acccess the symbols +//public means we know everything can access the subjects ina class diff --git a/Visibility/main.cpp b/Visibility/main.cpp new file mode 100644 index 0000000..3c00ef7 --- /dev/null +++ b/Visibility/main.cpp @@ -0,0 +1,49 @@ +/*Visibility in C++ refers to access specifiers — they control who can see and use (access) the members (variables, functions, etc.) of a class. +In simple words: +Visibility decides which parts of your class are public, private, or protected — i.e., who is allowed to touch them.*/ +//classes are generally private and structs are generally public +//this visibility has no affect on performance or running or the program and generation of code [not related with CPU] +//there are even called friends which can access the private + +#include +#include + +class Entity +{ + private : + int X, Y; + + + void Print(){} + + +public : + Entity() + { + X = 0;//constructor + Print(); + } + +}; + +class Player : public Entity//sub class of Entity +{ + public : + Player() + { + X = 2;// Even this constructor cant access the data cause it is private + Print(); + } +}; + +int main() +{ + Entity e; + e.X = 2;// we cant access the subjects from a priavte class + e.Print(); + + std::cin.get(); +} + +//protected is more visible than Private and less visible than public +//we keep classes public and private according to goal of not breaking the code [such as UIs]USER INTERFACE \ No newline at end of file