C++ Abstract Classes/Interface [on hold]
up vote
-1
down vote
favorite
Here's code I wrote just to learn interfaces in C++ (could be presented as program which represents some units in rpg)
IUnit
is interface for Unit
GroundUnit
represents ground unit which can attack other units
AirUnit
same as ground unit
#include <iostream>
class Enemy;
class IUnit {
public:
virtual void hit(IUnit&) = 0;
virtual void setHp(short) = 0;
virtual short hp() const = 0;
virtual ~IUnit();
};
IUnit::~IUnit() {
}
class GroundUnit : public IUnit {
private:
short m_hp;
short m_damage;
short m_coeff;
public:
GroundUnit(short hp, short damage, short coeff)
: m_hp(hp)
, m_damage(damage)
, m_coeff(coeff)
{}
void hit(IUnit& unit) override;
short hp() const override;
bool dead() const { return m_hp < 1;}
void setHp(short) override;
};
class AirUnit : public IUnit {
private:
short m_hp;
short m_damage;
public:
AirUnit(short hp, short damage)
: m_hp(hp)
, m_damage(damage)
{}
void hit(IUnit& unit) override;
short hp() const override;
void setHp(short) override;
bool dead() const { return m_hp < 1;}
};
void AirUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage);
}
short AirUnit::hp() const
{
return m_hp;
}
void AirUnit::setHp(short newHp)
{
m_hp = newHp;
}
void GroundUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage * m_coeff);
}
short GroundUnit::hp() const
{
return m_hp;
}
void GroundUnit::setHp(short newHp)
{
m_hp = newHp;
}
int main() {
GroundUnit groundUnit1(100, 2, 2);
GroundUnit groundUnit2(100, 2, 3);
AirUnit airUnit1(100, 7);
AirUnit airUnit2(100, 4);
groundUnit1.hit(airUnit1);
groundUnit2.hit(groundUnit1);
airUnit1.hit(groundUnit1);
}
I know this is pretty short code but I would like to be sure if I am doing it right. Let's say
- I want interface, so all methods must be pure virtual (right now). I guess this is bad case for interface because methods such as
hp()
are same in both Derived Classes.
I want abstract class so methods can be implemented in
IUnit
class
2.1. Here I would like to (probably) have method
dead()
implemented in Abstract Class so Abstract Class must know theUnit
hp . How
could it be done?
I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class
c++ interface
put on hold as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, Edward, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
Here's code I wrote just to learn interfaces in C++ (could be presented as program which represents some units in rpg)
IUnit
is interface for Unit
GroundUnit
represents ground unit which can attack other units
AirUnit
same as ground unit
#include <iostream>
class Enemy;
class IUnit {
public:
virtual void hit(IUnit&) = 0;
virtual void setHp(short) = 0;
virtual short hp() const = 0;
virtual ~IUnit();
};
IUnit::~IUnit() {
}
class GroundUnit : public IUnit {
private:
short m_hp;
short m_damage;
short m_coeff;
public:
GroundUnit(short hp, short damage, short coeff)
: m_hp(hp)
, m_damage(damage)
, m_coeff(coeff)
{}
void hit(IUnit& unit) override;
short hp() const override;
bool dead() const { return m_hp < 1;}
void setHp(short) override;
};
class AirUnit : public IUnit {
private:
short m_hp;
short m_damage;
public:
AirUnit(short hp, short damage)
: m_hp(hp)
, m_damage(damage)
{}
void hit(IUnit& unit) override;
short hp() const override;
void setHp(short) override;
bool dead() const { return m_hp < 1;}
};
void AirUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage);
}
short AirUnit::hp() const
{
return m_hp;
}
void AirUnit::setHp(short newHp)
{
m_hp = newHp;
}
void GroundUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage * m_coeff);
}
short GroundUnit::hp() const
{
return m_hp;
}
void GroundUnit::setHp(short newHp)
{
m_hp = newHp;
}
int main() {
GroundUnit groundUnit1(100, 2, 2);
GroundUnit groundUnit2(100, 2, 3);
AirUnit airUnit1(100, 7);
AirUnit airUnit2(100, 4);
groundUnit1.hit(airUnit1);
groundUnit2.hit(groundUnit1);
airUnit1.hit(groundUnit1);
}
I know this is pretty short code but I would like to be sure if I am doing it right. Let's say
- I want interface, so all methods must be pure virtual (right now). I guess this is bad case for interface because methods such as
hp()
are same in both Derived Classes.
I want abstract class so methods can be implemented in
IUnit
class
2.1. Here I would like to (probably) have method
dead()
implemented in Abstract Class so Abstract Class must know theUnit
hp . How
could it be done?
I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class
c++ interface
put on hold as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, Edward, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
5
Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Here's code I wrote just to learn interfaces in C++ (could be presented as program which represents some units in rpg)
IUnit
is interface for Unit
GroundUnit
represents ground unit which can attack other units
AirUnit
same as ground unit
#include <iostream>
class Enemy;
class IUnit {
public:
virtual void hit(IUnit&) = 0;
virtual void setHp(short) = 0;
virtual short hp() const = 0;
virtual ~IUnit();
};
IUnit::~IUnit() {
}
class GroundUnit : public IUnit {
private:
short m_hp;
short m_damage;
short m_coeff;
public:
GroundUnit(short hp, short damage, short coeff)
: m_hp(hp)
, m_damage(damage)
, m_coeff(coeff)
{}
void hit(IUnit& unit) override;
short hp() const override;
bool dead() const { return m_hp < 1;}
void setHp(short) override;
};
class AirUnit : public IUnit {
private:
short m_hp;
short m_damage;
public:
AirUnit(short hp, short damage)
: m_hp(hp)
, m_damage(damage)
{}
void hit(IUnit& unit) override;
short hp() const override;
void setHp(short) override;
bool dead() const { return m_hp < 1;}
};
void AirUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage);
}
short AirUnit::hp() const
{
return m_hp;
}
void AirUnit::setHp(short newHp)
{
m_hp = newHp;
}
void GroundUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage * m_coeff);
}
short GroundUnit::hp() const
{
return m_hp;
}
void GroundUnit::setHp(short newHp)
{
m_hp = newHp;
}
int main() {
GroundUnit groundUnit1(100, 2, 2);
GroundUnit groundUnit2(100, 2, 3);
AirUnit airUnit1(100, 7);
AirUnit airUnit2(100, 4);
groundUnit1.hit(airUnit1);
groundUnit2.hit(groundUnit1);
airUnit1.hit(groundUnit1);
}
I know this is pretty short code but I would like to be sure if I am doing it right. Let's say
- I want interface, so all methods must be pure virtual (right now). I guess this is bad case for interface because methods such as
hp()
are same in both Derived Classes.
I want abstract class so methods can be implemented in
IUnit
class
2.1. Here I would like to (probably) have method
dead()
implemented in Abstract Class so Abstract Class must know theUnit
hp . How
could it be done?
I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class
c++ interface
Here's code I wrote just to learn interfaces in C++ (could be presented as program which represents some units in rpg)
IUnit
is interface for Unit
GroundUnit
represents ground unit which can attack other units
AirUnit
same as ground unit
#include <iostream>
class Enemy;
class IUnit {
public:
virtual void hit(IUnit&) = 0;
virtual void setHp(short) = 0;
virtual short hp() const = 0;
virtual ~IUnit();
};
IUnit::~IUnit() {
}
class GroundUnit : public IUnit {
private:
short m_hp;
short m_damage;
short m_coeff;
public:
GroundUnit(short hp, short damage, short coeff)
: m_hp(hp)
, m_damage(damage)
, m_coeff(coeff)
{}
void hit(IUnit& unit) override;
short hp() const override;
bool dead() const { return m_hp < 1;}
void setHp(short) override;
};
class AirUnit : public IUnit {
private:
short m_hp;
short m_damage;
public:
AirUnit(short hp, short damage)
: m_hp(hp)
, m_damage(damage)
{}
void hit(IUnit& unit) override;
short hp() const override;
void setHp(short) override;
bool dead() const { return m_hp < 1;}
};
void AirUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage);
}
short AirUnit::hp() const
{
return m_hp;
}
void AirUnit::setHp(short newHp)
{
m_hp = newHp;
}
void GroundUnit::hit(IUnit& unit)
{
unit.setHp(unit.hp() - m_damage * m_coeff);
}
short GroundUnit::hp() const
{
return m_hp;
}
void GroundUnit::setHp(short newHp)
{
m_hp = newHp;
}
int main() {
GroundUnit groundUnit1(100, 2, 2);
GroundUnit groundUnit2(100, 2, 3);
AirUnit airUnit1(100, 7);
AirUnit airUnit2(100, 4);
groundUnit1.hit(airUnit1);
groundUnit2.hit(groundUnit1);
airUnit1.hit(groundUnit1);
}
I know this is pretty short code but I would like to be sure if I am doing it right. Let's say
- I want interface, so all methods must be pure virtual (right now). I guess this is bad case for interface because methods such as
hp()
are same in both Derived Classes.
I want abstract class so methods can be implemented in
IUnit
class
2.1. Here I would like to (probably) have method
dead()
implemented in Abstract Class so Abstract Class must know theUnit
hp . How
could it be done?
I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class
c++ interface
c++ interface
edited 2 days ago
Calak
1,48912
1,48912
asked 2 days ago
phos456
172
172
put on hold as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, Edward, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, Edward, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
5
Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
2 days ago
add a comment |
5
Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
2 days ago
5
5
Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
2 days ago
Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
5
Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
2 days ago