What is a constructor ? Why it is called so?
“A constructor is a special member function of a class . Its task is to initialize the objects of its class. ”
It is special because its name is same as that of the class to which it belongs.
The constructor is invoked whenever an object of its associated class is created.
It is called constructor because it constructs the values of data members of the class.
A constructor can never return any value. Hence ,it is written with no return type ( even void is not written ).
Whenever a class contain a constructor like one above , it will be initialized automatically , whenever an object of the class is created.
i.e. the declaration – integer int1;
This not only creates the object int1 of type integer , also initializes its data members m and n to zero .
Give the characteristics of a constructor function .or Syntax rule for writing constructors.
The constructor name is always same as the class name .
They do not have return types , not even void and therefore, they cannot return values.
They cannot be static or virtual .
They should be declared in public section .
They cannot be inherited though a derived class can call base class constructor .
Like other c++ function , they can have default arguments .
We cannot refer to their address .
An object with a constructor cannot be used as a member of union .
They make implicit calls to the operator ‘new’ and ‘delete’ when memory allocation is required.
When a constructor is declared for a class , initialization of class objects become mandatory , since constructor is invoked automatically when the objects are created.
What is inheritance ? Explain with suitable example .
The mechanism of deriving a new class from an old called is inheritance .
The old class is referred as base class and new class is reffered as derived class.
C++ strongly supports the concept of reusability . Once a class has been written and tested , if can be adapted by other programmers to suit their requirement .
This is basically done by creating new classes , reusing the properties of the existing ones
Functions and variables of a class that has been tested can be used by object of another class . This is known as inheritance
. The reuse of a class that has alreadynbeen tested , debugged and used many times , can save the efforts of developing and testing the same again .
Explain the concept of virtual function .
When user use the same function name in both the base and derived classes, the function in the base class is derived as virtual using the keyboard ‘virtual’ preceding its normal declaration .
When a function is made virtual , c++ determines which function to use at runtime , based on the type of object pointed to by the base pointer .
Thus , by making the base pointer to point two different objects it can execute different versions of the virtual function .
Virtual function can be accessed through the use of pointer declared as a pointer to the base class .
Also, the prototypes of the base class version of virtual function and all the derived class version must be identical .
If two functions with the same name have different prototype ,c++ considers them as overload functions , and the virtual mechanism is ingnore
keep study with us.
thank you
ReplyDelete👍👍
ReplyDelete👍👍
ReplyDeletethank you
ReplyDelete