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
56 changes: 56 additions & 0 deletions Arrays/Heap.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <array>

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<int, 5> 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
Binary file added Arrays/Heap.exe
Binary file not shown.
23 changes: 23 additions & 0 deletions Arrays/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>

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
Binary file added Arrays/Log.exe
Binary file not shown.
27 changes: 27 additions & 0 deletions Arrays/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

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();
}
49 changes: 49 additions & 0 deletions Instantiating--objects/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <string>

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
61 changes: 61 additions & 0 deletions Instantiating--objects/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <string>

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*/

Binary file added Instantiating--objects/main.exe
Binary file not shown.
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.
69 changes: 69 additions & 0 deletions Member initialiser lists/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <string>

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
Binary file added Member initialiser lists/Log.exe
Binary file not shown.
Loading