Saturday, November 5, 2011

Class with constructor

// Use of constructor
# include<iostream>
#include<conio.h>

using namespace std;

class integer{
int m,n;
public:
integer(int,int);

void display( void){

cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};

integer :: integer(int x,int y){
m=x;n=y;
}

int main(){
integer int1(0,111);    //constructor called implicitly
integer int2=integer(28,75);    //Constructor called explicitly

cout<<endl<<"Object1:"<<endl;
int1.display();

cout<<endl<<"Object 2"<<endl;
int2.display();
getch();

return 0;
}

No comments:

Post a Comment