Using the rps.cpp file, implement a class called Tool. It should have an integer
ID: 3570887 • Letter: U
Question
Using the rps.cpp file, implement a class called Tool.
It should have an integer field called strength and a character field called type.
You may make them either private or protected.
The Tool class should also contain the function void setStrength (int), assigning the strength
Create 3 more classes called Rock, Paper, and Scissors, which are derived from Tool.
Each of these classes will need a constructor which will take in an integer that is used to
initialize the strength field.
The constructor should also initialize the type field using 'r' for Rock, 'p' for Paper, and
Explanation / Answer
Program Code:
#include <iostream>
using namespace std;
//Tool class
class Tool
{
public:
//declaration of variables
int strength;
char type;
//constructor of Tool class
Tool(char c)
{
type=c;
}
//setStrength method that initialises the strength
void setStrength(int s)
{
strength=s;
cout<<"Strength of "<<type<<" is "<<strength;
cout<<endl;
}
};
//Scissors class extends Tool class
class Scissors: public Tool
{
typedef Tool super;
public:
int stren;
Scissors(int s):Tool('s')
{
super::setStrength(s);
stren=s;
}
//fight method that returns bool value
bool fight(Tool t)
{
bool b=false;
if(t.type=='p')
{
b=true;
super::setStrength(stren*2);
}
else
{
b=false;
super::setStrength(stren/2);
}
return b;
}
};
//Paper class extends Tool class
class Paper: public Tool
{
typedef Tool super;
public:
int stren;
Paper(int s):Tool('p')
{
super::setStrength(s);
stren=s;
}
//fight method that returns bool value
bool fight(Tool t)
{
bool b=false;
if(t.type=='r')
{
b=true;
super::setStrength(stren*2);
}
else
{
b=false;
super::setStrength(stren/2);
}
return b;
}
};
//Rock class extends Tool class
class Rock: public Tool
{
typedef Tool super;
public:
int stren;
Rock(int s):Tool('r')
{
super::setStrength(s);
stren=s;
}
//fight method that returns bool value
bool fight(Tool t)
{
bool b=false;
if(t.type=='s')
{
b=true;
super::setStrength(stren*2);
}
else
{
b=false;
super::setStrength(stren/2);
}
return b;
}
};
//main function
int main()
{
Scissors s1(5); // create Scissor s1 and assign strength 5
Paper p1(7); // create Paper object p1 and assign strength 7
Rock r1(15); // create Rock object r1 and assign strength 15
cout << "Scissor vs. Paper … Paper vs. Scissors" << endl;
cout << s1.fight(p1)<< p1.fight(s1) << endl; // scissor vs. paper
cout <<"Paper vs. Rock … Rock vs. Paper" << endl;
cout << p1.fight(r1) << r1.fight(p1) << endl; // paper vs. rock
cout << "Rock vs. Scissors … Scissors vs. Rock" << endl;
cout << r1.fight(s1) << s1.fight(r1) << endl; // rock vs. scissor
cout<<" ---------------------------------------------------";
cout<<" Second game: "<<endl;
Scissors s2(20);
Paper p2(50);
Rock r2(10);
cout << "Scissor vs. Paper … Paper vs. Scissors" << endl;
cout << s2.fight(p2)<< p2.fight(s2) << endl; // scissor vs. paper
cout <<"Paper vs. Rock … Rock vs. Paper" << endl;
cout << p2.fight(r2) << r2.fight(p2) << endl; // paper vs. rock
cout << "Rock vs. Scissors … Scissors vs. Rock" << endl;
cout << r2.fight(s2) << s2.fight(r2) << endl; // rock vs. scissor
// add more testing code here
return 0;
}
----------------------------------------------------------------------------------------------
Sample output:
Strength of s is 5
Strength of p is 7
Strength of r is 15
Scissor vs. Paper … Paper vs. Scissors
Strength of p is 3
Strength of s is 10
10
Paper vs. Rock … Rock vs. Paper
Strength of r is 7
Strength of p is 14
10
Rock vs. Scissors … Scissors vs. Rock
Strength of s is 2
Strength of r is 30
10
---------------------------------------
Second game:
Strength of s is 20
Strength of p is 50
Strength of r is 10
Scissor vs. Paper … Paper vs. Scissors
Strength of p is 25
Strength of s is 40
10
Paper vs. Rock … Rock vs. Paper
Strength of r is 5
Strength of p is 100
10
Rock vs. Scissors … Scissors vs. Rock
Strength of s is 10
Strength of r is 20
10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.