oop
Declaring multiple object pointers on one line causes compiler error
sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes; Does not declare 3 pointers – it is one pointer and 2 objects. sf::Sprite* unfortunately does not apply to all the variables declared following it, just the first. It is equivalent to sf::Sprite* re_sprite_hair; sf::Sprite re_sprite_body; sf::Sprite re_sprite_eyes; You want to do: sf::Sprite *re_sprite_hair, *re_sprite_body, *re_sprite_eyes; You need to put one … Read more
Can you write any algorithm without an if statement?
Smalltalk, which is considered as a “truly” object oriented language, has no “if” statement, and it has no “for” statement, no “while” statement. There are other examples (like Haskell) but this is a good one. Quoting Smalltalk has no “if” statement: Some of the audience may be thinking that this is evidence confirming their suspicions … Read more
What’s a good example for class inheritance? [closed]
I like the Stream hierarchy. The idea is that anything can use a stream without usually caring what kind of stream it is, and individual subclasses handle the storage differently (e.g. NetworkStream, MemoryStream and FileStream in .NET). If you’re interested in interfaces, then IEnumerable<T> in .NET is a great one – you can iterate over … Read more
Is OOP abused in universities? [closed]
The professors have the disadvantage that they can’t put you on huge, nasty programs that go on for years, being worked on by many different programmers. They have to use rather unconvincing toy examples and try to trick you into seeing the bigger picture. Essentially, they have to scare you into believing that when an … Read more
Is it proper for equals() to depend only on an ID?
Whether you should do this depends on the semantics of your class. That is, what does it mean to say that two objects of your class are equivalent? The most important distinction is between objects with value semantics and objects with entity semantics. Entity objects are not equivalent even if they have equivalent attributes (colour, … Read more
Avoiding parallel inheritance hierarchies
I am thinking of using the Visitor pattern. public class Car : Vehicle { public void Accept( IVehicleFormatter v ) { v.Visit (this); } } public class Truck : Vehicle { public void Accept( IVehicleFormatter v ) { v.Visit (this); } } public interface IVehicleFormatter { public void Visit( Car c ); public void Visit( … Read more
How to use Dependency Injection without breaking encapsulation?
Many of the other answers hint at it, but I’m going to more explicitly say that yes, naive implementations of dependency injection can break encapsulation. The key to avoiding this is that calling code should not directly instantiate the dependencies (if it doesn’t care about them). This can be done in a number of ways. … Read more
What is high level modules and low level modules.?
High level module is the interface / abstraction that will be consumed directly by the presentation layer. Low level on the other hand are bunch of small modules (subsystems) help the high level do their work. Example below is the high level module. I have excluded the dependency constructor injection for shorter sample. public class … Read more