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.
#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;
}
Comments
Leave a comment