Define a class named StarterPokemon that contains
Question
Define a class named StarterPokemon that contains
1 Four private members name ( a string), attack( a string), level ( an integer)
2 the corresponding assessors and mutators for the above member variables in the public section.
⦁ a String getName() – return the pokemon name
⦁ a String getType() – return pokemon type
⦁ a String getAttack()- return the pokemon attack
⦁ int getLevel() – return the level of the pokemon
⦁ void setName(string) – set the name of the pokemon via string parameter
⦁ void setType(string) – set the pokemon type
⦁ void setAttack(string) – set the attack
⦁ void setLevel(int) – set the level of the pokemon
3 two constructors
⦁ the default constructor that sets the name and type as ‘unknown’ the level as 5 and the attack of the pokemon as ‘Tackle’
⦁ so an overloaded constructor that takes four parameter as input in the order of (string inName, string inType, string inAttack, int inLevel and initialize the corresponding member variables of the pokemon object.
4 void printout() function that outputs the information of the pokemon in the following format
Name:<name, Type<type>,Attack:<attack>, Level:<level>
in the main() function
⦁ So declare a starterPokemon object and set its name to ‘Torchic’, its type to ‘Fire’, attack to ‘Ember’ and its level to ’14’. So Then output the pokemon information as:
⦁ Name: Torchic
⦁ Type: Fire
⦁ Attack: Ember
⦁ Level: 14
Note:
please do not hard code the above output instead ,you should call the assessors to get the information for output.
⦁ So declare an array of starterPokemon objects named poke(size = 3) where indicated to make the remaining code work. Uncomment code for testing
The man is commented in the initial template to allow you to test the class first(Test cases 4-8). So after passing these test cases uncomment and complete the main uncommenting the main while your class is still incomplete will lead to compiler errors.
Hint: So the attacks may contain one or more whitespaces thus you should use getLine() for their input. Also remember to call cin.ignore() before the getLine() function if you use a cin statement before the getLine(0 to ensure the correct input.
Summary
Here one class is created whose name is StarterPokemon whose data members are name, type, attack and level. Then there are assessors from which we can access them outside the when needed. Such that after this there are two constructors in which one is default constructor and second is parameterize constructor. So after that one function is used to show the correct order of all the elements. That function is known as printout. And finally in the main method there is all the declaration and the output statements.
Explanation
In this program one class is implement whose name is starterPokemon. Where we declare the data member as name, type, attack and level, where we assigned a string type to the first three and int to the last one. Then we use a assessors for the type, attack level and name. After that we set the mutators with the return type void. So after this we add one default and one parameterize constructor. And in the main method we add the all output statements and add for loop to print that at the specified number.
Code
#include <iostream> using namespace std; /* to implement the StarterPokemon class */ class StarterPokemon { /* data members */ string name, type, attack; int level; public: /* accessor for name */ string getName(){ return name;} /* accessor for type */ string getType(){ return type;} /* accessor for attack */ string getAttack(){ return attack;} /* accessor for level */ int getLevel(){ return level;} /* mutator for name */ void setName(string n){ name=n;} /* mutator for type */ void setType(string t){ type=t;} /* mutator for attack */ void setAttack(string a){ attack=a;} /* mutator for level */ void setLevel(int l){ level=l;} /* default constructor */ StarterPokemon(){ name="Unknown"; type="Unknown"; level=5; attack="Tackle";} /* parameterized constructor */ StarterPokemon(string n, string t, string a, int l){ name=n; type=t; level=l; attack=a;} /* to show all the details */ void printOut(){ cout<<"Name: <"<<name<<">, Type: <"<<type<<">, Attack: <"<<attack; cout<<">, Level: <"<<level<<">\n";} }; /* main code */ int main() { StarterPokemon s; s.setName("Torchic"); s.setType("Fire"); s.setAttack("Ember"); s.setLevel(14); cout<<"Name: "<<s.getName()<<endl; cout<<"Type: "<<s.getType()<<endl; cout<<"Attack: "<<s.getAttack()<<endl; cout<<"Level: "<<s.getLevel()<<endl<<endl; StarterPokemon s1[3]; for(int i=0;i<3;i++) { string n,t,a; int l; cin.ignore(); cout<<"Enter name: "; getline(cin,n); cout<<"Enter type: "; getline(cin,t); cout<<"Enter attack: "; getline(cin,a); cout<<"Enter level: "; cin>>l; s1[i].setName(n); s1[i].setType(t); s1[i].setAttack(a); s1[i].setLevel(l); } cout<<endl<<endl; for(int i=0;i<3;i++) s1[i].printOut(); }
Output