Answer to Question #198860 in C++ for Ali raza

Question #198860

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.


1
Expert's answer
2021-05-26T06:56:21-0400
#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;

}

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