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

In this 3rd installment of my own little C++ tutorial, I will talk about classes and objects in C++.

What are Classes ?
• A user-defined type
• A collection of variables combined with a set of related functions
• An extension of the concepts “struct”
• Mechanism used in C++ for data abstraction

A C++ class has the following associated attributes:
• Access Control
• Constructors and Destructors
• A collection of data members
• A collection of member functions
• An associated class tag name

Access Control
• Private (Only by own member functions)
• Protected (By own and child functions)
• Public (By all)

• member functions with the same name as the class (classname)
• support the automatic initialisation of objects at their point of declaration
• can take arguments
• do not have return types

• member function (~classname)
• automatically invoked when an object goes out of scope
• cannot take arguments
• does not have return type

Data Members

Data members are states or attributes of an object

An Example:
class String
int flag = 0; // error !!! no explicit initialization
int length; // Data member
char *content; // Data member
} ;

Member Functions
• Member functions are public interface
• Member functions have full access privilege to the public, protected and private members of the class
• Member functions are defined within the scope of their class