Introduction
to Constructors
The
definition of a class only creates a new user defined data type. The instances
of the class type should be instantiated (created and initialized) .
Instantiating object is done using constructor.
An
array or a structure in c++ can be initialized during the time of their
declaration.
struct sum
{
int n1,n2;
};
class add
{
int num1,num2;
};
int main()
{
int arr[]={1,2,3};
//declaration and initialization of array
sum s1={1,1};
//declaration and initialization of structure object
add a1={0,0}; //
class object declaration and initialization throws compilation error
}
Member
function of a class can accessall the members irrespective of their associated
access specifier.
The
initialization of class type object at the time of declaration similar to a
structure or an array is not possible because the class members have their
associated access specifiers (private or protected or public). Therefore
Classes include special member functions called as constructors. The constructor function initializes the class
object.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.