Answer to Question #180719 in C++ for Venkat

Question #180719

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-12T13:21:10-0400
#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;
}

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