Read OnLine Cpp Program Example



10 - CONSTRUCTURE
PRAC 3 PRAC 4

// constructure example
#include<iostream.h>
#include<conio.h>
class demo
{
public :
demo()
{
cout<<"Hello...";
}
demo (int a,int b)
{
int ans;
ans=a+b;
cout<<"\nAdd="<<ans;
}
demo (int a,int b,int c)
{
int ans;
ans=a+b+c;
cout<<"\nAdd="<<ans;
}
};
void main()
{
clrscr();
demo d1;
demo d2(2,4);
demo d3(4,5,6);
getch();
}


----OUTPUT----
Hello...
Add= 6
Add= 15

// constructure example
#include<iostream.h>
#include<conio.h>
class cRectangle
{
int *width, *height;
public :
cRectangle (int,int);
~cRectangle();
int area()
{
return(*width * *height);
}
};
cRectangle::cRectangle (int a,int b)
{
width=new int;
height=new int;
*width=a;
*height=b;
}
cRectangle::~cRectangle()
{
delete width;
delete height;
}
void main()
{
clrscr();
cRectangle rect (3,4),rectb (5,6);
cout<<"rect area:"<<rect.area()<<endl;
cout<<"rectb area:"<<rectb.area()<<endl;
getch();
}


----OUTPUT----
rect area:12
rectb area:30

Previous Next
Download Download