Object Oriented Programming Unit-3b Basic Theory

  • Objects as Function Arguments
  • Friend Function
  • Constructor
  • Parameterized Constructors
  • Multiple Construction in a Class
  • Constructors with Default Arguments
  • Destructors

  1. An object may be used as a function argument.
  2. This can be done in two ways:
  3. A copy of the entire object is passed to the function, method is known as pass by value.
  4. Only the address of the object is transferred to the function, method is known as pass by reference.
  5. When an address of the object is passed, the called function works directly on the actual object used in the call. This means that any changes made to the object inside the function will reflect in the actual object.
  6. The pass-by-reference method is more efficient since it requires to pass only the address of the object and not the entire object.
  7. Write an object oriented programming to addition of two Time (object) in the hour and minutes format.

Keyword “friend”.
When the function is declared as a friend,
then it can access the private and protected data members of the class.
A friend function is declared inside the class
with a friend keyword preceding as shown below

  • It is not in the scope of the class to which it has boon declared as friend.
  • Since it is not in the scope of the class, it cannot be called using the object of that class.
  • It can be invoked like a normal function without the help of any object.
  • Unlike member functions, it cannot access the member names directly and has to use an object n.ame and dot membership operator with each member name.(e.g. A.x).
  • It can be declared either in the public or the private part of a class without affecting its meaning.
  • Usually, it has the objects as arguments.

Write an object oriented programming to swap age:
Rama and Ranu.





A function cannot only receive object as arguments but also can return them.

The example in below Program illustrates how an object can be created (within a function) and returned to another function.
Write a object oriented programming to add two object’s data member.



The statement x.getdata(l00.299.95);
passes the initial values as arguments to the function getdata(), where these values are assigned to the private variables of object x.

All those 'function call' statements are used with the appropriate objects that have already been created.

These functions cannot be used to initialize the member variable at the time of creation of their objects.

However, for example,
int m = 10;
float x = 5.75;

are valid initialization statements for basic data types.

Similarly, when a variable of built--in type goes out of scope, then compiler automatically destroys the variable.

But it has not happened with the objects.

It is therefore clear that there is need of features of classes to be explored that would enable us to initialize the objects when they are created and destroy them when their presence is no longer necessary.

C++ provides a special member function called the constructor which enables an object to initialize itself when it is created.

This is known as automatic initialization of objects.

It also provides another member function called the destructor that destroys the objects when they are no longer required.

When a class contains n constructor like the one defined above, it is guaranteed that an object created by the class will be initialized automatically.

For example, the declaration
integer ObjInt; // object ObjInt created

A constructor is different from normal functions in following ways:
  • Constructor has same name as the class itself
  • Constructors don’t have return type
  • A constructor is automatically called when an object is created.
  • If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body)

Default constructor is the constructor which doesn’t take any argument.

It has no parameters.

Parameterized Constructor is possible to pass arguments to constructors.

Typically, these arguments help initialize an object when it is created.

To create a parameterized constructor, simply add parameters to it the way you would to any other function.

When you define the constructor’s body, use the parameters to initialize the object.

A copy constructor is a member function which initializes an object using another object of the same class.

Detailed article on Copy Constructor
Whenever we define one or more non-default constructors (with parameters) for a class, a default constructor (without parameters) should also be explicitly defined as the compiler will not provide a default constructor in this case.

However, it is not necessary but it’s considered to be the best practice to always define a default constructor.

A destructor works opposite to constructor; it destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically
A destructor is defined like constructor. It must have same name as class. But it is prefixed with a tilde sign (~).