C++ Abstract Classes/Interface [closed]











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




  1. 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.


  2. 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 the Unit hp . How
    could it be done?




I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class










share|improve this question















closed as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight Nov 14 at 8:56


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.
    – πάντα ῥεῖ
    Nov 13 at 22:02















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




  1. 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.


  2. 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 the Unit hp . How
    could it be done?




I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class










share|improve this question















closed as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight Nov 14 at 8:56


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.
    – πάντα ῥεῖ
    Nov 13 at 22:02













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




  1. 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.


  2. 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 the Unit hp . How
    could it be done?




I would like to know some "rules of thumb" what should (not) be in Interface/Abstract Class










share|improve this question















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




  1. 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.


  2. 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 the Unit 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 9:03









Calak

1,564112




1,564112










asked Nov 13 at 22:00









phos456

172




172




closed as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight Nov 14 at 8:56


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.




closed as off-topic by πάντα ῥεῖ, Snowhawk, Edward, Quill, Toby Speight Nov 14 at 8:56


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.
    – πάντα ῥεῖ
    Nov 13 at 22:02














  • 5




    Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
    – πάντα ῥεῖ
    Nov 13 at 22:02








5




5




Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
Nov 13 at 22:02




Please give more context what this code is supposed to do, otherwise it's hard to give you a good review.
– πάντα ῥεῖ
Nov 13 at 22:02















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Сан-Квентин

Алькесар

Josef Freinademetz