Pages

100 Contemporary Living Room Designs

Monday, September 10, 2012

C++ PROGRAM TO DEMONSTRATE DEFAULT AND PARAMETERISED CONSTRUCTORS.

Define the class Person which has name (char*) and age (int). Define the following constructors
a. default
b. with name as argument (assume age is 18)
c. with name and age as argument
Also define the copy constructor.



#include<iostream.h>
#include<string.h>
#include<conio.h>
class person
{
  char *name;
  int age;
  public:
  person()
  {
    name=NULL;
    //strcpy(name,NULL);
    age=0;
  }
  person(char *n)
  {
    strcpy(name,n);
    age=18;
  }
  person(char *n1,int a)
  {
    strcpy(name,n1);
    age=a;
  }
  person(person &p)
  {
    strcpy(name,p.name);
    age=p.age;
  }
  void disp()
  {
    cout<<"name is "<<name<<endl;
    cout<<"age is "<<age<<endl;
  }
};
void main()
{
  clrscr();
  person p1("ram"),p2("sita",20),p3=p2;
  cout<<"hello\n";
  p1.disp();
  p2.disp();
  p3.disp();
  getch();
}


OUTPUT


No comments:

Post a Comment