CHAPTER OUTLINE
CHAPTER FOUR
OBJECTS AND CLASSES
-6 HOURS
4.1 C++ Classes
4.2 Access Specifiers
4.3 Objects and the Member Access
4.4 Defining Member Function
4.5 Constructor
4.5.1 Default Constructor
4.5.2 Parameterized Constructor
4.5.3 Copy Constructor
4.6 Destructors
4.7 Object as Function Arguments and Return Type
4.8 Array of Objects
4.9 Pointer to Objects and Member Access
4.10 Dynamic Memory Allocation for Objects and Object Array
4.11 this Pointer
4.12 static Data Member and static Function
4.13 Constant Member Functions and Constant Object
4.14 Friend function and Friend class
3
C++ Classes
When you define a class, you define a blueprint for a data type. This doesn't
actually define any data, but it does define what the class name means, that is,
what an object of the class will consist of and what operations can be
performed on such an object.
A class definition starts with the keyword class followed by the class name;
and the class body, enclosed by a pair of curly braces. A class definition must
be followed either by a semicolon or a list of declarations.
The syntax for class definition is given below:
class class_name
{
private:
variable_declarations;
function declarations;
public:
variable_declarations;
function declarations;
protected:
variable_declarations;
function declarations;
};
4
Access Specifiers
→ The labels private, public and protected are known as access specifiers.
→ The access specifier controls the visibility of the member variable and
functions within the class to the outside functions and classes.
→ The members which are declared public are visible to every
functions/classes outside the class.
→ The members which are declared private are visible within the class.
→ Only member function can access the data in such case.
→ protected has special use in inheritance which will be discussed in the
respective chapter.