Inheritance -- the glory!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Inheritance -- the glory!

Post by Falco Girgis »

Lately I've been impressed with C++ programming. We've actually been getting into some goods. I can't believe I even got into any game programming without knowing inheritance. This crap is easy and is super useful/powerful. Check out what I cooked up in 10 minutes:

Code: Select all

#include <iostream>
#include <stdio.h>

class rectangle {
      protected: 
                 int width, length;
      public:
             void set_width(int w) { width = w; }
             void set_length(int l) { length = l; }
             int get_width() { return width; }
             int get_length() { return length; }
             int area() { return width * length; }
             rectangle(): width(10), length(10) {}
             rectangle(int w, int l): width(w), length(l) {}    
             ~rectangle() {}
};

class prism: public rectangle {
      private:
              int height;
      public:
             void set_height(int h) { height = h; }
             int get_height() { return height; }
             int area() { return width * height * length; }
             prism(): rectangle(), height(10) {}
             prism(int w, int l, int h): rectangle(w, l), height(h) {}
             ~prism() {}
};

using namespace std;

int main() {
    rectangle Recty;
    rectangle Rictor(3, 4);
    prism Prissy;
    prism Prictor(3, 4, 5);
    
    printf("Recty - \n\nWidth - %d\nLength - %d\nArea - %d\n", Recty.get_width(), Recty.get_length(), Recty.area());
    printf("Rictor - \n\nWidth - %d\nLength - %d\nArea - %d\n", Rictor.get_width(), Rictor.get_length(), Rictor.area());
    printf("Prissy - \n\nWidth - %d\nLength - %d\nHeight - %d\nArea - %d\n", Prissy.get_width(), Prissy.get_length(), Prissy.get_height(), Prissy.area());
    printf("Prictor - \n\nWidth - %d\nLength - %d\nHeight - %d\nArea - %d\n", Prictor.get_width(), Prictor.get_length(), Prictor.get_height(), Prictor.area());
    
    system("PAUSE");
    return 0;
}
Yes, ph33r its awesomness. My very first inheritance program. <3 this baby. <3 <3 <3.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

.....

I don't know what this line does:

Code: Select all

rectangle(): width(10), length(10) {}
Strange syntax to me. Can you enlighten?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

JS Lemming wrote:.....

I don't know what this line does:

Code: Select all

rectangle(): width(10), length(10) {}
Strange syntax to me. Can you enlighten?
It's a better way to use constructors. It's alot more efficient than setting width and length equal to ten in the function body. You use the colon then pass what the value of the variable should be to that variable.

When you're done with that if you need anything else in your constructor that can't be passed through the variable's name (like initting a string with strcpy), you can do it in the constructor's body.

It's just more efficient. It's called an initializer list.
DJ Yoshi
Game Developer
Game Developer
Posts: 1021
Joined: Wed Mar 02, 2005 9:12 pm
Location: Madison, AL

Post by DJ Yoshi »

Hell yeah man. This stuff actually seems really interesting...instead of that chapter on arrays.

Just WTF? Arrays are like, so damn easy. :nono: Mrs. Rountree
There is no signature.
Guest

Post by Guest »

I've officially use inheritance in two of my programs. W00T!

I wish I was taking C class. You lucky b******s...
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Arce wrote:I've officially use inheritance in two of my programs. W00T!

I wish I was taking C class. You lucky b******s...
C? Inheritance? No -- you mean C++ class.
Post Reply