الجمعة، 22 نوفمبر 2013
8:22 ص

Shape: two dimensional and three dimensional


#include "stdafx.h"
#include<iostream>
#include<conio.h>

using namespace std;
class shape
{
public:
virtual void print(){cout<<"Area: "<<area();}
virtual float area(){return 0;}
};
class two_Dim : public shape
{
public:
void print(){cout<<"Area: "<<area()<<endl<<"Perimiter: "<<perimiter();}
    float area(){return 0;}
virtual float perimiter(){return 0;}
};
class three_Dim : public shape
{
void print(){cout<<"Area: "<<area()<<endl<<"Volume: "<<volume();}
    float area(){return 0;}
virtual float volume(){return 0;}
};
class rectangle : public two_Dim
{
private:
float length;
float width;
public:
rectangle(float x,float y){length=x;width=y;}
void print(){cout<<"length: "<<length<<endl<<"width: "<<width<<endl<<"Area: "<<area()<<endl<<"Perimiter: "<<perimiter()<<endl;}
float area(){return length*width;}
float perimiter(){return 2*(length*width);}
};
class triangle : public two_Dim
{
private:
float base;
float height;
float x1,x3;
public:
triangle(float b,float h,float n1,float n3){base=b;height=h;x1=n1;x3=n3;}
void print(){cout<<"base: "<<base<<endl<<"height: "<<height<<endl<<"First Line: "<<x1<<endl<<"Third Line: "<<x3<<endl<<"Area: "<<area()<<endl<<"Perimiter: "<<perimiter()<<endl;}
float area(){return 0.5*base*height;}
float perimiter(){return x1+x3+base;}
};
class circle : public two_Dim
{
private:
float radius;
public:
circle(float r){radius=r;}
void print(){cout<<"Radius: "<<radius<<endl<<"Area: "<<area()<<endl<<"Perimiter: "<<perimiter()<<endl;}
float area(){return 3.14*radius*radius;}
float perimiter(){return 2*3.14*radius;}
};
class box : public three_Dim
{
private:
float l;
public:
box(float m){l=m;}
void print(){cout<<"Line: "<<l<<endl<<"Area: "<<area()<<endl<<"Volume: "<<volume()<<endl;}
float area(){return 6*l*l;}
float volume(){return l*l*l;}
};
class cylinder : public three_Dim
{
private:
float radius;
float height;
public:
cylinder(float r,float h){radius=r;height=h;}
void print(){cout<<"Radius: "<<radius<<endl<<"Height: "<<height<<endl<<"Area: "<<area()<<endl<<"Volume: "<<volume()<<endl;}
float area(){return (2*3.14*radius*radius)+(2*3.14*radius*height);}
float volume(){return 3.14*radius*radius*height;}
};
void main()
{
shape sh;
two_Dim t2;
three_Dim t3;
rectangle rc(2,3);
triangle tr(2,4,1,3);
circle cr(2);
box bx(2);
cylinder cl(2,3);
shape *ptr=&sh;
cout<<"Shape:\n";
ptr ->print();
cout<<"\n\nTwo Dim:\n";
ptr=&t2;
ptr ->print();
cout<<"\n\nThree Dim:\n";
ptr=&t3;
ptr ->print();
cout<<"\n\nRectangle:\n";
ptr=&rc;
ptr ->print();
cout<<"\n\nTriangle:\n";
ptr=&tr;
ptr ->print();
cout<<"\n\nCircle:\n";
ptr=&cr;
ptr ->print();
cout<<"\n\nBox:\n";
ptr=&bx;
ptr ->print();
cout<<"\n\nCylinder:\n";
ptr=&cl;
ptr ->print();

_getch();
}

0 التعليقات:

إرسال تعليق