Professional Documents
Culture Documents
Parameterised Constructor
Parameterised Constructor
CONSTRUCTOR
BY
THANAY.AGILAN,MOHINI
DEFINITION
In C++,Constructor is automatically
called when object(instance of class)
is created. It is special member
function of the class. Which
constructor has arguments thats
calledParameterized Constructor.
RULES TO BE FOLLOWED
SYNTAX
class class-name
{
Access Specifier:
Member-Variables
Member-Functions
public:
class-name(variables)
{
// Constructor code
}
... other Variables & Functions
};
SAMPLE PROGRAM
#include<iostream>
#include<conio.h>
int main() {
Example Object(10,20);
// Constructor invoked.
Object.Display();
OUTPUT
Im Constructor
Values :10 20
AREA OF RECTANGLE
#include<iostream.h>
#include<conio.h>
class area
{
int length,breadth;
public:
area(int l,int b)
{
length=l;
breadth=b;
cout<<Length of rectangle is:<<length<<endl;
cout<<Breadth of rectangle is: <<breadth<<endl;
}
void display()
{
cout<<Area of rectangle is: <<length*breadth;
}
};
void main()
{
clrscr();
area a1(8,7);
a1.display();
getch();
}