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>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(const string& name="Mike", int age=30);
~Person();
void display() const;
};
Person::Person(const string& name, int age) {
this->name = name;
this->age = age;
}
Person::~Person() {}
void Person::display() const {
cout << "Name: " << this->name << ", Age: " << this->age << endl;
}
int main() {
Person one;
Person two("Peter");
Person three("John", 27);
one.display();
two.display();
three.display();
return 0;
}
Comments
Leave a comment