[C++] Classes

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
The_Perfection
New Member
Posts: 4
Joined: Tue 13 Feb, 2007 4:51 pm

[C++] Classes

Post by The_Perfection »

Hello all, I've been programming in C++ for a while and still have problems with classes. (Specifically inheritance.)

Does anyone happen to have or can quickly make some sample problems involving classes and/or inheritance? I don't need to be terribly complicated, in competition I only have (I think) two hours to take a test and make a functional program. Last year at the national level they asked us to use inheritance and it killed me...

Anywho, it's my first post! Hello all!
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Hello!

What exactly is your complaint with inheritance - the theoretical or the practical?
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

There are loads of OOP tutorials floating around the internet.

You can read about inheritance here: http://en.wikipedia.org/wiki/Inheritanc ... ramming%29


inheritance is not at all as hard to understand as the article makes you believe though
The_Perfection
New Member
Posts: 4
Joined: Tue 13 Feb, 2007 4:51 pm

Post by The_Perfection »

It's not so much that I don't understand it, it would just be nice if I had a practical sample problem to use it with because I cant think of anything. The only time I've ever actually used (or actually, tried to use) inheritance in a program was at that competition, and I haven't used it since. (Since most of my programming is for the GBA or DS, I use structs because they are simpler and easier to use IMO, not to mention, (IIRC,) they have little overhead. (Don't know if that last part is true or not, but I'm pretty sure I've heard it somewhere.))

In really short: Can you come up with some sample problems that would use classes/inheritance? I can figure out most of the other stuff on my own.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

A common place where I'll use inheritence is where I have a number of types that are roughly similar but each needs their own specific brand of magic.

For example, in Brass 2 a source file is broken down into a list of tokens. A token at the most basic level is just a string ("+", "call", ",") but ends up being one of a specific type of token that derives from the 'Token' base class (in this case, an OperatorToken, ConstantToken and a PunctuationToken). The OperatorToken class has an extra method that can be used to compare one OperatorToken with another to establish the order of precedence; the ConstantToken has methods that can try and extract a numeric value from it; the PunctuationToken does nothing special.

I know interfaces as a feature of the .NET framework, but their basic design idea is a good one. In the OperatorToken case I implement the IComparable interface, which is another form of inheritence. I have to override a CompareTo method in the class, and this means that, for example, I can chuck my OperatorTokens into a list and call .Sort and have them automatically sorted for me as the List.Sort method can use the CompareTo method on each item in the list to establish the sorted order.

I believe two of the typical examples for inheritence are a Shape base class, and each inheriting class has to offer a GetSides method and a Draw method. Alternatively there's the Animal base class, where the inheriting classes have a GetNumberOfLegs method and a MakeNoise method, that would "Moo" for the Cow : Animal and "Meow" for the Cat : Animal.

(I'm mainly a C# programmer so am not 100% aware of the way classes work in C++; for example in my world a class and a struct are almost identical beyond the fact that a class is a reference type and a struct is a value type - the basic ideas should be sound, though).
The_Perfection
New Member
Posts: 4
Joined: Tue 13 Feb, 2007 4:51 pm

Post by The_Perfection »

Hmm... interesting. I can see how all that would work for Brass. (Although, I'd like to mention that I have absolutely no earthly idea what it is because I only work in C/C++ right now.)

I don't know what you are talking about with the "IComparable interface" and whatnot though, you lost me there.

Okay, two typical examples. I'll get to work on them and see what I can get out of them.

Yes, the way you wrote that looks right for C++, if I remember correctly,

Code: Select all

class newClass : baseClass
is how it's done. I think.
The_Perfection
New Member
Posts: 4
Joined: Tue 13 Feb, 2007 4:51 pm

I figured it out!

Post by The_Perfection »

Apologies for the double-post. (I know it's a practice to abstain from if possible.)

After messing with Visual Studio .NET I stumbled across something that made me feel really stupid. It can create classes for you, and one of the things it can do for you automatically is - you guessed it - inheritance. I made a quick test and it worked. If only I had known about it at nationals... oh well. State is coming up this week and I'm more prepared now.

Thank you for all your help!
Post Reply