Page 1 of 1

Inheritance -- the glory!

Posted: Mon Mar 28, 2005 9:28 am
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.

Posted: Mon Mar 28, 2005 3:52 pm
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?

Posted: Mon Mar 28, 2005 4:08 pm
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.

Posted: Fri Apr 01, 2005 6:21 am
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

Posted: Sat Jun 18, 2005 10:46 am
by Guest
I've officially use inheritance in two of my programs. W00T!

I wish I was taking C class. You lucky b******s...

Posted: Sat Jun 18, 2005 1:32 pm
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.