25.08.2017 Object-Oriented Programming Lab 7 1. Shape, TwoDShape, ThreeDShape, Disk, Square, Sphere, Cube class hiyerarşisi. Disk, kare, küre ve küp şekilleriyle çalışmak için abstract class larla birlikte bir class hiyerarşisi oluşturun. Shape - abstract class. - Türetilmiş class ların print(), scan(), area(), volume() metotlarına arayüz sağlar. TwoDShape - Shape class ından türetilmiş abstract class. - uble x // şeklin x koordinatı. - uble y // şeklin y koordinatı. ThreeDShape - TwoDShape class ından türetilmiş abstract class. - uble z // şeklin z koordinatı. Disk - TwoDShape class ından türetilmiş class. - uble r // diskin yarıçapı. - print(), scan(), area(), volume(). // diske göre tasarlanmış metotlar. Square - TwoDShape class ından türetilmiş class. - uble a // karenin kenar uzunluğu. - print(), scan(), area(), volume(). // kareye göre tasarlanmış metotlar. Sphere - ThreeDShape class ından türetilmiş class. - uble r // kürenin yarıçapı. - print(), scan(), area(), volume(). // küreye göre tasarlanmış metotlar. Cube - uble a // karenin kenar uzunluğu. - print(), scan(), area(), volume(). // kübe göre tasarlanmış metotlar. Oluşturduğunuz class ları bir programda deneyin: Her class tan birer obje oluşturun, kullanıcıdan koordinat ve boyut bilgilerini alın ve her bir objenin tüm bilgilerini (objenin şekli, merkez koordinatları, boyutları, alanı, hacmi) ekrana yazdırın.
#ifndef SHAPE_H #define SHAPE_H #define PI 3.14159 class shape virtual void print(void) = 0; virtual void scan(void) = 0; virtual uble area(void) = 0; virtual uble volume(void) = 0; ; #endif // SHAPE_H #ifndef TWO_D_SHAPE_H #define TWO_D_SHAPE_H #include "shape.h" class twodshape : public shape uble x; uble y; twodshape(uble X=0.0, uble Y=0.0); ; #endif // TWO_D_SHAPE_H #ifndef TWO_D_SHAPE_H #define TWO_D_SHAPE_H #include "shape.h" class twodshape : public shape uble x; uble y; twodshape(uble X=0.0, uble Y=0.0); ; #endif // TWO_D_SHAPE_H twodshape::twodshape(uble X, uble Y) x = X; y = Y; #ifndef THREE_D_SHAPE_H #define THREE_D_SHAPE_H class threedshape : public twodshape uble z;
; threedshape(uble X=0.0, uble Y=0.0, uble Z=0.0); #endif // THREE_D_SHAPE_H #include "threedshape.h" threedshape::threedshape(uble X, uble Y, uble Z) : twodshape(x, Y) z = Z; #ifndef DISK_H #define DISK_H class disk : public twodshape uble r; disk(uble X=0.0, uble Y=0.0, uble R=1.0); uble area(void); uble volume(void); ; #endif // DISK_H #include "disk.h" disk::disk(uble X, uble Y, uble R) : twodshape(x, Y) r = R > 0? R : 1.0; void disk::print(void) cout << "shape : disk" << endl << "(x,y) : (" << x << "," << y << ")" << endl << "volume: " << volume() << endl; void disk::scan(void) cout << "shape : disk" << endl cout << "enter r: "; cin >> r; while(r <= 0.0); // 0'dan buyuk bir yaricap degeri girilene kadar uble disk::area(void)
return PI*r*r; uble disk::volume(void) return 0; #ifndef SQUARE_H #define SQUARE_H class square : public twodshape uble a; square(uble X=0.0, uble Y=0.0, uble A=1.0); uble area(void); uble volume(void); ; #endif // SQUARE_H #include "square.h" square::square(uble X, uble Y, uble A) : twodshape(x, Y) a = A > 0? A : 1.0; void square::print(void) cout << "shape : square" << endl << "(x,y) : (" << x << "," << y << ")" << endl << "volume: " << volume() << endl; void square::scan(void) cout << "shape : square" << endl cout << "enter a: "; cin >> a; while(a <= 0.0); // 0'dan buyuk bir kenar degeri girilene kadar uble square::area(void) return a*a;
uble square::volume(void) return 0; #ifndef SPHERE_H #define SPHERE_H #include "threedshape.h" class sphere : public threedshape uble r; sphere(uble X=0.0, uble Y=0.0, uble Z=0.0, uble R=1.0); uble area(void); uble volume(void); ; #endif // SPHERE_H #include "sphere.h" sphere::sphere(uble X, uble Y, uble Z, uble R) : threedshape(x,y,z) r = R > 0? R : 1.0; void sphere::print(void) cout << "shape : sphere" << endl << "(x,y,z) : (" << x << "," << y << "," << z << ")" << endl << "volume : " << volume() << endl; void sphere::scan(void) cout << "shape : sphere" << endl cout << "enter z: "; cin >> z; cout << "enter r: "; cin >> r; while(r <= 0.0); // 0'dan buyuk bir kenar degeri girilene kadar uble sphere::area(void) return 4*PI*r*r; uble sphere::volume(void)
return 4.0/3.0*PI*r*r*r; // integer bolmesi hatasina dusme! #ifndef CUBE_H #define CUBE_H #include "threedshape.h" class cube : public threedshape uble a; cube(uble X=0.0, uble Y=0.0, uble Z=0.0, uble A=1.0); uble area(void); uble volume(void); ; #endif // CUBE_H #include "cube.h" cube::cube(uble X, uble Y, uble Z, uble A) : threedshape(x,y,z) a = A > 0.0? A : 1.0; void cube::print(void) cout << "shape : cube" << endl << "(x,y,z) : (" << x << "," << y << "," << z << ")" << endl << "volume : " << volume() << endl; void cube::scan(void) cout << "shape : cube" << endl cout << "enter z: "; cin >> z; cout << "enter a: "; cin >> a; while(a <= 0.0); // 0'dan buyuk bir kenar degeri girilene kadar uble cube::area(void) return 6*a*a; uble cube::volume(void)
return a*a*a; #include "shape.h" #include "disk.h" #include "square.h" #include "sphere.h" #include "cube.h" int main(void) disk Disk_Shape; square Square_Shape; sphere Sphere_Shape; cube Cube_Shape; shape *Shape_Arr[] = &Disk_Shape, &Square_Shape, &Sphere_Shape, &Cube_Shape; int lpr; for(lpr=0; lpr<4; lpr++) Shape_Arr[lpr]->scan(); cout << endl; cout << "--------------------" << endl << endl; for(lpr=0; lpr<4; lpr++) Shape_Arr[lpr]->print(); cout << endl; return 0;