Posts
Definition and example of parameterized constructor.
- Get link
- X
- Other Apps
PARAMETERIZED CONSTRUCTOR: - These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument. The constructor with parameters can be used to initialize data members of the object. To create a constructor with parameters you have to specify its parameters in the parentheses as we do to specify parameters of a function. e.g. #include<iostream.h> #include<conio.h> class number { int itemcode,cost; public: number(int x,int y) { itemcode=x; cost=y; } void display() { cout<<"\n code= "<<itemcode; cout<<"\n cost= "<<cost; } }; void main() { clrscr(); number n1(192,356); cout<<"values for first object="; n1.display(); number n2(546,657); cout<<"\n value for second object="; n2.display(); getch(); }
WHAT IS CONSTRUCTOR ?
- Get link
- X
- Other Apps
A Constructor is a special member function of class because its name is same as the class name.it is called constructor because it construct the values of the data member of the class and it cannot return any values. if a class has constructor each object of that type is initilized with the constructor to use in a program.constructor are called at the point where an object of the class is created.