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
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.
39 changes: 39 additions & 0 deletions Member initialiser lists/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//Its a way for us to initialiaze our class member funtions in the constructor

#include <iostream>
#include <string>

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();
}
Binary file added Member initialiser lists/main.exe
Binary file not shown.