Saturday, November 19, 2011

C and C++ Examples ( CONSTRUCTOR )


/*PROGRAM TO IMPLEMENT CONSTRUCTOR*/

#include<iostream.h>
#include<string.h>
class person
{
private:
char name[20];
int age;
double height;
public:
person(char *string,int a,double h);
void get_person_data(void);
void set_person_data(char *string,int a,double h);
void show_person_data(void);
};
person::person(char *string,int a,double h)
{
strcpy(name,string);
age=a;
height=h;
}
void person::get_person_data(void)
{
cout<<"enter the name of the person:";
cin.get(name,20);
cout<<"enter the age of the person:";
cin>>age;
cout<<"enter the height of the person:";
cin>>height;
}

void person::set_person_data(char *string,int a,double h)
{
strcpy(name,string);
age=a;
height=h;
}
void person::show_person_data(void)
{
cout<<"name of the person:"<<name<<"\n";
cout<<"age of the person:"<<age<<"years\n";
cout<<"height of the person:"<<height<<"cms\n";
}
void main(void)
{
person p1=person("bill",40,170.5);
person p2("steave",35,169.5);
cout<<"data about the first person:";
p1.show_person_data();
cout<<"\n";
cout<<"data about the 2nd person:";
p2.show_person_data();
}




















OUTPUT:

 data about the first person:name of the person:Raju
age of the person: 40years
height of the person: 161.5cms

data about the 2nd person:name of the person: venki
age of the person: 35years
height of the person: 159.5cms

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju