C++ - Class
In C++, class is a keyword. Using class, we can implement all object oriented programming language concepts. All the members declared in C++ classes are private by default. We can also have protected access level to for derived classes and public access level for any functions or classes.
Three access levels are,
1. Private - Can be accessed only by the members of the struct or class
2. Protected - Can be accessed by the struct and its derived struct or class
3. public - Can be accessed by any other struct or class or function.
In C++, class is a keyword. Using class, we can implement all object oriented programming language concepts. All the members declared in C++ classes are private by default. We can also have protected access level to for derived classes and public access level for any functions or classes.Three access levels are,1. Private - Can be accessed only by the members of the struct or class2. Protected - Can be accessed by the struct and its derived struct or class3. public - Can be accessed by any other struct or class or function
class MyString
{
private:
char *m_pString;
public:
MyString()
{
std::cout << "Calling Default Constructor\n";
m_pString = NULL;
}
~MyString()
{
if( this->m_pString != NULL)
{
std::cout << "Calling Destructor\n";
delete this->m_pString;
this->m_pString = NULL;
}
}
MyString(const char *p)
{
std::cout << "Calling Parameterized Constructor\n";
int len = strlen(p);
m_pString = new char [len + 1];
strcpy(m_pString, p);
}
MyString(const MyString &other)
{
std::cout << "Calling Copy Constructor\n";
m_pString = other.m_pString;
//int len = strlen(other.m_pString);
//m_pString = new char [len + 1];
//strcpy(m_pString, other.m_pString);
}
const MyString& operator = (const MyString &other)
{
std::cout << "Calling assignment operator\n";
int len = strlen(other.m_pString);
m_pString = new char [len + 1];
strcpy(m_pString, other.m_pString);
return *this;
}
operator const char*()
{
return this->m_pString;
}
};
|