public class Object
{
public Image icon;
}
public class Robot : Object
{
public string owner;
public bool is_enemy;
}
public class MasterRobot : Robot
{
public MasterRobot()
{
if(!is_enemy)
{
icon = Image.FromFile("images\\mr_friendly.bmp");
}
else
{
icon = Image.FromFile("images\\mr_enemy.bmp");
}
}
}
public class Medic : Robot
{
}
public class Engineer : Robot
{
}
public class Destroyer : Robot
{
}
public class Wall : Object
{
}
Leveraging OOP
Given the following classes and inheritance that is in place, how can I assign the appropriate icons to each KIND of Robot (as you can see I am doing with the MasterRobot) without having to duplicate the same code in the constructor for each class?
I suggest that you create a base class constructor that receives the 'enemy' and 'friend' filenames, and pass them to this constructor from the constructor of the derived classes.
Quote:Original post by ToohrVyk
I suggest that you create a base class constructor that receives the 'enemy' and 'friend' filenames, and pass them to this constructor from the constructor of the derived classes.
Can you give me a source code example? I don't quite see how this helps.
You will need to write at least two things: the names of the files and a short command to explain what to do with them. You will not be able to make things any shorter. My suggestion is to use:
This reduces the amount of additional characters to be typed to :base(,) for each derived class.
class BaseClass { protected BaseClass( string friend, string enemy ) { if( friendly ) { /* load friend */ } else { /* load enemy */ } } public BaseClass( ) : this( "BaseFriend.tga", "BaseEnemy.jpg" ) { }}class Derived : public BaseClass { public Derived( ) : base( "DerivedFriend.tiff", "DerivedEnemy.gif" ) { }}
This reduces the amount of additional characters to be typed to :base(,) for each derived class.
Quote:Original post by ToohrVyk
You will need to write at least two things: the names of the files and a short command to explain what to do with them. You will not be able to make things any shorter. My suggestion is to use:
*** Source Snippet Removed ***
This reduces the amount of additional characters to be typed to :base(,) for each derived class.
So like this?
public class Object{ public Image icon;}public class Robot : Object{ public string owner; public bool is_enemy; public Robot() { } protected Robot(string friendly_image, string enemy_image) { if(!is_enemy) { icon = Image.FromFile(friendly_image); } else { icon = Image.FromFile(enemy_image); } } }public class MasterRobot : Robot{ public MasterRobot( ) : base( "images\\mr_friendly.bmp", "images\\mr_enemy.bmp" ) { }} public class Medic : Robot{ public Medic( ) : base( "images\\med_friendly.bmp", "images\\med_enemy.bmp" ) { }}public class Engineer : Robot{ public Engineer( ) : base( "images\\eng_friendly.bmp", "images\\eng_enemy.bmp" ) { } }public class Destroyer : Robot{ public Destroyer( ) : base( "images\\dest_friendly.bmp", "images\\dest_enemy.bmp" ) { }}public class Wall : Object{ public Wall() { icon = Image.FromFile("images\\wall.bmp"); }}
class Robot:public Object { *insert base robot code here*public: Rect rect;};class ImageView: public View { Image image; Object& object;public: ImageView(std::string filename, Object& object_):image(filename), object(object_) {} void render(Surface& screen) { screen.draw(image, object.rect); }};...Robot* robots = {new MasterRobot(), new Medic()};ImageView robot_views = {ImageView("master_robot.png"), ImageView("medic.png")};
bytecoder: the OP wants to specify the graphics per-class, not per-instance. (and is using C#, not C++)
Quote:Original post by ToohrVyk
bytecoder: the OP wants to specify the graphics per-class, not per-instance. (and is using C#, not C++)
Correct on both counts. I think I have my answer now....I tested and it seems to be working just fine.
I had another question:
//Instead of doing this:public class Destroyer : Robot{ public Destroyer( ) : base( "images\\dest_friendly.bmp", "images\\dest_enemy.bmp" ) { }}//Can I somehow do THIS:public class Destroyer : Robot{ public Destroyer( ) : base( iType.DestroyerFriendly, iType.DestroyerEnemy) { }}iType.DestroyerFriendly = "images\\dest_friendly.bmp"iType.DestroyerEnemy = "images\\dest_enemy.bmp"
What is a nice way to do this...make things a little cleaner?
Quote:Original post by ToohrVyk
bytecoder: the OP wants to specify the graphics per-class, not per-instance. (and is using C#, not C++)
That's what mine does (it's actually even more flexible). Look into Model-View-Controllers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement