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