Write a program that has a above mentioned attributes and also have an overloaded constructor that takes arguments like(Name,Age,Gender,nationality) from the user ,assign the parameers to attributes of class and display on console.
#include <iostream>
using namespace std;
class Human {
string name;
int age;
string gender;
string nationality;
Human(string name, int age, string gender, string nationality) {
this->name = name;
this->age = age;
this->gender = gender;
this->nationality = nationality;
}
void display() {
cout << "name: " << this->name << "\n";
cout << "age: " << this->age << "\n";
cout << "gender: " << this->gender << "\n";
cout << "nationality: " << this->nationality << "\n";
}
};
int main()
{
string name, gender, nationality;
int age;
cout << "Enter human data: ";
cin >> name;
cin >> age;
cin >> gender;
cin >> nationality;
Human h = new Human(name, age, gender, nationality);
h->display();
return 0;
}
Comments
Leave a comment