Selasa, 15 Februari 2011

Source Code - Inheritance

Mengenai Inheritance menghitung luas bangun datar (square, circle, rectangle, oval) dengan menggunakan struktur model pewarisan Multilevel ?


#include

#include

using namespace std;

class BasePoint { //class induk

public:

BasePoint(int px, int py):x(px),y(py) {} //constructor dengan default argument

int x, y; //position

virtual string type() = 0; //default constructor dengan nilai 0 (nol)

virtual void info() {

cout << endl << "figure: " << type() << endl;

cout << "position: x=" << x << ", y=" << y << endl;

}

};

class Figure1P : public BasePoint { // pewarisan bersifat public dari class BasePoint

public:

Figure1P(int px, int py, int r):p1(r),BasePoint(px, py) {} //constructor dengan default argument

int p1;

virtual void info() { //default constructor dengan nilai 0 (nol)

BasePoint::info();

cout << "property 1: p=" << p1 << endl;

}

};

class Square : public Figure1P { // pewarisan bersifat public dari class Figure1P

public:

Square(int px, int py, int r):Figure1P(px, py, r) {}; //constructor dengan default argument

virtual string type() { //default constructor dengan nilai 0 (nol)

return "square";

}

};

class Circle : public Figure1P { // pewarisan bersifat public dari class Figure1P

public:

Circle(int px, int py, int r):Figure1P(px, py, r) {} //constructor dengan default argument

virtual string type() { //default constructor dengan nilai 0 (nol)

return "circle";

}

};

class Figure2P : public Figure1P { // pewarisan bersifat public dari class Figure1P

public:

Figure2P(int px, int py, int w, int h):p2(h),Figure1P(px, py, w) {} //constructor dengan default argument

int p2;

virtual void info() { //default constructor dengan nilai 0 (nol)

Figure1P::info();

cout << "property 2: p=" << p2 << endl;

}

};

class Rectangle : public Figure2P { // pewarisan bersifat public dari class Figure2P

public:

Rectangle(int px, int py, int w, int h):Figure2P(px, py, w, h) {} //constructor dengan default argument

virtual string type() { //default constructor dengan nilai 0 (nol)

return "rectangle";

}

};

class Oval : public Figure2P {

public:

Oval(int px, int py, int w, int h):Figure2P(px, py, w, h) {}; //constructor dengan default argument

virtual string type() { //default constructor dengan nilai 0 (nol)

return "oval";

}

};

void main(void) {

BasePoint **objs = new BasePoint*[5];

// allocate space for 10 BasePoint pointers,

// they may be used to store derived classes

// creating objects

objs[0] = new Circle(7, 6, 55);

objs[1] = new Rectangle(12, 54, 21, 14);

objs[2] = new Square(19, 32, 10);

objs[3] = new Oval(43, 10, 4, 3);

objs[4] = new Square(3, 41, 3);

bool flag=false;

do {

cout<< endl << "We have 5 objects with numbers 0..4" << endl;

cout<<"Enter object number to view information about it " << endl;

cout<< "Enter any other number to quit " << endl;

char onum; // in fact, this is a character, not a number

// this allows user to enter letter and quit...

cin >> onum;

// flag -- user have entered number 0..4

flag = ((onum >= '0')&&(onum <= '4'));

if (flag)

objs[onum-'0']->info();

} while(flag);

// freeing memory

for(int i=0;i<5;i++)

delete objs[i];

delete [] objs;

}

Tidak ada komentar:

Posting Komentar