Questions about C++ Classes
Hello,
I just finished reading Learning C++ through Game Programming, or some title similar to it to refreshen my C++ skills.(I did not bother to look up the book name, so it may be incorrect) It did cover some topics that I did not go over in my class. I just finished Chapter 10(this was the last chapter in the book) which basically talked about inheritance and polymorphism dealing with classes. I noticed that the author had put his ints,chars,strings and et cetera into his base class as protected. I did not understand why would he do it when you can put it in private and make functions in the public section to alter and display those things. I want to make sure I am able to do that. Also, why would someone put it in protected? Another question I have is that I seen that some of his base classes had useless info in it, where the derived classes basically "overload"(I think it is the correct term, but if not I am sorry) them. For example, it basically made it functionally while before it basically did nothing. Why would someone do that? In my logic I think it is kind of useless and don't it take away the performance speed by doing that? Well I will appericiate any responses.
~Carl J. Loucius
P.S. Did anyone read any of the C++ How to Program books by Dietel? Are they any good, because I think I am going to use that book next before moving on to more complicated things. For example, an graphics api.
Protected is just like private except when a class derives from a class, it inherits all of it's public and protected members. This could be useful for lets say you have maybe have a Sprite class which will just be a normal little sprite. This is the base class for all classes that derive from it. Then, you could inherit stuff from the Sprite class and give those classes a little more functionality. Excuse me if I don't make a lot of sense or don't even explain. It's pretty late here, and I am really tired and also sick, so yea.
I do wish this helps any!
Chad
I do wish this helps any!
Chad
Making methods or properties protected is similar to private, however, derived classes may access them. When they are private, not even the derived classes may access them directly.
I'm not sure I understand your second question but I think you MIGHT be talking about abstract classes. If a method is declared but not implemented as in the following:
class MyClass {
public:
virtual void Draw () = 0;
};
it is an abstract class and cannot be instantiated. A derived class may provide it's own implementation for that method and then may be instantiated. As in the above example, you may have similar objects that need to be drawn differently. The abstract class provides the interface while the derived objects implement their own draw methods.
Hope this helps!
- Jay
I'm not sure I understand your second question but I think you MIGHT be talking about abstract classes. If a method is declared but not implemented as in the following:
class MyClass {
public:
virtual void Draw () = 0;
};
it is an abstract class and cannot be instantiated. A derived class may provide it's own implementation for that method and then may be instantiated. As in the above example, you may have similar objects that need to be drawn differently. The abstract class provides the interface while the derived objects implement their own draw methods.
Hope this helps!
- Jay
Dietel and Deitel are my favorites authors, so in my opinion, yeah they are good.
Now to your question. Lets say you make a base Shape class, that has x and y location private members. You make a Circle class that inherits from the Shape class.
You decide to make it so that the location is set by the constructor and that from then on to change the location you will use a move function in the base class. Not a bad design, you make sure you have a valid location in the constructor and then make sure they are valid moves and life is good. Well almost.
Now you are going to Draw your circle using a virutal Draw function. How do you know where your circle is? You can't access the locations to find out. How about collisions? You can't do them in the base class because the collision for a circle would be different then a collision for a square. So how do you set the final location of the circle after the collision? Still can't access the locations in the base class.
So that is where protected comes in. It is for tightly coupled classes to allow direct access to the members of the base class. In my opinion it is VERY VERY OVERUSED. The problem is that if you have a long inheritance chain with lots of different classes that can change a value it can be hard to find a bug when a value is doing strange stuff. So use with care.
theTroll
Now to your question. Lets say you make a base Shape class, that has x and y location private members. You make a Circle class that inherits from the Shape class.
You decide to make it so that the location is set by the constructor and that from then on to change the location you will use a move function in the base class. Not a bad design, you make sure you have a valid location in the constructor and then make sure they are valid moves and life is good. Well almost.
Now you are going to Draw your circle using a virutal Draw function. How do you know where your circle is? You can't access the locations to find out. How about collisions? You can't do them in the base class because the collision for a circle would be different then a collision for a square. So how do you set the final location of the circle after the collision? Still can't access the locations in the base class.
So that is where protected comes in. It is for tightly coupled classes to allow direct access to the members of the base class. In my opinion it is VERY VERY OVERUSED. The problem is that if you have a long inheritance chain with lots of different classes that can change a value it can be hard to find a bug when a value is doing strange stuff. So use with care.
theTroll
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement