I have the classes Player and HumanPlayer. HumanPlayer is derived from Player.
class Player {
private:
int id;
protected:
string name;
public:
Player(int id);
~Player();
}
class HumanPlayer : public Player {
public:
HumanPlayer(int id, string name);
}
I want to make a constructor for HumanPlayer the set the Player id and name but I don't seem to figure out how to set Player::id to the HumanPlayer::id.
This is what I have worked out but gives an error
"int Player::id' is private within this context"
HumanPlayer::HumanPlayer(int id, string name) : Player(id){
this -> id = id;
this -> name = name;
}
For your understanding.
class Player {
private:
int id;
protected:
std::string name;
public:
//Base class constructor, initialize Id.
Player(int i):id(i)
{}
~Player() {}
//Test
int GetId()
{
return id;
}
std::string GetName()
{
return name;
}
};
class HumanPlayer : public Player {
public:
HumanPlayer(int i, std::string s) :Player(i) //Pass on Id base class constructor
{
name = s; //Protected variable accessible.
}
};
void main()
{
HumanPlayer hPlayer(10, "Test");
std::cout << hPlayer.GetId() << std::endl;
std::cout << hPlayer.GetName() << std::endl;
}
Related
I have declared a class Products and another class CD the class CD is inheriting the class Products.
Now I have declared an constructor to update the value of the. and I am getting an error
#include <iostream>
#include <string>
class Products
{
private:
std::string name;
std::string type;
double price;
public:
virtual std::string getname();
virtual double getprice();
virtual void show();
std::string gettype()
{
return type;
}
};
class CD: public Products
{
private:
std::string artist;
std::string studio;
public:
CD(std::string sname,double sprice,std::string sartist,std::string sstudio)
{
this->type = "CD";
this->name = sname ;
this->price = sprice;
this->artist = sartist;
this->studio = sstudio;
}
void show()
{
std::cout<<"\nName of the CD:\t"<<this->name;
std::cout<<"\nArtist of the CD:\t"<<this->artist;
std::cout<<"\nStudio of the CD:\t"<<this->studio;
std::cout<<"\nPrice of the cd:\t"<<this->price;
}
};
int main()
{
CD obj("Oceans",49,"somesinger","somestudio");
}
ERROR :
In constructor 'CD::CD(std::string, double, std::string)';
'std::string Products::type' is private within this context
this->type="CD";
'std::string Products::name' is private within this context
this->name=sname;
'double Products::price' is private within this context
this->price= sprice;
Basically it is not giving error for the private data members of the CD class but just the data members that are being inherited from Products Class
#include <iostream>
#include <string>
class Products
{
private:
std::string m_name;
std::string m_type;
double m_price;
public:
// No need for your setters/getters to be virtual
// if the derived class won't override anything or not
const std::string& getType() const { return m_type; }
const std::string& getName() const { return m_name; }
double getPrice() const { return m_price; }
void setType(const std::string& new_type) { m_type = new_type; }
void setName(const std::string& new_name) { m_name = new_name; }
void setPrice(double new_price) { m_price = new_price; }
// Force derived class to override function
virtual void show() = 0;
};
class CD: public Products
{
private:
std::string artist;
std::string studio;
public:
CD(std::string sname,double sprice,std::string sartist,std::string sstudio)
{
this->setType("CD");
this->setName(sname) ;
this->setPrice(sprice);
this->artist = sartist;
this->studio = sstudio;
}
void show()
{
std::cout<<"\nName of the CD:\t"<<this->getName();
std::cout<<"\nArtist of the CD:\t"<<this->artist;
std::cout<<"\nStudio of the CD:\t"<<this->studio;
std::cout<<"\nPrice of the cd:\t"<<this->getPrice();
}
};
int main()
{
CD obj("Oceans",49,"somesinger","somestudio");
obj.show();
}
I want you to understand some changes here. First the removal of virtual keyword. In your case the setters/getters had no need to be virtual, as they were not being overriden or didn't have a need to be based on the current example. Second, the setters/getters are setup to access the private members accordingly. We now use these functions within class CD. Also we changed the function show() to be pure virtual notice the = 0 at the end. I added a comment saying this forces derived classes to override the function. Lastly, your main wasn't doing anything so I added a obj.show() to actually print something.
In this solution, I've added a constructor for Products, and CD's constructor calls it to initialize the members that are private to Products.
I removed the virtual on getName and getPrice since these features don't change other products.
show remains virtual, and I split it into a piece in Products and a piece in CD so they each display their respective fields. This separates the printing according to where the variables are, so for example, another class derived from Products wouldn't have to reimplement printing of name and price.
#include <iostream>
#include <string>
class Products
{
private:
std::string name;
std::string type;
double price;
public:
std::string getname(); // Does not need to be virtual, as it's not overriden
double getprice(); // Also does not need to be virtual
virtual void show() const {
std::cout<<"\nName of the " << type << ":\t"<<this->name;
std::cout<<"\nPrice of the " << type << ":\t"<<this->price;
};
Products (const std::string &stype, double sprice, const std::string &sname)
: name (sname), type (stype), price (sprice) {
}
std::string gettype() const
{
return type;
}
};
class CD: public Products
{
private:
std::string artist;
std::string studio;
public:
CD(const std::string &sname,double sprice, const std::string &sartist, const std::string &sstudio)
: Products ("CD", sprice, sname)
{
artist = sartist;
studio = sstudio;
}
void show() const override
{
Products::show(); // Call parent show() to show the basics
std::cout<<"\nArtist of the " << gettype() << ":\t"<<this->artist;
std::cout<<"\nStudio of the " << gettype() << ":\t"<<this->studio;
}
};
int main()
{
Products shoe ("Shoe", 3.49, "Nike runner");
shoe.show();
CD obj("Oceans",49,"somesinger","somestudio");
obj.show();
}
I am stuck on a problem involving Polymorphism. My code keeps telling me that there is no default constructor for my class in this case I named creature, even though I did instantiate a constructor that takes a string in the creature class. I have a feeling I am missing something very small here and was hoping someone could help me with this. The code is as follows below.
class Creature
{
public:
Creature(string);
virtual void DoAction() = 0;
virtual void DrawOnScreen() = 0;
protected:
string CreatureName;
};
Creature::Creature(string pname)
{
this->CreatureName = pname;
};
class Monster : public Creature
{
Monster(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
Monster::Monster(string pname)
{
this->CreatureName = pname;
};
class Player : public Creature
{
Player(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
Player::Player(string pname)
{
this->CreatureName = pname;
}
class WildPig : public Creature
{
WildPig(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
WildPig::WildPig(string pname)
{
this->CreatureName = pname;
}
class Dragon : public Creature
{
Dragon(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
Dragon::Dragon(string pname)
{
this->CreatureName = pname;
}
I only included the classes within this snippet to keep it short and focused on where I believe the problem lies. Any help would be greatly appreciated.
Monster::Monster(string pname)
{
this->CreatureName = pname;
}
is equivalent to
Monster::Monster(string pname) : Creature()
{
this->CreatureName = pname;
}
And Creature doesn't have default constructor. You need:
Monster::Monster(string pname) : Creature(pname) {}
I have three base Classes {Player,Weapon,Game} and one inherited Class Warrior from Player. The code I added may not be minimal but I added anything that can be relevant. Since I didn't know where to look.
This is my test for simple debugging, I add Players into vectorPlayers which is definded in game.h
int main() {
Weapon w1("hello",STRENGTH,3);
Game game(3);
game.addWarrior("s22","we",STRENGTH,3,55);
//4
game.addWarrior("s22","we",STRENGTH,3,55);
return 0;
}
When I reach the the 4th line, vectorPlayers[0] includes the correct inputs. But when I enter to game.cpp and call the function addPlayer in addWarrior,suddenly players_name for example switches to
error: Cannot access memory at address 0x1
//inside game.cpp
GameStatus Game::addPlayer(const string playerName, const string weaponName,
Target target,int hit_strength)
{
Weapon newWeapon(weaponName,target,hit_strength);
Warrior newWarrior(playerName,newWeapon,false);
if(ifNameAlreadyExists(playerName)) {
addRegPlayer(static_cast<Player*>(&newWarrior));
}
return SUCCESS;
}
GameStatus Game::addRegPlayer(Player* player)
{
playersVector.push_back(player);
}
//base classes
class Game {
private:
int maxPlayer;
vector<Player*> playersVector;
}
class Player {
string player_name;
int level;
int strength;
Weapon player_weapon;
protected:
int place;
int life;
public:
Player(const string &name, const Weapon &weapon);
virtual ~Player();
}
//inherited class
class Warrior : public Player {
private:
bool rider;
public:
Warrior ();
Warrior (string const& name, Weapon const& weapon, bool rider);
~ Warrior () override = default;
Warrior &operator=(const Warrior &warrior) ;
Warrior ( const Warrior &warrior) = default;
void makeStep() override;
};
//Constructor of Warrior
Warrior::Warrior(string const& name, Weapon const& weapon, bool rider) :
Player(name,weapon),rider(rider){
if (weapon.getTarget() == LEVEL){
throw IllegalWeapon();
}
}
//constructor of player
Player::Player(const string& name, const Weapon& weapon) :
level(1),life(1),strength(1),player_weapon(weapon),player_name(name),place(0){
}
I have 2 classes. Base - Animal and derived MyDog. I want to change the ageOfAnimal from the setAge method and then display changed value from derived class - MyDog
code:
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
int ageOfAnimal = 0;
int setAge(int age) {
ageOfAnimal = age;
return ageOfAnimal;
}
};
class MyDog: public Animal
{
public:
void getInfo() {
cout << ageOfAnimal;
}
};
int main()
{
Animal *animal = new Animal;
animal->setAge(20);
MyDog myDog;
myDog.getInfo();
return 0;
}
Output: 0
Why can't I get 20? Is this even posible?
UPDATE
Thanks for answers but what i want is to change the state of ageOfAnimal property from different objects
class Animal
{
public:
int ageOfAnimal = 0;
int setAge(int age) {
ageOfAnimal += age;
return ageOfAnimal;
}
}
class MyCat: public Animal
{
public:
void getInfo() {
cout << ageOfAnimal;
}
}
class MyDog: public Animal
{
public:
void getInfo() {
cout << endl << ageOfAnimal;
}
}
int main() {
myDog dog;
dog.getInfo();
MyCat cat;
cat.getInfo();
}
output:
20
20
but what i want to see it's 20 21. How to do this?
In the usage code there are two different objects: Animal * animal and MyDog myDog. Both objects have their own instances of ageOfAnimal: changing it in one object does not modify the second object (which is the expected behavior).
1. As MyDog is derived from Animal one could simply:
MyDog myDog;
myDog.setAge(20);
myDog.getInfo();
2. If you want ageOfAnimal to be global for all instances of Animal you should make it a static member.
UPDATE
To reflect the updated question: one can put getInfo into Animal class. Default age for cats and dogs can be then specified in the constructors.
Even better way would be to specify age in Animal ctor and use constructor initializer lists for MyDog and MyAnimal ctors.
class Animal
{
public:
int ageOfAnimal = 0;
int setAge(int age)
{
ageOfAnimal += age;
return ageOfAnimal;
}
void getInfo()
{
cout << endl << ageOfAnimal;
}
};
class MyCat : public Animal
{
public:
MyCat()
{
setAge(40);
}
};
class MyDog : public Animal
{
public:
MyDog()
{
setAge(20);
}
};
int main()
{
MyDog dog;
dog.getInfo();
MyCat cat;
cat.getInfo();
}
Output:
20
40
Or if you want age to be shared between all objects, make it a static member as suggested earlier:
class Animal
{
public:
static int ageOfAnimal;
int setAge(int age)
{
ageOfAnimal += age;
return ageOfAnimal;
}
void getInfo()
{
cout << ageOfAnimal;
}
};
// Initialize age
int Animal::ageOfAnimal = 0;
I'm trying to become accustomed to classes. Here I've made a base class called Animal and a derived class called Dog.
I was originally able to to get the base class to work alone, but when I tried adding a derived class, things got messy and I got errors. Here is the code, and if you could let me know what I'm doing wrong, that'd be great!
#include <iostream>
#include <string>
using namespace std;
class Animal{
protected:
int height, weight;
string name;
public:
int getHeight() { return height; };
int getWeight() { return weight; };
string getName() { return name; };
Animal();
Animal(int height, int weight, string name);
};
Animal::Animal(int height, int weight, string name){
this->height = height;
this->weight = weight;
this->name = name;
}
class Dog : public Animal{
private:
string sound;
public:
string getSound() { return sound; };
Dog(int height, string sound);
};
Dog::Dog(int height, string sound){
this->height = height;
this->sound = sound;
}
int main()
{
Animal jeff(12, 50, "Jeff");
cout << "Height:\t" << jeff.getHeight << endl;
cout << "Weight:\t" << jeff.getWeight << endl;
cout << "Name:\t" << jeff.getName << endl << endl;
Dog chip(10, "Woof");
cout << "Height:\t" << chip.getHeight() << endl;
cout << "Sound:\t" << chip.getSound() << endl;
}
The default constructor for the Animal class is not defined. You need:
Animal::Animal() : height(0), weight(0) // Or any other desired default values
{
}
You should also have a virtual destructor on the base class.
class Animal
{
public:
~Animal() {} // Required for `Animal* a = new Dog(...); delete a;`
// deletion via base pointer to work correctly
};
Edit:
Upon removal of Animal() I get an error that says 'Animal': no appropriate default constructor available
You need to implement the default constructor (see above). Without it the int members will not be initialized and have undefined values.