Classes in C++ programming

By  Falak Hasija    7 - 28 July, 20
classes-in-cpp-programming

Share article on social media


Classes in C++ proragmming are a combination of data and functions joined together to form a type.

HOW DATA MEMBERS WITHIN A CLASS ARE ACCESSED?

The determination of accessing data members is done by ACCESS SPECIFIERS.

There are three access specifiers that can be used with either the class member data or the member functions:

1. private: When data are declared private, they can be accessed only by functions within the class, that is, they cannot be accessed by functions outside of class.

2. public: When data or functions within a  class must be directly referenced outside of the class, they are declared to be public.

3. protected:  When data or functions are declared protected,  then, members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

 


 

  Declaring a class: To declare a class, we use the keyword class followed by its name.

class class_name {

   void function();

         };

 

Class Objects: 

Instantiation: Defining an object of a class type is called instantiation. 

Creating an object: class_name cn;  //  cn is an object for class class_name.

Accessing object members: To refer to members within an object, we use the member operator, which is simply a period ( . ). The general format for member access is as shown below:

objectID.memberID

                                  cn.function();         //above code

NOTE: Data members can only be accessed when they are public.

The this pointer: The this pointer contains the address of the invoking object so that it can refer to data and other functions in the class.

There are the following three characteristics:

1. It is a constant pointer, which means that it can't be changed.
2. Most of the time it is used implicitly, i.e., it is hidden. 
3. We can use it explicitly when we need to access the current host object. 

 

 Manager Functions:

The manager functions are constructors, copy constructors, and destructors.

  

1. Constructors:  They are the special member functions that are called when an instance of a class is created. 

Note: A class may have more than one constructor.

Format: All constructors follow two basic rules:

 1. The name of the function is the name of the class. Thus, the constructor for the class_name must be class_name. 

 2. The function must have no return type, even void.

 The basic format of the constructor is:

className ( parameter list ); //declaration

className :: className ( parameter list)    //definition

{

 .................

}

 

   Default constructor: A default constructor is a constructor that can be called without any arguments. 

2. Copy constructor: Copy constructor is a function that is called whenever a copy of an existing instance needs to be created.

 

Calling copy constructor: The copy constructor is called automatically when we define a class object using an existing instance of the object as a parameter.

class_name cn1;

class_name cn2 (cn1);

 

Default Copy constructor: If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy. 

 

3. Destructors: Destructors come into play when an object dies, either because it is no longer in scope or because it has been deleted.

Format: The name of the destructor is the name of the class preceded by tilde ( ~ ).

class_name :: ~class_name()

{

.........

}     //class_name destructor

 

Note: A class can have only one destructor. 

Default Destructor: If we do not provide a destructor for our class, the system provides one for us. A default destructor is very primitive and does nothing. It is provided because the C++ compiler is looking for a destructor when the object ceases to exist.