Answer to Question #181808 in C++ for Kaushal Anand

Question #181808

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-15T16:34:44-0400
#include <iostream>
#include <string>
using namespace std;


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


Person::Person(string name = "John", int age = 1)
{
	this->name = name;
	this->age = age;
}


void Person::View()
{
	cout << "Name is: " << name << ". And age is: " << age << " years old." << endl;
}


int main()
{
	Person person1;
	Person person2("Lila");
	Person person3("Moana", 14);
	person1.View();
	person2.View();
	person3.View();
	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