diff --git a/Arrays/Heap.cpp b/Arrays/Heap.cpp new file mode 100644 index 0000000..2dfc0de --- /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 memory 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 which is constant expression + 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;//type and size + + 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..416b6d9 --- /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 int 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/Arrow Operator/Log.cpp b/Arrow Operator/Log.cpp new file mode 100644 index 0000000..5eab9ef --- /dev/null +++ b/Arrow Operator/Log.cpp @@ -0,0 +1,53 @@ +#include +#include + +class Entity +{ +public : + int x; + + public : + void Print() const //need a const method to process on later + { + std::cout << "Hello!" << std::endl; + } +}; + +class scopedptr +{ +private : + Entity* m_Obj; + public : + scopedptr(Entity* entity) + :m_Obj(entity) + { + } + + ~scopedptr() + { + delete m_Obj; + } + + //Entity* GetObject() { return m_Obj; } + Entity* operator->()//we are overloading it + { + return m_Obj; + } + + const Entity* operator->() const + { + return m_Obj; + } +}; + + + +int main() +{ + const scopedptr entity = new Entity();//like Entity* entity = new Entity(); and for const scoped we need const version of the overloading + //entity.GetObject()->Print(); + entity->Print(); + + std::cin.get(); +} + diff --git a/Arrow Operator/main.cpp b/Arrow Operator/main.cpp new file mode 100644 index 0000000..a620f94 --- /dev/null +++ b/Arrow Operator/main.cpp @@ -0,0 +1,56 @@ +#include +#include + +class Entity +{ +public : + int x; + + public : + void Print() const + { + std::cout << "Hello!" << std::endl; + } +}; + +class scopedptr +{ +private : + Entity* m_Obj; + public : + scopedptr(Entity* entity) + :m_Obj(entity) + { + } + + ~scopedptr() + { + delete m_Obj; + } +}; + + + +int main() +{ + + Entity e; + e.Print(); + + + Entity* ptr = &e; + + ptr->Print();//DEREF Entity ptr + + ptr->x = 2; + //Entity& entity = *ptr;//DEREF + //ptr.Print();//JUST A POINTER or we can do (*ptr).Print(); + //entity.Print(); + + + + std::cin.get(); +} + +// ABOUT HOW ARROW OPERATOR AFFECTS STRUCT AND CLASS POINTERS +// AS an Operator we can overload it and use it on our own custom classes \ No newline at end of file diff --git a/Arrow Operator/offset.cpp b/Arrow Operator/offset.cpp new file mode 100644 index 0000000..a71ec7b --- /dev/null +++ b/Arrow Operator/offset.cpp @@ -0,0 +1,26 @@ +#include +#include + +struct Vector3 +{ + float x, y, z;//if x,z,y we have different layout in memory even though same kinda class +}; + + +int main() +{ + int offset = (int)&((Vector3*)0)->y;//kinda give me a invalid memory without ref and after it will give offset + std::cout << offset << std::endl; + + + /*or + &((Vector3*)nullptr)->x; + */ + + std::cin.get(); +} + +//suppose we need to find offset of this y variable was in the memory +//x is 0 y is 4 and z is 8 bytes in the struct! +//in C++, offset refers to the distance, measured in bytes, from a reference point—typically the beginning of a data structure (like a struct or class)—to a specific member or memory location. +//useful when we serialising data in game engines diff --git a/Copying--Copying Constructor/Deep.cpp b/Copying--Copying Constructor/Deep.cpp new file mode 100644 index 0000000..64fe783 --- /dev/null +++ b/Copying--Copying Constructor/Deep.cpp @@ -0,0 +1,94 @@ +//"DEEP COPYING" works through copying constrctor[is a constructor which called for second string when actually copies it ] + +#include +#include +#include + +class String +{ + private : + char* m_Buffer; + unsigned int m_size;//see size + public : + String(const char* string) + { + m_size = strlen(string); + m_Buffer = new char[m_size + 1 ]; + memcpy(m_Buffer, string, m_size); + m_Buffer[m_size] = 0; + + } + + /* String(const String& other)//taking const ref to same class [copy constrctor] and we want memory in ptr not the pointer + : m_Buffer(other.m_Buffer), m_size(other.m_size) + { + + } + this is default copy constrctor in c++ + */ + + String(const String& other) + : m_size(other.m_size) + { + std::cout << "Copied String!" << std::endl; + m_Buffer = new char[m_size + 1];//creating a new buffer + memcpy(m_Buffer, other.m_Buffer, m_size + 1);//same like before string class but a new buffer one also we know previous string have null termination + } + + + // String(const String& other) = delete; then coppy constrctor wont work same like unq ptr + + /* OR + + String(const String& other) + { + memcpy(this, &other, sizeof(String)); + } + */ + + ~String() + { + delete m_Buffer; + } + + char& operator[](unsigned int index) + { + return m_Buffer[index]; + } + + friend std::ostream& operator<<(std::ostream& stream, const String& string); +}; + + +std::ostream& operator<<(std::ostream& stream, const String& string) + { + stream << string.m_Buffer; + return stream; + } + + void PrintString(const String& string)//by attaching ref and const it wont copies again and we cant edit existing string + { + String copy = string;//power of const ref + std::cout << string << std::endl; + } + +int main() +{ + + String string = "Cherno"; + String second = string; + + second[2] = 'a'; + + // std::cout << string << std::endl; + //std::cout << second << std::endl; + + PrintString(string); + PrintString(second);//simply needlessly copying it and we actually copying it 2 more copies + + std::cin.get(); +} + +//AFTER THIS SECOND STRING CANT CHANGE FIRST ONE +//By const ref we can do copies how much we wanted and this time copied String for string SECOND + diff --git a/Copying--Copying Constructor/Log.cpp b/Copying--Copying Constructor/Log.cpp new file mode 100644 index 0000000..5543b4b --- /dev/null +++ b/Copying--Copying Constructor/Log.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +class String//strings made of an array of characters +{ + private : + char* m_Buffer; + unsigned int m_size;//see size + public : + String(const char* string) + { + m_size = strlen(string); + m_Buffer = new char[m_size + 1 ];//with null termination character to get no extra memory adress + + memcpy(m_Buffer, string, m_size);//destination ,souce and the size in bytes and size is + 1 if we not created null termination character down there + m_Buffer[m_size] = 0;//manually created the null termination character + + /*for(int i = 0; i < m_size; i++) + m_Buffer[i] = string[i];//copies character one by one + best way is by memory copy */ + } + ~String() + { + delete m_Buffer; + } + + char& operator[](unsigned int index) + { + return m_Buffer[index]; + } + + friend std::ostream& operator<<(std::ostream& stream, const String& string); //to avoid using another method GetBuffer and use priavte member we need to make a friend +}; + + +std::ostream& operator<<(std::ostream& stream, const String& string) //overloading operator + { + stream << string.m_Buffer; + return stream; + } +int main() +{ + + String string = "Cherno"; + String second = string; + + second[2] = 'a'; + + std::cout << string << std::endl; + std::cout << second << std::endl; + + + std::cin.get(); +} + +//IN this copying compiler takes intial string class members list and copies them into a new memory adress second +//It does a shallow copy means the 2 strings have same char pointer means memory adresss is same in both strings in buffer +//At the end we want to delete the same block of adress twice so that we get error after the termination of {} +//To not to crash we need new char array to store copied string its like both pointing to same memory adress!! +//We need second block which conatain different pointer points a new memory adress! +//IK done through "DEEP COPYING" means not the shallow copying it does copy object Entirely \ No newline at end of file diff --git a/Copying--Copying Constructor/main.cpp b/Copying--Copying Constructor/main.cpp new file mode 100644 index 0000000..0a2a484 --- /dev/null +++ b/Copying--Copying Constructor/main.cpp @@ -0,0 +1,31 @@ +#include +#include + + + struct Vector2 + { + float x, y; + }; +int main() +{ + Vector2* a = new Vector2{ 2, 3 }; + Vector2* b = a; //here this new opearator affects both a and b cause those 2 ptrs points same memory address!! + + //b->x = 2; affects memory adress not pointer + + // b.x = 5;//a.x still 2 just copying a to b but still those 2 points different memory adressses + + + std::cin.get(); +} + +//By assigning one to other variable to other it always copies +//In pointers it copies the memory adress +//You cant re assign the refernces[just changing underlying thing thats pointing to ] + +/* int a = 2 ; + int b = a; + + int b = 3;//still a is 2 + */ + diff --git a/Dynamic Arrays/Vector.cpp b/Dynamic Arrays/Vector.cpp new file mode 100644 index 0000000..8101c81 --- /dev/null +++ b/Dynamic Arrays/Vector.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +struct Vertex +{ + float x, y, z; +}; + +std::ostream& operator<<(std::ostream& stream, const Vertex& vertex) +{ + stream << vertex.x << ", " << vertex.y << ", " << vertex.z; + return stream; +} + +void Funtion(const std::vector& vertices) //by const ref we ensuring we are not copying while linking to Dynamic array +{ + +} + +int main() +{ + std::vector vertices; + vertices.push_back({ 1, 2, 3}); //to add stuff .Add in other languages we have constructor here but we have used init list + vertices.push_back({ 4, 5, 6}); + Funtion(vertices); + + for (int i = 0; i < vertices.size(); i++) + std::cout << vertices[i] << std::endl; + + + //vertices.clear(); //make array size back to 0 + vertices.erase(vertices.begin() + 1 ); //to remove 2nd element we cant write like 2 ,it takes const iterator + + for (const Vertex& v : vertices) //range for loops and with const ref we are not copying data + std::cout << v << std::endl; //by writting after erase it did eliminate 2nd element + + + std::cin.get(); +} + + + + // we need to memtion and name adn if there we even can pass primitive types like int float + // pointers placement depends on which type did it take heap or inline stack [class or struct] on vector + // means we need to use vertex pointers or vertex objects + // it is optimal to store vertex obj than vector ptrs + //cause they are inline and dynamic arrays memory are contigious[in row] + //in vector strings everything need to recopy and reallocate for resizing which is slow,where as in ptrs the actual mem stays intact cause were just holding ptrs and that mem stored in diff parts + // moving instead of copying largely solves this particular issue ,but there still some copying which is not ideal + // as vector is a class we do know size + //in c++ we done through index like normal array ,in java it is .get(); + \ No newline at end of file diff --git a/Dynamic Arrays/main.cpp b/Dynamic Arrays/main.cpp new file mode 100644 index 0000000..567be9d --- /dev/null +++ b/Dynamic Arrays/main.cpp @@ -0,0 +1,30 @@ +#include +#include + +struct Vertex +{ + float x, y, z; +}; + +std::ostream& operator<<(std::ostream& stream, const Vertex& vertex) +{ + stream << vertex.x << ", " << vertex.y << ", " << vertex.z; + return stream; +} + +int main() +{ + //Vertex vertices[5]; + Vertex* vertices = new Vertex[5]; + vertices[4]; + + std::cin.get(); +} + +//this is about templated standard library[a library filled with container types contain data] +//whole thing made of templates and data type the conatainer contains that actually you to decide!! +//you can provide the underlying data type that this datastructure actually hanldles +//it called arraylist rather than vector cause these are dynamic arrays !! +//its a set that doesnt enforce any kind of uniqueness to its actual elments so ,unlike arrays these can resize +//suppose we have 10 elements initially and later its 11 we can resize array from 10 to 11[creates new array copies everything from array 10 and delete old one ] +//ignoring std array for static array and size tied to both stack and heap for continously adding inputs we can create array of very large size [not preferrable] \ No newline at end of file diff --git a/Implicit conversion and Explicit keyword/main.cpp b/Implicit conversion and Explicit keyword/main.cpp new file mode 100644 index 0000000..cc39ca9 --- /dev/null +++ b/Implicit conversion and Explicit keyword/main.cpp @@ -0,0 +1,43 @@ +//Implicit means without explicitly telling what to do [automatically] +//To explicit a constructor we keep explicit keyword before the constructor +//compiler can perform one implicit conversion in our code[without casting] + +#include +#include + +class Entity +{ + private : + std::string m_Name; + int m_Age; + public : + explicit Entity(const std::string& name) + : m_Name(name), m_Age(-1) {} + explicit Entity(int age) + : m_Name("Unknown"), m_Age(age) {} + + +}; + +void PrintEntity(const Entity& entity) +{ + //Printing +} + +int main() +{ + + PrintEntity(22); + //PrintEntity("Cherno"); //Cherno string is not a std string its a const char array of 7 characaters and c++ have to convert 2 [char array to string and string to Entity] + PrintEntity(std::string("Cherno")); + PrintEntity(Entity("Cherno"));//both are the same + + //Entity a ("Cherno"); //or Entity a = Entity("Cherno"); + Entity a = Entity("Cherno");//cant cause iam equating string literal to a Entity object!! + Entity b = 22;//we cannot do when it is explicit we have to do Entity b(22); or Entity b = (Entity)22;[explicitly casting to a Entity] or Entity b = Entity(22); + + std::cin.get(); +} + + +//c++ cannot perform 2 implicit conversions at a time!! \ No newline at end of file diff --git a/Implicit conversion and Explicit keyword/main.exe b/Implicit conversion and Explicit keyword/main.exe new file mode 100644 index 0000000..1838779 Binary files /dev/null and b/Implicit conversion and Explicit keyword/main.exe differ diff --git a/Instantiating--objects/heap.cpp b/Instantiating--objects/heap.cpp new file mode 100644 index 0000000..e7ee710 --- /dev/null +++ b/Instantiating--objects/heap.cpp @@ -0,0 +1,49 @@ +#include +#include + +using string = std::string; + + +class Entity +{ + private : + + string m_Name; + public : + + Entity() : m_Name("Unknown") + { + } + + Entity(const string& name) : m_Name(name) + { + } + + const string& GetName() const + { + return m_Name; + } + +}; + +int main() +{ + + Entity* e; + + { + + Entity* entity = new Entity("Cherno");//we should make an Entity pointer called entity nand this new Entity returns a Entity pointer! and this is common code in c# or java by removing pointer + e = entity;//not copying just assigning[storing memory adress] + std::cout << entity->GetName() << std::endl;//also can be (*entity) dereferncing or with arrow operator + + } + + std::cin.get(); + delete e;//variable name,until this Cherno exits through objects +} + +//in c# there is struct which is value based type kind of allocated on stack eventhough you used new keyword +//in java everything is in heap +//in c# all classes on heap +//with smart pointers we can allocate on heap also get objects get autodeleted after scope like shed pointers \ No newline at end of file diff --git a/Instantiating--objects/main.cpp b/Instantiating--objects/main.cpp new file mode 100644 index 0000000..eb8fd2f --- /dev/null +++ b/Instantiating--objects/main.cpp @@ -0,0 +1,61 @@ +#include +#include + +using string = std::string; + + +class Entity +{ + private : + + string m_Name; + public : + + Entity() : m_Name("Unknown") + { + } + + Entity(const string& name) : m_Name(name) + { + } + + const string& GetName() const + { + return m_Name; + } + +}; + +/*void Function() +{ + int a = 2; + Entity entity = Entity("Cherno");//after this loop this Entity gets destroyed! +}*/ + +int main() +{ + + Entity* e; + { + + // Function();// Stack frame gets created includes all classes and also primitive types and after that loop its all get destroyed! + + Entity entity("Cherno"); //calls default constructor if there are no parameters[null referncce exception in c# or java ] + + //Entity entity = Entity("Cherno");//same as the previous line + + e = &entity; + std::cout << entity.GetName() << std::endl; + } + + std::cin.get(); +} + + +/*we need to instantiate a class [not static] + 2 ways of creating by the type of memory used heap and stack + its all based on lifetime and speedness + 1.stack is autodeleted after the end of loop and its fast + 2.heap have to deleted manually and its slow + also we have an area where our source code lives*/ + \ No newline at end of file diff --git a/Instantiating--objects/main.exe b/Instantiating--objects/main.exe new file mode 100644 index 0000000..04258c9 Binary files /dev/null and b/Instantiating--objects/main.exe differ 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/Local static/Singleton.cpp b/Local static/Singleton.cpp new file mode 100644 index 0000000..bd006b2 --- /dev/null +++ b/Local static/Singleton.cpp @@ -0,0 +1,29 @@ +#include + +class Singleton +{ //WITHOUT STATIC LOCAL + private : + static Singleton* s_Instance; + public : + static Singleton& Get() { return *s_Instance; } + + void Hello() {} + +}; + +Singleton* Singleton::s_Instance = nullptr; //DECLARED + + + +int main() +{ + Singleton::Get().Hello(); + + std::cin.get(); + +} + + + + +//a class should have only one instance in existence \ No newline at end of file diff --git a/Local static/main.cpp b/Local static/main.cpp new file mode 100644 index 0000000..0a46c7b --- /dev/null +++ b/Local static/main.cpp @@ -0,0 +1,33 @@ +#include + +//static int i = 0; //static doesnt matter here by keeping ;problem is this i can be accesable anywhere + +void Funtion() +{ + //static int i = 0; same when we taken int i = 0 outside + + static int i = 0; + i++; + std::cout << i << std::endl; +} + +int main() +{ + Funtion(); + //i = 10; //funtion calls when we keep static var outside + Funtion(); + Funtion(); + Funtion(); + Funtion(); + + std::cin.get(); +} + + +//we can use static in local scope which has 2 different considerations lifetime and scope +//scope means where we can access variable +//in funtion variable is local to the declared funtion +//static local variable gives lifetime until our program ends and only can be accessed inside funtion only +//static in a funtion scope and funtion has same lifetime but different scope in class the var can acccessable in whole class by anything where as in funtion,only accesable in it!! +// without static keyword in funtion we create variable everysingle time we set to 0 and increment by 1 +//we can give static in local scope for not getting accesable by everyone diff --git a/Local static/singleton2.cpp b/Local static/singleton2.cpp new file mode 100644 index 0000000..f1d7563 --- /dev/null +++ b/Local static/singleton2.cpp @@ -0,0 +1,32 @@ +#include + +class Singleton +{ //WITHOUT STATIC LOCAL + + public : + static Singleton& Get() //BY EVERY TIME YOU CALL GET first it construct singleton ionstance then returns existing instance + + { + static Singleton Instance; + return Instance; + + } //WITHOUT STATIC IN FUNTION AS SOON AS SCOPE ENDS SINGLETON DESTROYS[STACK] + + + void Hello() {} + +}; + +int main() +{ + Singleton::Get().Hello(); + + std::cin.get(); + +} + + + + +//a class should have only one instance in existence +//it helps by replacing initialisation funtions where you might call static initialisation funtion at some pt of all ur objects so if use static get methods we can simplify \ No newline at end of file diff --git a/Local static/static.cpp b/Local static/static.cpp new file mode 100644 index 0000000..fa0cd64 --- /dev/null +++ b/Local static/static.cpp @@ -0,0 +1,22 @@ +#include + +void Funtion() +{ + static int i = 0; + i++; + std::cout << i << std::endl; +} + +int main() +{ + Funtion(); + Funtion(); + Funtion(); + Funtion(); + Funtion(); + + std::cin.get(); +} + + +//in this i variable only local to funtion cant be accesable outside diff --git a/Member initialiser lists/Log.cpp b/Member initialiser lists/Log.cpp new file mode 100644 index 0000000..28c758f --- /dev/null +++ b/Member initialiser lists/Log.cpp @@ -0,0 +1,69 @@ +#include +#include + +class Example +{ + public : + Example() + { + std::cout << "Created Entity!" << std::endl; + + } + + Example(int x) + { + + std::cout << "Created Entity with " << x << "!" << std::endl; + + } + +}; + +class Entity +{ + //This is through member initializer list!! + private : + std::string m_Name; + Example m_Example;//created an Entity! like it is in Entity class + + + //int m_Score; + //int x, y, z; + + public : + + Entity() + : m_Example(Example(8)) //we can also use only 8 inside + //: x(0), y(0), z(0)//,m_Score(0) //Initialiser list need to list in order of class members declared!! + { + //m_Name = "Unknown";//ThiS m_Name object constructed twice [default constrfuctor and with unknown parameter] + + m_Name = std::string("Unknown"); + //m_Example = Example(8); + + } + + Entity(const std::string& name) //constructor + : m_Name(name) + { + } + const std::string& GetName() const { + return m_Name; + } + +}; + +int main() +{ + + Entity e0; + //std::cout << e0.GetName() << std::endl; + + /*Entity e1("Cherno"); + std::cout << e1.GetName() << std::endl;*/ + + std::cin.get(); +} + + //this keeps code in the constructor a lot cleaner !! + //but there is funtional differnce specifically applied to classes \ No newline at end of file diff --git a/Member initialiser lists/Log.exe b/Member initialiser lists/Log.exe new file mode 100644 index 0000000..cd8e18d Binary files /dev/null and b/Member initialiser lists/Log.exe differ diff --git a/Member initialiser lists/main.cpp b/Member initialiser lists/main.cpp new file mode 100644 index 0000000..6519971 --- /dev/null +++ b/Member initialiser lists/main.cpp @@ -0,0 +1,39 @@ +//Its a way for us to initialiaze our class member funtions in the constructor + +#include +#include + +class Entity +{ + + private : + std::string m_Name; + public : + + Entity() //Default constructor + { + m_Name = "Unknown"; + } + + Entity(const std::string& name) //constructor + { + m_Name = name; + + } + const std::string& GetName() const { + return m_Name; + } + +}; + +int main() +{ + + Entity e0; + std::cout << e0.GetName() << std::endl; + + Entity e1("Cherno"); + std::cout << e1.GetName() << std::endl; + + std::cin.get(); +} \ No newline at end of file diff --git a/Member initialiser lists/main.exe b/Member initialiser lists/main.exe new file mode 100644 index 0000000..a31d155 Binary files /dev/null and b/Member initialiser lists/main.exe differ diff --git a/Mutable--keyword/Lambda.cpp b/Mutable--keyword/Lambda.cpp new file mode 100644 index 0000000..0994b4b --- /dev/null +++ b/Mutable--keyword/Lambda.cpp @@ -0,0 +1,47 @@ +#include +#include + +class Entity +{ + private : + std::string m_Name; + mutable int m_DebugCount = 0; + + public : + const std::string& GetName() const + { + m_DebugCount++; + return m_Name; + } + +}; + +int main() +{ + + class Entity e ; + e.GetName(); + + int x = 8; + auto f = [=]() mutable + //we can write &x or x or = or & + { + //std::cout << "Hello" << std::endl; + //x++;//by value = we get error we need to get it through local variable so better to use mutable keyword + + /* int y = x; + y++; + std::cout << y << std::endl;*/ + + x++;//we passed it through value through mutable keyword + std::cout << x << std::endl; + + }; + + f(); + //x = 8 cause we pased through value than reference + std::cin.get(); +} + /*lambda is a little through away funtion + you can write and + assign it to the variable quickly */ diff --git a/Mutable--keyword/main.cpp b/Mutable--keyword/main.cpp new file mode 100644 index 0000000..646d9ac --- /dev/null +++ b/Mutable--keyword/main.cpp @@ -0,0 +1,31 @@ +/*the application of mutable mostly with lambdas and const + its like mutable reversing the meaning of const*/ + +#include +#include + +class Entity +{ + private : + std::string m_Name; + mutable int m_DebugCount = 0; + + public : + const std::string& GetName() const//const and ref + //making methods const so not to modify the class + + { + m_DebugCount++;//its for supppose how many times we debugging + return m_Name; + } + +}; + +int main() +{ + + class Entity e ; + e.GetName();//we will do if the method is const + + std::cin.get(); +} diff --git a/Object Lifetime-(stack $ scope lifetimes)/Log.cpp b/Object Lifetime-(stack $ scope lifetimes)/Log.cpp new file mode 100644 index 0000000..cf9b273 --- /dev/null +++ b/Object Lifetime-(stack $ scope lifetimes)/Log.cpp @@ -0,0 +1,54 @@ +#include +#include + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity!" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity!" << std::endl; + } +}; + +class ScopedPtr +{ + private : + Entity* m_Ptr; + public : + ScopedPtr(Entity* Ptr) + : m_Ptr(Ptr) + { + } + + ~ScopedPtr() + { + delete m_Ptr; + } + +}; + + + + +int main() +{ + + + { + + ScopedPtr e = new Entity();//we can also write it in constructor way and it is implicit conversion + Entity* e = new Entity(); //upper code allocated on heap and get destroyed after the scope ends ,scopedptr object allocated on stack! + } + + + std::cin.get(); +} + + +// we can write unq ptr which is a scoped ptr but now lets write in a simple class +// timer class for benchmarking and mutex locking[multiple threads cant access at sametime locks and unlocks after ] are examples of this type of using scopedptrs' diff --git a/Object Lifetime-(stack $ scope lifetimes)/main.cpp b/Object Lifetime-(stack $ scope lifetimes)/main.cpp new file mode 100644 index 0000000..19bca5a --- /dev/null +++ b/Object Lifetime-(stack $ scope lifetimes)/main.cpp @@ -0,0 +1,49 @@ +#include +#include + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity!" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity!" << std::endl; + } +}; + +int* CreateArray(int* array) +{ + + int* array = new int[50];//Heap allocation + //int array[50];//stack allocated + return array;//returning ptr in stack memory +} + +int main() +{ + +int array[50];//copying memory +int* a = CreateArray(array); + + { + //Entity e; + + Entity* e = new Entity(); + } + + + std::cin.get(); +} + + + +// {} THIS IS A SCOPE!! WE HAVE SCOPES FOR CLASSES,EMPTY CLASSES AND FOR IF STATEMENTS! +// JUST DEBUG THE CODE AND VERIFY EACH LINE +// WHILE STACK OR SCOPE ALLOCATION AFTER SCOPE ENDS IT DOES DESTROYS ENTITY +// WHILE HEAP ALLOCATION IT DOES LIVES EVEN AFTER SCOPE ENDS +// BY USING "Scoped ptr" OR SCOPE CLASSES AND UNQ PTR WHICH IS A SMART POINTER WE CAN ALLOCATE ON HEAP AND AUTO DELETE THE MEMORY AFTER SCOPE ENDS +// A CLASS THATS A WRAPPER OVER A POINTER WHICH UPON CONSTRUCTION HEAP ALLOCATES THE POINTER AND THEN UPON DESTRUCTION DELETES THE POINTER \ No newline at end of file diff --git a/Operators -- its overloading/Log.cpp b/Operators -- its overloading/Log.cpp new file mode 100644 index 0000000..f829f51 --- /dev/null +++ b/Operators -- its overloading/Log.cpp @@ -0,0 +1,64 @@ +#include +#include + + +struct Vector2 +{ + float x, y; + + Vector2(float x,float y) + : x(x), y(y) {} + + /*Vector2 Add(const Vector2& other) const + { + return *this + other; const ptr in this case so we need to dereference this this keyword!! + return operator+(other); + + } */ + + + + Vector2 Add(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); + } + + + Vector2 operator+(const Vector2& other) const + { + return Add(other); + } + + /* + + Vector2 operator+(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); we have done reverse of the overloading like creating Add from + + } + */ + + + + Vector2 Multiply(const Vector2& other) const + { + return Vector2(x * other.x, y * other.y ); + } + + Vector2 operator*(const Vector2& other) const + { + return Multiply(other); + } + +}; + +int main() +{ + Vector2 position(4.0f, 4.0f); + Vector2 speed(0.5f, 1.5f); + Vector2 powerup(1.1f, 1.1f); + + + Vector2 result1 = position.Add(speed.Multiply(powerup)); + Vector2 result2 = position + (speed * powerup);//BODMAS +} + diff --git a/Operators -- its overloading/dumb.cpp b/Operators -- its overloading/dumb.cpp new file mode 100644 index 0000000..da63c1e --- /dev/null +++ b/Operators -- its overloading/dumb.cpp @@ -0,0 +1,84 @@ +#include +#include + + +struct Vector2 +{ + float x, y; + + Vector2(float x,float y) + : x(x), y(y) {} + + + Vector2 Add(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); + } + + + Vector2 operator+(const Vector2& other) const + { + return Add(other); + } + + + + + Vector2 Multiply(const Vector2& other) const + { + return Vector2(x * other.x, y * other.y ); + } + + Vector2 operator*(const Vector2& other) const + { + return Multiply(other); + } + + + //equals(),Tostring in case of java + + bool operator==(const Vector2& other) const//EQUATING + { + return x == other.x && y == other.y; + } + + bool operator!=(const Vector2& other) const + { + + // return !operator==(other); not preferrable!!!! + return !(*this == other);//to reverse how it works!! + } + + +}; + + std::ostream& operator<<(std::ostream& stream, const Vector2& other) + { + stream << other.x << ", " << other.y; + return stream; + } + +int main() +{ + Vector2 position(4.0f, 4.0f); + Vector2 speed(0.5f, 1.5f); + Vector2 powerup(1.1f, 1.1f); + + + Vector2 result1 = position.Add(speed.Multiply(powerup)); + Vector2 result2 = position + (speed * powerup);//BODMAS + + //if (!result1.equals(result2))//this is how you equate in a java + if(result1 == result2) + { + + } + + + std::cout << result2 << std::endl;//no overload for << operator to take in an output string and also Vector2 + +} + + +//This is about the leftshift operator!!! + diff --git a/Operators -- its overloading/main.cpp b/Operators -- its overloading/main.cpp new file mode 100644 index 0000000..862cd4a --- /dev/null +++ b/Operators -- its overloading/main.cpp @@ -0,0 +1,40 @@ +#include +#include + + +struct Vector2 +{ + float x, y; + + Vector2(float x,float y) + : x(x), y(y) {} + + Vector2 Add(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); + } + + Vector2 Multiply(const Vector2& other) const + { + return Vector2(x * other.x, y * other.y ); + } + + +}; + +int main() +{ + Vector2 position(4.0f, 4.0f); + Vector2 speed(0.5f, 1.5f); + Vector2 powerup(1.1f, 1.1f);//Multiplies the result!! + + + Vector2 result = position.Add(speed.Multiply(powerup));//without operator overloading + + std::cin.get(); +} + + +//Operators are just funtions!! +//Operator overloading is just giving new funtionality to the operator +//This is not supported on java and partially on c# and use when only its needed! \ No newline at end of file diff --git a/Optimising usage -- (std vector)/Log.cpp b/Optimising usage -- (std vector)/Log.cpp new file mode 100644 index 0000000..b460a7b --- /dev/null +++ b/Optimising usage -- (std vector)/Log.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +struct Vertex +{ + float x, y, z; + + Vertex(float x, float y, float z) + : x(x), y(y), z(z) + { + } + Vertex(const Vertex& vertex) + :x(vertex.x), y(vertex.y), z(vertex.z) + { + std::cout << "Copied!" << std::endl; + } + +}; + +int main() + { + + std::vector vertices; + vertices.reserve(3); + + vertices.emplace_back(1, 2, 3); + vertices.emplace_back(4, 5, 6); + vertices.emplace_back(7, 8, 9); + + std::cin.get(); + } + + +// to optimise vertex that constructing inside main and copied to vector class,just construct in vector class itself +// just pass the parameter list itself in place of vertex object to get constructed in vector class itself +//tells vector construct a vertex object with the following parameters in place in our actual vector memory \ No newline at end of file diff --git a/Optimising usage -- (std vector)/Log.exe b/Optimising usage -- (std vector)/Log.exe new file mode 100644 index 0000000..3046a3a Binary files /dev/null and b/Optimising usage -- (std vector)/Log.exe differ diff --git a/Optimising usage -- (std vector)/main.cpp b/Optimising usage -- (std vector)/main.cpp new file mode 100644 index 0000000..1a68386 --- /dev/null +++ b/Optimising usage -- (std vector)/main.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +struct Vertex +{ + float x, y, z; + + Vertex(float x, float y, float z) + : x(x), y(y), z(z) + { + } + Vertex(const Vertex& vertex) + :x(vertex.x), y(vertex.y), z(vertex.z) + { + std::cout << "Copied!" << std::endl; + } + +}; + +int main() + { + + + std::vector vertices; + vertices.reserve(3); //takes capacity of 3 and its not resizing by copying or giving it in constructor is saying just construct it 3 vertices objects vertices(3) but we want memory + + vertices.push_back(Vertex(1, 2, 3)); + vertices.push_back(Vertex(4, 5, 6)); + vertices.push_back(Vertex(7, 8, 9)); // via defualt constructor + + std::cin.get(); + } + + +//This is about optimising using of std::vector class on reallocation and copying [slow code] +// rules of optimisation is basically knowing your environment +//how do things work and what do i need to do exactly and what should happen +// so now optimising about reducing copying our objects!! especially object storing vector class +// WE NEED TO KNOW WHEN COPY CONSTRCTOR CALLING +// constructing this vertex object on inside of our main funtion stack frame then pushing[copying] it to the vertices vector cause copy so can we construct that vertex in place of actual memory vector allocated to us +// vector resizing twice here one by defualt and 2 when moves to 2nd element and 3 moving to 3rd element +// make enough memory to hold vector from beggining is optimising 2 \ No newline at end of file diff --git a/Smart pointers -- Types/main.cpp b/Smart pointers -- Types/main.cpp new file mode 100644 index 0000000..3d9ff32 --- /dev/null +++ b/Smart pointers -- Types/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity" << std::endl; + } + + void Print() {} + +}; + +int main() +{ + { + //std::unique_ptr entity(new Entity()); + + std::unique_ptr entity = std::make_unique(); //prefferd to call make unique to avoid constructor exceptions[ptr with no ref and thats a memory leak]! + + //std::unique_ptr e0 = entity; copy constructor and assignment operator actually deleted! + + entity->Print(); //EXACTLY SAME AS RAW POINTER + } + + + std::cin.get(); +} + + + +// SMART POINTERS ARE TO AUTO DELETE THE MEMORY ADDRESS THAT IS HEAP ALLOCATED!! +// AND SOME ARE NOT GONNA MENTION NEW AND ALSO DELETE KEYWORDS!! +// SMART POINTERS ARE THE WRAPPER AROUND A REAL RAW POINTER +// MEMORY AUTO DELETES ACCORDING TO THE POINTER YOU USED +// UNQ POINTER IS A SCOPED PTR[YOU CANT COPY UNQ PTR] OR TWO UNQ PTRS POINTING TO SAME BLOCK OF MEMORY AND ONE OF THEM DIES IT FREES THE MEMORY +// THIS IS THE ONLY PTR AND THAT ONLY REF TO THAT PTR YOU ACTUALLY WANTS +// WAY OF WRITTING IS UNQ PTR HAS EXPLICIT CONSTRUCTOR AND FIRST WITH CLASS NAME AND GIVING A OBJECT NAME AND KEEPING IT A CONSTRUCTOR \ No newline at end of file diff --git a/Smart pointers -- Types/shared.cpp b/Smart pointers -- Types/shared.cpp new file mode 100644 index 0000000..c0b0a7d --- /dev/null +++ b/Smart pointers -- Types/shared.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity" << std::endl; + } + + void Print() {} + +}; + +int main() +{ + { + { + std::shared_ptr e0; + { + + + std::shared_ptr sharedEntity = std::make_shared(); + e0 = sharedEntity; + // std::shared_ptr e0 = std::make_shared(); + /*Here this is preferrable due to this shared ptr stores memory in control block + so if you do allocation it allocate first new Entity and then construct the shared + ptr in control block which is 2 allocations*/ + + } //not yet destroyed + } + } + + + std::cin.get();//after this memory frees after all shared ptr dies +} +//shared ptr based on ref counting +//when shared ptr gives to other shared ptr countis 2 +//when all ref summation is 0 the memory frees + diff --git a/Smart pointers -- Types/weak.cpp b/Smart pointers -- Types/weak.cpp new file mode 100644 index 0000000..0d6c634 --- /dev/null +++ b/Smart pointers -- Types/weak.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity" << std::endl; + } + + void Print() {} + +}; + +int main() +{ + { + { + std::weak_ptr e0; + { + std::shared_ptr sharedEntity = std::make_shared(); + // std::weak_ptr weakEntity = sharedEntity;//copy Entity but not increase the ref counting + e0 = sharedEntity; + + } + }//DESTROYED ENTITY + } + + + std::cin.get(); +} + +//shared ptr to shared ptr ref count increases +//shared ptr to weak ptr ref count remain same like you have Entities but you dont take ownership[it wont keep you alive] +//try to use unq ptr and then shared always according to overhead \ No newline at end of file diff --git a/String - Literals/Log.cpp b/String - Literals/Log.cpp new file mode 100644 index 0000000..601ab17 --- /dev/null +++ b/String - Literals/Log.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include + +int main() +{ + + using namespace std::string_literals;//gives no of funtions for convenience + + std::string name0 = std::string("Cherno") + "hello!";//cant do cause you cant add 2 raw literals directly + std::string name0 = "Cherno"s + "hello!";//this s returns a standard string and u8 for normal string and L infront of the string literals for wide string + + + std::u32string name0 = U"Cherno"s + U" hello";//for wide string it is wstring and R for row + + const char* example = R"(Line1 + Line2 + Line3 + Line4)"; + + const char* ex = "Line1\n" + "Line2\n" + "Line3\n"; + + + + const char* name = u8"Cherno";//made with utf 8 similar to remaining ones [ unicode transformation format ] + const wchar_t* name2 = L"Cherno";//wide and this is 2 bytes per character[but variable for such as linux and windows] + + const char16_t* name3 = u"Cherno";//2 bytes per character or 16 bits a character using string + const char32_t* name4 = U"Cherno";//32 bits character or 4 bytes per character + + + + std::cin.get(); +} + +/*string literals are always stored in read only memory + so i cant equate a char pointer to a string litreral + which is a READ ONLY MEMORY + we can use a const char array */ \ No newline at end of file diff --git a/String - Literals/main.cpp b/String - Literals/main.cpp new file mode 100644 index 0000000..9ebf11f --- /dev/null +++ b/String - Literals/main.cpp @@ -0,0 +1,20 @@ +#include +#include + +#include +#include + + /*just a standard c library include 3 funtions and + its a string literal also written as + "Suhas"; 0[this is numeric zero]*/ + +int main() +{ + const char name[8] = "Che\0rno"; + + /*8 cause it even ends with \0*/ + + std::cout << strlen(name) << std::endl; + + std::cin.get(); +} \ No newline at end of file diff --git a/String - Literals/main.exe b/String - Literals/main.exe new file mode 100644 index 0000000..c01c629 Binary files /dev/null and b/String - Literals/main.exe differ diff --git a/Strings - working/Log.cpp b/Strings - working/Log.cpp new file mode 100644 index 0000000..8756b60 --- /dev/null +++ b/Strings - working/Log.cpp @@ -0,0 +1,37 @@ +#include +#include + +void PrintString(const std::string& string)//creating copy of that std::string class + +{ + // string += "h"; by removing the const and the refernce keywords and its just a read only function + std::cout << string << std::endl; + +} + +int main() +{ + std::string name = "Suhas";// + "hello!";process of appending strings + name += "hello!"; + + std::string name = std::string("Suhas") + "hello"; + PrintString(name); + bool contains = name.find("no") != std::string::npos;//to find text in our string + + /*npos is the illegal position for that find + and name.find returns position of that no thing*/ + + + name.size(); + + /*strlen(s) for the length of the string char* s + and strcpy for copying of the strings*/ + + + + std::cout << name << std::endl; + + std::cin.get(); + +} /* string copying in funtions are quite slow + strings can be addable than char ptrs */ \ No newline at end of file diff --git a/Strings - working/main.cpp b/Strings - working/main.cpp new file mode 100644 index 0000000..4679cfd --- /dev/null +++ b/Strings - working/main.cpp @@ -0,0 +1,36 @@ +#include +#include + +int main() +{ + std::string name = "Suhas"; + + /*char pointer[c style of defining] and we keep it const + cause to make string immutable; + will changes to std::string by + using header file of string*/ + + char name2[6] = { 'S', 'u', 'h', 'a', 's', 0 }; + + /*0 or '\0' is the null termination character + string terminates after hitting 0; without it + the string goes outside of allocated memory*/ + + + std::cout << name2 << std::endl; + + + name[2] = 'a'; + + std::cin.get(); + +} + /*Its just the array of chars + char is of 1 byte of memory and + string generally uses char ; + wide strings are 2 bytes per character + std::string is templated version of basic + string class which is templated with char */ + +//char is underlying datatype for each chartacter in std::string +//string has a constructor takes char or const char ptr \ No newline at end of file diff --git a/Strings - working/main.exe b/Strings - working/main.exe new file mode 100644 index 0000000..b79fd31 Binary files /dev/null and b/Strings - working/main.exe differ diff --git a/Terenary--operators/Log.cpp b/Terenary--operators/Log.cpp new file mode 100644 index 0000000..7abfc30 --- /dev/null +++ b/Terenary--operators/Log.cpp @@ -0,0 +1,22 @@ +#include +#include + +static int s_Level = 102; +static int s_Speed = 2; + +int main() +{ + /* if (s_Level > 5) + s_Speed = 10; + else + s_Speed = 5;*/ + + //s_Speed = s_Level > 5 ? 10 : 2; //we need to write the condition first and then ?: + s_Speed = (s_Level > 5 && s_Level < 100) ? s_Level > 10 ? 15 : 10 : 5;//this brackets are initially absent + + + std::cout << s_Speed < +#include + +static int s_Level = 1; +static int s_Speed = 2; + +int main() +{ + /* if (s_Level > 5) + s_Speed = 10; + else + s_Speed = 5;*/ + + //s_Speed = s_Level > 5 ? 10 : 2; //we need to write the condition first and then ?: + s_Speed = s_Level > 5 ? s_Level > 10 ? 15 : 10 : 5; + + + std::string rank = s_Level > 10 ? "Master" : "Beginner";//not construct an intermediate string due to return value optimisation and its faster + + std::string otherRank;//without terenary operator and constructing a temporary string and destroying it! + if(s_Level > 10) + otherRank = "Master"; + + else + otherRank = "Beginner"; + + + std::cin.get(); +} + + + + + diff --git a/Using--Libraries/main.cpp b/Using--Libraries/main.cpp new file mode 100644 index 0000000..821f397 --- /dev/null +++ b/Using--Libraries/main.cpp @@ -0,0 +1,28 @@ +//STATIC LINKING + + +//IN OTHER LANGUAGES ADDING LIBRARIES IS VERY TRIVIAL TASK THROUGH PACAKGE MANAGER WHICH DONWLOAD AND INSTALL OTHER LIBRARIES +//BETTER TO ADD ANOTHER PROJECT CONTAINS YOUR SOURCE CODE OF YOUR DEPENDENCY OF YOUR LIBRARY[COPIES OF PHYSICAL BINARIES OR CODE] AND COMPILE TO STATIC OR DYNAMIC LIBRARY WHERE WE CAN COMPILE AND DEBUG FOR LARGE FILES +//LINKING AGINAST BINARIES IS FAST AND IF YOU DONT HAVE ACCESS TO SOURCE CODE + +//NOW ABOUT LINKING AGINAST BINARIES [GLFW], IN WINDOWS PREVIEW OF BINARIES EXIST UNLIKE LINUX AND MAC + +//ALL ABOUT STATIC LINKING WHICH IS FAST THAN DYNAMIC LINKING + +//INCLUDE HAVE HEADER FILES FUNTIONS IN PREBUILT BINARIES AND LIBRARY HAS PRE BUILT BINARIES +// STATIC PUT LIB PUT IN EXE FILE [LINKER], DYNAMIC IN RUNTIME[SEPARATELY IN ANOTHER PROJECT] + +#include +#include + +//int glfwInit() { return 0; } + + +int main() +{ + + int a = glfwInit();//LINKER ERROR[WE HAVEN'T LINKED LIBRARY ] and after defining the funtion it get no error + + std::cin.get(); +} + 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 diff --git a/const -- types/Log.cpp b/const -- types/Log.cpp new file mode 100644 index 0000000..06ac31f --- /dev/null +++ b/const -- types/Log.cpp @@ -0,0 +1,74 @@ +#include +#include + +class Entity +{ + private : + int m_X, m_Y; + mutable int var; + public : + //const int* const GetX() const //[means cant change the content and cant reassign pointer also cant change member variables of the class through methods] + int GetX() const + { + + var = 2;//suppose it needa to modify keep mutable in front of the variable + return m_X; + } + + int GetX() + { + return m_X; + } + + /*keeping const after method in class means + this method cannot modify actual class and its + belonged variables makes just a + read only method*/ + + void SetX(int x)// const makes no sense its setting + { + m_X = x; + } + +}; + + void PrintEntity(const Entity& e)//pass it by conast refernece to avoid copying [just a read only memory] + { + /* e = nullptr; + e = Entity(); + + its not like pointers + reassigning this object means changing its own + no separation between pointer and contents of the pointer + cause you are the contents in ref + you are the entity though you are a ref + and GetX funtion need to be const otherwise it violates const Entity */ + + std::cout << e.GetX() << std::endl; + + } + + + int main() + { + + Entity e; + const int MAX_AGE = 90; + + const int* const a = new int; + + *a = 2; + a = (int*)&MAX_AGE; + a = nullptr; + + + std::cout << *a << std::endl; + + std::cin.get(); + + + + } + + +//int* m_X, *m_Y //its like both considering pointers \ No newline at end of file diff --git a/const -- types/main.cpp b/const -- types/main.cpp new file mode 100644 index 0000000..ed5e4c7 --- /dev/null +++ b/const -- types/main.cpp @@ -0,0 +1,29 @@ +#include +#include + +int main() +{ + const int MAX_AGE = 5;//this integer is a constant you cannot change it! + + int* a = new int; //created on heap tO acquire a pointer + + *a = 2; + a = (int*)&MAX_AGE; + + int const* a = new int;//contents cant be changed '*a' + const int* a = new int;//same as the above + + int*const a = new int;//cant reassign the pointer 'a' + const int*const a = new int;//contents and assigning of pointer cant be changed + + + + /*dereferencing and equating to 2 [contents] + reassigning pointer to some other memory adress*/ + + std::cout << *a << std::endl;//reading 'a' is fine + + + + std::cin.get(); +} \ No newline at end of file diff --git a/const -- types/main.exe b/const -- types/main.exe new file mode 100644 index 0000000..6918168 Binary files /dev/null and b/const -- types/main.exe differ diff --git a/new--keyword/main.cpp b/new--keyword/main.cpp new file mode 100644 index 0000000..8c1f3c3 --- /dev/null +++ b/new--keyword/main.cpp @@ -0,0 +1,42 @@ +#include +#include + +using string = std::string; + + +class Entity +{ + private : + string m_Name; + public : + Entity() : m_Name("Unknown"){} + Entity(const string& name) : m_Name(name) {} + + const string& GetName() const { return m_Name; } +}; + + +int main() +{ + + int a = 2; + int* b = new int [50]; // 200 bytes and returns a pointer + + Entity* e = new(b) Entity();//returns a void pointer and the aloocated to b memory adress + //Entity* e = (Entity*)malloc(sizeof(Entity));//malloc(50); and this returns a Entity pointer + + //free (e); + + + delete e;//calls destructor and free funtion that "malloc"ed + delete[] b; + +//scope based pointing and ref counting for using it in advanced way!! + + std::cin.get(); +} + +//This new is a heap allocated keyword which returns a pointer +//This can be applied to classes, primitive types and also arrays +//This memory allocated in heap contigously from the freeelist which have the memory adresses +//we cannot take the malloc rather than new Entity(); cause this calls conatructor diff --git a/this--keyword/Log.cpp b/this--keyword/Log.cpp new file mode 100644 index 0000000..7526c34 --- /dev/null +++ b/this--keyword/Log.cpp @@ -0,0 +1,42 @@ +#include +#include + +void PrintEntity(const Entity& e);//DECLARED + +class Entity +{ + public : + int x, y; + + Entity(int x, int y) + { + Entity* e = this; + + this->x = x; + this->y = y; + + Entity& e = *this; + + PrintEntity(*this);//pass the current instance of Entity class with x, y! + + //delete this; avoid this cause you are freeing memory from memeber funtion + } + + int GetX() const + { + const Entity& e = *this; + } +}; + +void PrintEntity(Entity* e)//DEFINED +{ + //PRINTING +} + +int main() +{ + std::cin.get(); +} + + +//supppose we want to call a method which is outside class from within entity class \ No newline at end of file diff --git a/this--keyword/main.cpp b/this--keyword/main.cpp new file mode 100644 index 0000000..a564cf0 --- /dev/null +++ b/this--keyword/main.cpp @@ -0,0 +1,41 @@ +//this keyword only applicable to member funtion[funtion in the class i.e, method!!] +//this is a pointer to the current object instance!! +//in order to call a method we need to instantiate the object! + +#include +#include + +class Entity +{ + public : + int x, y; + + Entity(int x, int y) + //: x(x), y(y) we can use member initisaliser list! without it it cannot be differentiated;so we need to give reference to the class member varaible!! + { + Entity* e = this;//Entity pointer const and we cannot reassign the adrress + //Entity* const& e = this; to assign it to a ref this is the way + // e->x = x; or + this->x = x;//or (* this).x = x; + this->y = y; + } + + int GetX() const + { + + //Entity* e = this; not valid + const Entity* e = this; //in const funtion e->x = 5; cant be done : this is const Entity * const + + return x; + } + + +}; + +int main() +{ + std::cin.get(); +} + + +//IN Const methods this is a const entity constant pointer!! \ No newline at end of file