Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We created in class a Person class to represent a person. The class has instance

ID: 655691 • Letter: W

Question


We created in class a Person class to represent a person. The class has instance variables for a person's name, which is a string, and an integer age. These variables are names and age, respectively. Write a default constructor for Person that sets name to the string "No name yet" and age to zero. Write a second constructor for Person that sets name to a given string and age to a given age. Write a static method createAdult() for Person that returns a special instance of this class. The instance represents a generic adult and has the name "An adult" and the age 21.

Explanation / Answer

#include <iostream.h>
#include <string.h>
#include <conio.h>


class Person
{
char name[50];
int age;
public:

Person()
{
strcpy(name,"no name yet");
age=0;
}

Person(char *s, int a)
{
strcpy(name,s);
age=a;
}

void display()
{
cout<<name<<" "<<age;
}

static Person& createAdult(char S[], int a , Person &P)
{
strcpy(P.name,S);
P.age=a;
return P;
}

}


void main()
{
int a;
char str[50];

Person p1=new Person();
cout<<"Person 1 Details:";
p1.display();

cout<<"Enter the name of the Generic Adult:";
cin>>str;
cout<<endl<<"Enter the age: ";
cin>>a;

Person p2=new Person(str,a);

p2.display();
Person p4=new Person();
Person p3=new Person("An Adult",21,p4);
Person::createAdult();
cout<<"Generic Adult Details:";
p3.display();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote