Inheritance -- the glory!
Posted: Mon Mar 28, 2005 9:28 am
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:
Yes, ph33r its awesomness. My very first inheritance program. <3 this baby. <3 <3 <3.
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;
}