ikReza

OOP constructor overloading

Feb 13th, 2020
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. class Pokemon
  7. {
  8. private:
  9.     string name, type;
  10.     int hp;
  11. public:
  12.     Pokemon();
  13.     Pokemon(string name);
  14.     Pokemon(string name, string type);
  15.     Pokemon(string name, int hp);
  16.     Pokemon(string name, string type, int hp);
  17.     string toString();
  18. };
  19.  
  20. Pokemon::Pokemon()
  21. {
  22.     name = "Pikachu";
  23.     type = "Electric";
  24.     hp = 60;
  25. }
  26.  
  27. Pokemon::Pokemon(string name)
  28. {
  29.     this->name = name;
  30.     type = "Water";
  31.     hp = 60;
  32. }
  33.  
  34. Pokemon::Pokemon(string name, string type)
  35. {
  36.     this->name = name;
  37.     this->type = type;
  38.     hp = 60;
  39. }
  40.  
  41. Pokemon::Pokemon(string name, int hp)
  42. {
  43.     this->name = name;
  44.     type = "Grass";
  45.     this->hp = hp;
  46. }
  47.  
  48. Pokemon::Pokemon(string name, string type, int hp)
  49. {
  50.     this->name = name;
  51.     this->type = type;
  52.     this->hp = hp;
  53. }
  54.  
  55. string Pokemon::toString()
  56. {
  57.     stringstream ss;
  58.     ss << "Name: " << name << " -->   Type: " << type << " -->   HP: " << hp;
  59.     return ss.str();
  60. }
  61.  
  62. int main()
  63. {
  64.     Pokemon myPokemon1;
  65.     Pokemon myPokemon2("Buizel");
  66.     Pokemon myPokemon3("Treecko", "Grass");
  67.     Pokemon myPokemon4("Serperior", 120);
  68.     Pokemon myPokemon5("Dragonite", "Dragon", 150);
  69.     Pokemon myPokemon6("Latios", "Dragon", 130);
  70.  
  71.     cout << myPokemon1.toString() << endl;
  72.     cout << myPokemon2.toString() << endl;
  73.     cout << myPokemon3.toString() << endl;
  74.     cout << myPokemon4.toString() << endl;
  75.     cout << myPokemon5.toString() << endl;
  76.     cout << myPokemon6.toString() << endl;
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment