Please help me with this lab question. This lab has to be done by C++. ( Lab 2-p
ID: 3754029 • Letter: P
Question
Please help me with this lab question. This lab has to be done by C++. (Lab 2-player.h and player.cpp is attached at the bottom)
In this lab you’ll be using inheritance to create new derived classes from base classes. Start by making a new Visual Studio project and create files called location.h, location.cpp and main.cpp (you will also want to add player.h and player.cpp from your Lab 2). Start by writing the Location base class so that it contains two attributes: a boolean variable called visited and a char called symbol (make these variables protected in the class – Make sure everyone in your group knows why these variables should be protected). Write set and get methods for these attributes and write a constructor for this class with the paramater list (char s = ‘ ‘) . This constructor should initialize symbol to the parameter s and set visited to false. Also, write two functionality methods for this class called visit and draw. The visit method should set the visited variable to true and return 1; The draw method should check to see if visited is true, and if so it should simply display symbol on the screen, otherwise a period should be displayed on the screen (“.”). Use the following prototypes for these methods:
virtual void draw();
virtual int visit(Player &p);
Now, write a new class called Crystal that is derived from Location. The class should begin:
class Crystal : public Location //: public BaseClass specifies inheritance
This class describes something that the player of your game can visit or find (in this case, it will be a crystal that the player can find). This class should have an attribute called taken (bool). Write an appropriate constructor and set and get methods for this class. The constructor should take the following form:
Crystal::Crystal(char s)
: Location(s) //: Location(s) passes the parameter s to the base
{ //class constructor (in this case, Location)
taken = false; //initialize class specific attributes here
}
Discuss with your group what this code does (in particular the part “: Location(s)”). The code for the draw method of this class below. Discuss with your group what you think this code is doing? Add this code to your project in crystal.cpp.
void Crystal::draw()
{
if (visited == true && taken == false)
cout << symbol;
else if (taken == true)
cout << " ";
else
cout << ".";
}
Now write the visit method so that if the attribute visited is false, visited is set to true and a single cout statement displays something like “You found a magic crystal.”. For now, have the visit method return 1.
Now write a new class that also inherits from the Location class (can be called anything that you want). This new class should follow the format of how you created the Crystal class (i.e. give it appropriate attributes, a constructor, set and get methods, and define draw and visit).
Once you have your classes written, write a short main to test them out. Create objects of each of the classes Location, Crystal, and whatever you named your last class. Call the draw methods of these objects, then call their visit methods (you will need to pass them a Player object). Finally, call your draw methods again to make sure that everything is working as expected.
Player.h
using namespace std;
class Player
{
private:
string name;
int age;
public:
Player(); //default constructor declaration
Player(string n, int a); //parameterized constructor declaration
void setName(string n);
void setAge(int a);
string getName();
int getAge();
void print();
};
player.cpp
#include "player.h"
Player::Player()
{
name = "";
age = 0;
}
Player::Player(string n,int a)
{
name = n;
age = a;
}
void Player::setName(string n)
{
name = n;
}
void Player::setAge(int a)
{
age = a;
}
string Player::getName()
{
return name;
}
int Player::getAge()
{
return age;
}
void Player::print()
{
cout<<"Name: "<<name<<", Age: "<<age<<endl;
}
Explanation / Answer
Inheritance is one of the Characteristic of OOPS Concepts
Re-Usability Functionality is illustrated as part of Inheritance. Deriving One Class (Parent) Member Functions and Member Variables from Another Class (Child) is called Inheritance.
Depends on Access Specifier Type ( Public, Private, Protected ) Inheritance Behaviour can Vary
Lab 2 Code Logic is Illustrated below
**Inheriting Player Class from Location Class
**Create Location Object and execute/manupilate Player Class Member Funcions
#include <iostream>
using namespace std;
class Crystal:Public Location
{
//empty class
};
class Player:Public Location
{
private:
string name;
int age;
public:
Player(); //default constructor declaration
Player(string n, int a); //parameterized constructor declaration
void setName(string n);
void setAge(int a);
string getName();
int getAge();
void print();
};
Player::Player()
{
name = "";
age = 0;
}
Player::Player(string n,int a)
{
name = n;
age = a;
}
void Player::setName(string n)
{
name = n;
}
void Player::setAge(int a)
{
age = a;
}
string Player::getName()
{
return name;
}
int Player::getAge()
{
return age;
}
void Player::print()
{
cout<<"Name: "<<name<<", Age: "<<age<<endl;
}
int main()
{
Player play("AJAY",20);
cout<<"After Parameterized Constructor Initialization Entered Name is:"<<obj->getName()<<endl;
cout<<"After Parameterized Constructor Initialization Entered Age is:"<<obj->getAge()<<endl;
obj->print(); //Print Names and Age Values Assigned in parameterized Constructor
Location *obj = &play;
obj->setName("ARUN");
obj->setAge(18);
cout<<"Name Set is:"<<obj->getName()<<endl;
cout<<"Age Set is:"<<obj->getAge()<<endl;
obj->print(); //Print Names and Age Values Set
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.