C++ Programming: Classes and Objects (Part II)

In part II of my illustration on C++ Classes & Objects, I will give some code snippets as examples:

Example of Class declaration:

class String
String(char *aString=0); //Constructor
~String(); //Destructor
int GetLength() const; //Member function
const char* GetContent() const; //Member function
void SetContent(const char* aString) //Member function
int m_nlength; //Data member
char* m_szContent; //Data member

Examples of Member functions implementation:

int String::GetLength() const
return m_nLength;

Object Instantiation

#include “String.h”
/* Create object using memory from stack */
String S1(“Hello”);
/* Create object using memory from heap */
String* pS1;
pS1 = new String(“Hello”);
cout << pS1->GetContent() << endl;

Derived classes:

class subClassName : public baseClassName
//private member declarations
//public member declarations