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
a. default
b. with name as argument (assume age is 18)
c. with name and age as argument
Also define the copy constructor.
#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
Learning from knowledgeable instructors can make a significant difference. Their ability to explain complex topics and provide real-world examples is invaluable.click here for more information.
ReplyDelete