In this section we will study object oriented programming. When we have to use dozens of functions in one program, object oriented programming or OOP is very helpful. Object oriented programming has the greatest ability to solve complex problems. It also helps the user to code, debug, maintain and conceptualize the program easily.
Object oriented programming does not belong to a particular programming rather it can be used in C language. C++ also supports object oriented programming.
OOP consists of classes and objects.
A class is considered to be user defined data type. In object oriented programming, objects are the members of classes. Each class in C++ has a precise role and a set of actions.
In C++ programming we can create new data types as classes to represent objects. There is difference between a structure and a class that is in structure there is no member function, it is limited to just holding the data. But in classes we can also define member functions that are accessed by the objects.
Classes have two main components or members: data members and member functions. Data members may be simple data variables, arrays, etc. Member functions operate on the objects of the classes by using the data members. These member functions are also called methods.
When a class is defined it does not mean that you are defining any data but it defines what the name of class means, what its objects will consists of and what are the operations that can be performed on an object. Let us define a class of rectangle:
The class name is followed by the keyword class that indicates that a class has been declared. The data members are declared using data types. Here the data members are specified as private.
Private is an Access specifier which means that these data members cannot be accesses anywhere within the scope of class object. There are three types of access specifiers: private, protected, and public. The data members like member functions can also be specified as public which means that they can be accessed anywhere in the scope of class objects. This is the main difference between structures and classes.
Note that a class is also terminated with a semicolon after the closing braces.
A member function in a class is declared in the class definition. It can be operated by any object of the class in which it has been declared. Member functions can also access the data members of the class in which they are declared.
As we all know that the member functions are defined in a class. Now here arises a question that how the member functions are defined, this is demonstrated in the following example:
Suppose we have to define the member function void set() of the above created class:
Object is a variable whose data type is the name of the class. It has all information of class (data members, member functions). An object consists of data and the functions that are to be done on the data.
Objects are declared in the main() function followed by the name of the class. We have already defined a class rectangle, now we are declaring objects. Consider the following example:
Constructor is a function whose name is same as the name of the class. Constructor has no return type but we can pass arguments and parameters. Constructors are used to remove the garbage values. There are four types of constructors:
- Default constructor
- Parameterized constructor
- Default parameterized constructor
- Copy constructor
All constructors except copy constructor will be written as function in class definition. When using constructors there no need to input data from the user in output, rather the values are entered in the main() function.
When an object is created default constructor is automatically called. It has no parameters or arguments. The example of declaration of default constructor is as follows:
Default constructors are mostly used to initialize the values of data members to 0 so that the garbage values should be avoided.
Parameterized constructor has parameters or arguments. The parameters differ the parameterized constructor from the default constructor. This constructor is called by objects when certain values are passed through the objects. Example:
circle(int a, int b);
Calling of constructor:
circle c(2, 3);
c is an object and has two parameters 2 and 3 that are passed to int a, and int b. Therefore, we do not need to input values from the user. For printing these values we simple use this object with the print function as:
The values 2 and 3 are passed in the print function and will be printed.
Parameterized constructor is always used with default constructor.
Default parameterized constructor
It is the combination of both constructors. When this constructor is used there is no need to use the default and parameterized constructor separately. This constructor is used when we are using constant data members that are initialized through member initialization list that is:
Note that the data members are initialized to zero in the constructor declaration. in the member initialization list a1 is assigned the value of a and vice versa.
One object when copied to another, this is called copy constructor e.g.
Note that when copying, a new object is created; it means that to copy one object to other we have to create a new object.
Destructor has also the same name that of class. By the end of the program destructor is automatically called and destroys all objects in descending order.
following example shows declaration of destructor.
Calling of destructor:
Static data members
These data members are independent variables and hold all information of class. When we are using static data members, there is no need to use multiple objects. These variables are sometime used to count how many objects are created in a program. they are declared inside the class and defined outside the class.
The declaration is as follows:
static int count;