Mar 23, 2007

Constructor in C++

// constructor : is a member function of a class. it must have same name
// as a class. it does not have any return type neither int nor void.
// it automatically get executed when object is created. it is used to
// initialize variables.


#include<iostream.h>
#include<conio.h>
class demo
{
public :
int a,b;
demo() // constructor
{
a=10;
b=20;
}

void add()
{
int c=a+b;
cout << c <<endl;
}
};

main()
{
clrscr();
demo d1;

// here object is created d1 of demo class and constructor get called automatically no need to

//call it explicitly.
d1.add();
getch();
}

No comments:

Post a Comment

Infolinks In Text Ads

Total Pageviews

Powered by Blogger.

Dont Forget To Follow Us

Blog Archive