Answer to Question #180917 in C++ for Shashank Shekhar Singh

Question #180917

Write a program that demonstrate the use of constructor with default arguments for the following problem. Define a class Person with data member as name and age. Create three objects with no argument, one argument name and two argument name and age. You are not allowed to create more than one constructor.


1
Expert's answer
2021-04-13T09:53:44-0400
#include <iostream>
#include <string>


using namespace std;


class Person
{
public:
	Person(string name, int age);
	~Person() {}
	void Display();
private:
	int age;
	string name;
};


Person::Person(string name_ = "NoName", int age_ = 18) : name(name_), age(age_)
{}
void Person::Display()
{
	cout << name << " " << age << endl;
}




int main()
{
	Person Bill;
	Person Mike("Mike");
	Person Will("Will", 25);
	Bill.Display();
	Mike.Display();
	Will.Display();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS