Question

Godzilla Class Pt. 2 C++

I have to continue building off of this class and do the following

Expanded Godzilla Class We are now going to properly structure our Class members correctly. Begin by denoting your data membeEven More Expanded Godzilla Class Its now time for Godzilla and Mechagodzilla to go to battle. Add an attack function as des

The Final Godzilla Class Now add the following function to your class: A greet function that accepts another Godzilla object

I got help with the code below but could use some guidance as to how to finish this out. Any help would be appreciated

This is the code for the main.cpp

#include <iostream>
#include "Godzilla.h"
using namespace std;

int main() {

    double health;
    double power;

    //prompt the user to input the health and power for Godzilla
    cout<< "Enter the health: " ;
    cin >> health;
    cout <<"Enter the power: " ;
    cin >> power;

    //calls the class
    Godzilla Godzilla, Mechagodzilla("Mechagodzila" , health, power);

    //outputs the health and power for the OG Zilla and Mechagodzilla
    cout << "Godzilaa: Health = " << Godzilla.health << ", Power = " << Godzilla.power << endl;
    cout << "Mechagodzilla: Health =" << Mechagodzilla.health << ", Power = " << Mechagodzilla.power << endl;

    return 0;
}

This is what is in the Godzilla.h file

#include "Godzilla.h"

#include<string>
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

class Godzilla {

public:
    //the variables
    string name;
    double health;
    double power;

    //default constructor

    Godzilla();

    //parameterized constructor

    Godzilla(string name, double health, double power);


};


#endif //SET8_GODZILLA_H

And this is the code for Godzilla.cpp

#

include "Godzilla.h"

#include<string>
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

Godzilla::Godzilla() {
    srand(time(0));
    name = "Godzilla";
    health = (rand() % 100) + 50;
    power = (rand() % 25) + 10;
}
Godzilla::Godzilla(string name, double health, double power) {
    if (health <= 0 ) {
        health = rand() % 100 + 50;

    }
    if(power <= 0){
        power = rand() % 25 + 10;
    }
}

Expanded Godzilla Class We are now going to properly structure our Class members correctly. Begin by denoting your data members as private and the methods as public. Add the following functions below to your class you should include in your class: Appropriate getter and setter functions. The power setter should ensure that the power is greater than zero. If not, then reassign it to the random default value. Use a private helper function wherever one makes sense to refactor your constructors and setters. Now in main, have the Gozilla object (not the Mechagodzilla object) call the setters based on the user's input. When you output the data members' values to verify, use the corresponding getters.
Even More Expanded Godzilla Class It's now time for Godzilla and Mechagodzilla to go to battle. Add an attack function as described below: An attack function that accepts another Godzilla object as a parameter. The function should modify the target's health by subtracting the callee's power. It should also print out a message in the form of "Callee's Name attacks Target's Name." If the target's health falls below zero, print out a second message stating "Target's Name has been vanquished." Now in main, have Godzilla attack Mechagodzilla once. Then, have Mechagodzilla attack Godzilla until Godzilla has been vanquished. Your program should end at that point
The Final Godzilla Class Now add the following function to your class: A greet function that accepts another Godzilla object as a parameter which is passed by constant . reference. That is, the parameter should be marked constant so that the function cannot inadvertantly change the target's values. The function should print out a message in the form of "Callee's Name bows to Target's Name." Finally, in main before the two Godzillas do battle, have Godzilla greet Mechagodzilla. As in, the code would look like godzilla.greet( mechaGodzilla ); In order for this to work, you will need to make sure that you have marked ALL of your functions as const or not appropriately. Which functions should be constant functions? Which parameters should be constant parameters? Do this for every function - denote it as a const function when it is not modifying and of the callee's data members. Likewise, mark any parameters in every function as const that are not modified within the function body.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Godzilla.h file

#include<string>
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

class Godzilla {

public:
//the variables
string name;
double health;
double power;

//default constructor

Godzilla();

//parameterized constructor

Godzilla(string name, double health, double power);
//setter functions
void setName(string str);
void setHealth(double hth);
void setPower(double power);
//getter functions
string getname() const;
double getHealth() const;
double getPower() const;
void greet(Godzilla &obj);
int attack(Godzilla &obj);
};

======================

//Godzilla.cpp file

#include "Godzilla.h"

Godzilla::Godzilla() {
srand(time(0));
name = "Godzilla";
health = (rand() % 100) + 50;
power = (rand() % 25) + 10;
}
Godzilla::Godzilla(string name, double health, double power) {
this->name=name;
if (health <= 0 ) {
health = rand() % 100 + 50;

}
if(power <= 0){
power = rand() % 25 + 10;
}
}
//setter functions
void Godzilla::setName(string str)
{
name=str;
}
void Godzilla::setHealth(double hth)
{
health=hth;
}
void Godzilla::setPower(double pwr)
{
power=pwr;
}
//getter functions
string Godzilla::getname() const
{
return name;
}
double Godzilla::getHealth() const
{
return health;
}
double Godzilla::getPower() const
{
return power;
}
void Godzilla::greet(Godzilla &obj)
{
cout<<name<<" bows to "<<obj.getname()<<endl;
}
int Godzilla::attack(Godzilla &obj)
{
cout<<name<<" attacks "<<obj.getname()<<endl;
double Targethealth=obj.getHealth();
Targethealth-=power;
if(Targethealth < 0)
{
cout<<obj.getname()<<" has been vanquished"<<endl;

//when -1 is sent then health of target decreased below 0
return -1;
}
obj.setHealth(Targethealth);
return 0;
}

=========================

//Main.cpp file

#include "Godzilla.h"


int main()
{

double health;
double power;

//prompt the user to input the health and power for Godzilla
cout<< "Enter the health: " ;
cin >> health;
cout <<"Enter the power: " ;
cin >> power;

//calls the class
Godzilla Godzilla, Mechagodzilla("Mechagodzila" , health, power);

//outputs the health and power for the OG Zilla and Mechagodzilla
cout << "Godzilaa: Health = " << Godzilla.health << ", Power = " << Godzilla.power << endl;
cout << "Mechagodzilla: Health =" << Mechagodzilla.health << ", Power = " << Mechagodzilla.power << endl;
  
//use setters
Godzilla.setHealth(80);
Godzilla.setPower(15);
Mechagodzilla.setHealth(60);
Mechagodzilla.setPower(17);
//call getter function and display new values
cout << "Godzilaa: Health = " << Godzilla.getHealth() << ", Power = " << Godzilla.getPower() << endl;
cout << "Mechagodzilla: Health =" << Mechagodzilla.getHealth() << ", Power = " << Mechagodzilla.getPower() << endl;
//greet Mechagodzilla
Godzilla.greet(Mechagodzilla);

//till we get return value less than 0 ,Godzilla attacks Mechagodzilla,,0 means health of Mechagodzilla is still greater than 0
while(Godzilla.attack(Mechagodzilla)==0);
  
return 0;
}

=========================================

//Output

Enter the health: Enter the power: Godzilaa: Health = 112, Power = 34
Mechagodzilla: Health =2.34063e-310, Power = 2.34063e-310
Godzilaa: Health = 80, Power = 15
Mechagodzilla: Health =60, Power = 17
Godzilla bows to Mechagodzila
Godzilla attacks Mechagodzila
Godzilla attacks Mechagodzila
Godzilla attacks Mechagodzila
Godzilla attacks Mechagodzila
Godzilla attacks Mechagodzila
Mechagodzila has been vanquished

Add a comment
Know the answer?
Add Answer to:
Godzilla Class Pt. 2 C++ I have to continue building off of this class and do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do this in C++. Thank you in advance. Note that you have to carefully create...

    Please do this in C++. Thank you in advance. Note that you have to carefully create your class using the names and capitalization shown below. Put comments into your program, make sure you indent your program properly, and use good naming conventions. Create a class called entry. It should have two private data members of type std::string. One represents a name and the other represents an email address. Make sure you give them ,meaningful names. Create public getters and setters...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember...

    C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember class should have a constructor with no parameters, a constructor with a parameter, a mutator function and an accessor function. Also, include a function to display the name. Define a class called Athlete which is derived from FitnessMember. An Athlete record has the Athlete's name (defined in the FitnessMember class), ID number of type String and integer number of training days. Define a class...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • Base Class enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected:...

    Base Class enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected: HeroType type; string name; double health; double attackStrength; public: Character(HeroType type, const string &name, double health, double attackStrength); HeroType getType() const; const string & getName() const; /* Returns the whole number of the health value (static_cast to int). */ int getHealth() const; void setHealth(double h); /* Returns true if getHealth() returns an integer greater than 0, otherwise false */ bool isAlive() const; virtual void...

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • QUESTION 18 According to class materials, the program is allowed to overload operators only if at...

    QUESTION 18 According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type. 1. (a) True 2. (b) False QUESTION 19 According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT