In C++ using classes, take the code below making a class -- called Asteroid. and
ID: 3824833 • Letter: I
Question
In C++ using classes, take the code below making a class -- called Asteroid. and add the objects.
It will have a size and speed.
Create 2 variables of type asteroid and set the size and speed.
The setsize function should check if the size is below 1 or above 20. If it is below 1, make it 1. If it is above 20, make it 20. Otherwise, keep the size that was sent in.
#include <iostream>
using namespace std;
class Asteroid {
private:
int size , speed;
public:
Asteroid() {
}
void setSize(int s){
if(s < 1){
size = 1;
}
else if(size > 20){
size = 20;
}
else{
size = s;
}
}
};
int main()
{
}
Explanation / Answer
Hi, I have added required code.
Please let me know in case of any issue.
#include <iostream>
using namespace std;
class Asteroid {
private:
int size , speed;
public:
Asteroid() {
}
void setSize(int s){
if(s < 1){
size = 1;
}
else if(size > 20){
size = 20;
}
else{
size = s;
}
}
void setSpeed(int s){
speed = s;
}
};
int main()
{
// creating two objects of Asteroid
Asteroid a1;
a1.setSpeed(45);
a1.setSize(34);
Asteroid a2;
a2.setSpeed(34);
a2.setSize(5);
return 0;
}