Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C++ Classes and Objects

The classes and objects are the most important features of C++. A Class is similar to a structure but it provides more advanced features. A structure is a collection of data items (which may be of different types or similar types). On the other hand, a class is a collection of data items and functions. The functions are written to perform various operations on the data items.


A class is defined for creating objects of the same type. In a class, all attributes (or data items) and functions are defined. The data items of a class may be simple variables, arrays, pointers, structures, or objects of other classes etc. The data items of a class are called data members. The functions of a class are called member functions. The functions are also sometimes referred to as methods.


The objects are created according to a defined class. A defined class serves as a template for creating objects. It is like a blueprint of a building or project. Out of a blueprint, a builder can build many houses One blueprint can be reused to build many houses of the same type. Similarly, one defined class can be used to create many objects of the same type.


When objects are created into computer memory then each object belonging to a particular class has a private copy of the variables defined in the class. There is only one copy of functions (or methods) for all objects of that class.


For example, if class “Temp" is defined with two functions "Input" & “Show” and four data members “Name”, “Code”, “Address”, “Marks”. Suppose two objects “A” and “B” are created for class “Temp”. The representation of objects A & B and the functions “input()” & “Show()” into memory is shown in the following image:


Defining a Class

A class is a blueprint of an object. A class defines the types of data that will be held in an object, and defines the code for the member functions.


A class is defined in a similar way as a structure is defined. The keyword “class” is used to define a class. Like a structure, the body of the class is terminated with a semicolon (;).


The general syntax to define a class is given below:


class class__name

{

   body of the class

 };



where
class:  It is a keyword of C++ which is used to define the class.

class_name: It represents the name of class. It is similar to tag of a structure. The objects are created by this name The rules to give the name of a class and a variable are same.

body of class: The body of class contains the definitions of data members and member functions. These are called members of the class. These are written between braces ( {} ).

semicolon (;): The body of a class always ends with semicolon (;).

Example:


class abc

{

private:

int roll_no;

char name[15];

public;

void input(void)

{

cout>roll_no;

cout
cin>>name;

}

void print (void)

cout
};




In this example, the class “abc” is defined. It contains two data members and two member functions. The member functions are directly defined inside the class and the prototypes (declarations) of these functions are omitted. We can also give only the prototypes of the functions inside the class. The definitions of member functions can be placed outside the main() function.


The member function “input()” is defined to input values in the data members while “print()” function is defined to print the values of data members.


Member Access Specifiers

The commands that determine whether a member of a class can be "accessed from outside the class or not are called member access specifiers. These are used to control the accessing of members of a class.

In C++, there are three member access specifiers that can be used in a class:


  • private
  • public
  • protected

What is Private Specifier

The ‘private’ specifier is used to restrict the access of a class members within the class. The members of the class that follow the "private" specifier can only be accessed from within the class. They cannot be accessed from outside the class. By default, access specifier is private. Thus if no access specitier is given, all the members are treated as private.


Normally all data members are declared as ‘private’. The member functions can also be declared as ‘private’ if we want to use the member functions within the class. Making a data to be accessed from within the class is called data hiding.


What is Public Specifier

The ‘public’ specifier is used to allow the access of class members outside the class as well as within the class. The members of the class that follow the ‘public’ specifier can be accessed from inside and from outside the class.

Normally, member functions are declared as ‘public’. It is because the member functions provide the services to access data from object. This set of services form the public interface of the class. Usually, the member functions of the class associated with objects are called for execution from the main() function or from any other function.

However, there is no rule that data members must be ‘private’ and member functions “public . It is only used to fulfill the property of data hiding. The data hiding is the powerful feature of object-oriented programming. We can specify data members as ‘public‘ and member functions as “private’.


What is Protected specifier


A protected access specifier is a stage between private and public access. It is used to allow the access of class members and friend functions of that class.


  No access specifier is used in structure. By default, access specifier for a structure (or struct) is 'public’. It is because we can access the data elements from outside the structure. It means that structure has no data-hiding property.


Declaring Objects of Class


The objects of a class are declared in the similar way as structure variables are declared. A defined class is also like a data type. Once a class is defined, the class name becomes a new data type specifier (like int, float, double & char).


When a class is defined, no space is reserved for it in the memory. It only provides the information to compiler how its objects are created. When any object of a class is created, a memory space is reserved for that object.


The general syntax to declare objects is as follows:

class_name obj1, obj2,.... ,;



Where:

  • class_name: It represents the name of the class whose type of objects is to be created.
  • obj1, obj2,...: They represents the name of objects to be created of same class type. They are written separated by commas (,).

Such as, to declare objects of class "abc", the declaration statement will be:
abc s1, s2;


In this statement, the objects ‘s1’ and s2’ will be created of the class “abc”. When objects of a class are created, separate memory spaces are reserved for each object. The objects hold their own data members but the member functions of a class are stored at only one place in memory. However, all objects of the same class use the same code of member functions. In case of inline functions, the actual code of member functions are inserted at each place where member functions are called for execution. By default, the member functions defined inside the class are treated as inline functions.


The class “abc” has two data members and two member functions. The logical representation of objects ‘s1 ’ and ‘s2’ in memory is shown in the following image.


Accessing Class Members

The "private" class members can only be accessed within the class whereas "public" class members can be accessed within the class and outside the class (such as anywhere in the program).


Within the class, the class members are accessed only with the reference of their names. The class members are accessed outside the class in the similar way as the data members of structure are accessed. The public members of the class associated with a specitic object are accessed by using the dot operator (.). The dot operator is also called the class member access operator.


To access a member of a specific class, the object name, the dot operator and then the member of class is written. In case of member function, the parentheses are also used followed by the member function name. The arguments of member function are given inside parentheses (if function uses arguments).

The general syntax to access a class member is as follows:

object_name .member_name [([arguments])];



Suppose the "s1" is the name of the object of class "abc" and "input" is the name of member function. To access the "input" member function, following statement will be use:

s1 .input();



C++ Example Programs of Classes & Objects


Example program 1: This program inputs a date and displays on the screen using class.


#include

#include

class ddd

{

   private:

   int y, m, d;

   public:

   void getdate (void)

{

   cout>y;

   cout>m;

   cout>d;

}

void printdate (void)

    cout
    cout
   }

};

main ()

{

   ddd dt;

   clrscr();

   dt .getdate();

   dt .printdate();

   getch();

}




In this program, the class "ddd" has three data members y, m, & d of "int" type and two member functions:
  1. getdate()
  2. printdate()
The definitions of both the functions are inside the class "ddd". The member function "getdate()" inputs the values of year, month and day. The member function "printdate()" displays the date format.


This post first appeared on Programming Explain, please read the originial post: here

Share the post

C++ Classes and Objects

×

Subscribe to Programming Explain

Get updates delivered right to your inbox!

Thank you for your subscription

×