Answer to Question #180965 in C++ for Dharmendra Tripathi

Question #180965

Write a program that demonstrate the use of constructor with default

arguments the for following problem. Define a class Person with data member as nameand age. Create three argument, no with objects one argument two and name argument

age. and name are not allowed to create more than one constructor. You


1
Expert's answer
2021-04-13T09:19:17-0400
#include <iostream>
#include <vector>


using namespace std;


class Person
{
    string _name;
    int _age;


public:


    Person(string name, int age): _name(name), _age(age){}


    void SetName(string name)
    {
        _name = name;
    }


    void SetAge(int age)
    {
        _age = age;
    }


    string GetName()const
    {
        return _name;
    }


    int GetAge()const
    {
        return _age;
    }
};


int main()
{
    string nameBuf;
    int ageBuf;
    vector<Person> persons;
    int i;
    for(i = 0; i < 3; i++)
    {
        cout << "Enter name of person: ";
        nameBuf = "";
        ageBuf = 0;
        cin >> nameBuf;
        cout << "Enter age of " << nameBuf << ": ";
        cin >> ageBuf;
        persons.push_back(Person(nameBuf, ageBuf));


    }
    for(i = 0; i < 3; i++)
    {
        cout << "Person #" << i + 1 << ": name = " << persons[i].GetName() << ", age = " << persons[i].GetAge() << endl;
    }
    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