Popular Posts

6/24/2012

Sample Cylinder C++ Code



You can see the following code :


//Sample Cylinder C++ ...!
//The following code
//Royal University of Phnom Penh
#include<iostream.h>
#include<math.h>
#include<string.h>
class Solid {
public:
enum type{CY,CU,PI};
virtual void output()=0;
//virtual void input()=0;
virtual float volume()=0;
virtual type objType()=0;
};
class Cylinder:public Solid
{
private:
float r,h;
public:
Cylinder(float r1=0,float h1=0){r=r1;h=h1;}
virtual void output(){cout<<"r:"<<r<<"\t"<<"h:"<<h<<endl; }
//virtual void input();
virtual float volume(){return 3.14*r*r*h;}
virtual type objType(){ return Solid::CY; }
};
class Cube:public Solid
{
private: float l,w,h;
public: Cube(float l1=0,float w1=0,float h1=0){l=l1;w=w1;h=h1;}
virtual void output(){cout<<"l:"<<l<<"\t"<<"w:"<<"\t"<<w<<"h:"<<h<<endl;}
//virtual void input();
virtual float volume(){return l*w*h;}
virtual type objType(){ return Solid::CU; }
};
class Pyramid:public Solid{
private: float s1,s2,s3,h;
public:
Pyramid(float ss1=0,float ss2=0,float ss3=0,float h1=0){s1=ss1;s2=ss2;s3=ss3;h=h1;}
virtual void output(){cout<<"s1:"<<s1<<"\t"<<"s2"<<s2<<"\t"<<"s3"<<s3<<"\t"<<"h:"<<h<<endl; }
virtual float volume(){ float p=0,s=0,v=0;
p=(s1+s2+s3)/2;
s=sqrt((p*(p-s1)*(p-s2)*(p-s3)));
v=s*h/3;
return v;
}
virtual type objType(){ return Solid::PI; }
};
void main(){
Solid *p[6]={new Cylinder (25,12),new Cube(10,15,20),new Pyramid(12,13,15,20),new Cylinder(12,32),new Cube(20,20,12),new Pyramid(21,40,21,12)};
cout<<"All solid:"<<endl;
for(int i=0;i<6;i++)
 p[i]->output();
cout<<"Cylinder only:"<<endl;
float s=0;
for(i=0;i<6;i++)
if(p[i]->objType()==Solid::CY){
p[i]->output();
s=s+p[i]->volume();
}
cout<<"Total volume of Cylinder:"<<s<<endl;
cout<<"Cube only:"<<endl;
s=0;
for(i=0;i<6;i++)
if(p[i]->objType()==Solid::CU){
p[i]->output();
s=s+p[i]->volume();
}
cout<<"Total volume of Cube:"<<s<<endl;
cout<<"Pyramid only:"<<endl;
s=0;
for(i=0;i<6;i++)
if(p[i]->objType()==Solid::PI){
p[i]->output();
s=s+p[i]->volume();
}
cout<<"Total volume of Pyramid:"<<s<<endl;
}

0 comments:

Post a Comment