Monday, February 23, 2015

Write a C++ program to calculate area of circle,sphere and cylinder using base class round-shape

Create a base class round-shape(radius). define three different
shapes as circle, sphere and cylinder(height) from round-shape.
class round-shape
{
protected :float radius;
public:static float pi;
round-shape(float)//default argument
virtual float area()=0;
}
Write a C++ program to calculate area of circle,sphere and cylinder



#include<iostream.h>
#include<conio.h>
class round
{
protected:float radius;
public:static float pi;
       round(float r=0)
       {
       cout<<"\n Enter the radius:-";
       cin>>r;
       radius=r;
       }
       virtual float area()=0;
       };
class circle:public round
{
public:float circle;
       float area()
       {
       circle=pi*radius*radius;
       cout<<"\n Area of circle:-"<<circle;
       return 0;
       }
       };
class sphere:public round
{
public:float sph;
       float area()
       {
       sph=4*pi*radius*radius;
       cout<<"\n Area of sphere:-"<<sph;
       return 0;
       }
};
class cylinder:public round
{
public:float ac;
       float h;
public:cylinder(float height=0)
       {
       cout<<"\n Enter height:-";
       cin>>height;
       h=height;
       }
       float area()
       {
       float tsac,csac,vc,acbc;
       tsac=2*pi*radius*(h+radius);
       csac=2*pi*radius*h;
       vc=pi*radius*radius*h;
       acbc=pi*radius*radius;
       ac=tsac+csac+vc+acbc;
       cout<<"Area of cylinder is:-"<<ac;
       return 0;
       }
};
float round::pi=3.14;
int main()
{
int height;
clrscr();
cout<<"\n\t\t To calculate area of circle\n\n";
circle c;
c.area();
cout<<"\n\t\t To calculate area of sphere\n\n";
sphere s;
s.area();
cout<<"\n\t\t To calculate area of cylinder\n\n";
cylinder cl;
cl.area();
getch();
return 0;
}






============================================
OUTPUT

                To calculate area of circle                                    
                                                                               
                                                                               
Enter the radius:-1                                                           
                                                                               
Area of circle:-3.14         
                                                 
                To calculate area of sphere                                    
                                                                               
                                                                               
Enter the radius:-1                                                           
                                                                               
Area of sphere:-12.56  
                                                       
                To calculate area of cylinder                                  
                                                                               
                                                                               
Enter the radius:-1                                                           
                                                                               
Enter height:-1
Area of cylinder is:-25.120001

Write a cpp program for car passing through toll booth

Write a cpp (C++) program for car passing through toll booth








Imagine a tollbooth at a bridge. a car passing by the booth
is expected to pay a toll. the tollbooth keeps the track of the number
car that gone and total cash amount collected.
create a class tollbooth with the data member
-total number of cars passed
-total toll collected
write necessary member function
1.a constructor that initializes both data member to zero.
2.paying ca(): when any cars passes through the tollbooth. that much
toll gets added into total toll collected and total number of cars passed is
incremented by one
3. nonpaying car(); increment the car total but adds nothing to cash total
4.display(); the total number car passed and total cash collected


#include<iostream.h>
#include<conio.h>
class toolbath
{
int noc,tot;
public:tollbooth(int n=0,int t=0)
       {
       noc=n;
       tot=t;
       }
       void pay()
       {
       int i;
       noc++;
       cout<<"\n Enter the amount to pay toll:-";
       cin>>i;
       tot=tot+i;
       }
       void non_pay()
       {
       noc++;
       }
       void display()
       {
       cout<<"\n Total number of car passed:-"<<noc;
       cout<<"\n Total toll collected:-"<<tot;
       }
};
int main()
{
int n,t,ans;
char ch='y';
clrscr();
cout<<"\n Enter total number of car passed:-";
cin>>n;
cout<<"\n Enter total toll collected:-";
cin>>t;
tollbooth b(n,t);
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter the car type:-";
cout<<"\n Press 1: for paying car";
cout<<"\n Press 2: for non-paying car";
cin>>ans;
if(ans==1)
b.pay();
if(ans==2)
b.non_pay();
cout<<"\n Do you want to continue(Y|N):-";
cin>>ch;
}
b.display();
getch();
return 0;
}

=========================================================

OUTPUT


Enter total number of car passed:-123                                          
                                                                               
Enter total toll collected:-7478                                               
                                                                               
Enter the car type:-                                                           
Press 1: for paying car                                                        
Press 2: for non-paying car1                                                   
                                                                               
Enter the amount to pay toll:-12                                               
                                                                               
Do you want to continue(Y|N):-y                                                
                                                                               
Enter the car type:-                                                           
Press 1: for paying car                                                        
Press 2: for non-paying car1                                                   
                                                                               
Enter the amount to pay toll:-14                                               
                                                                               
Do you want to continue(Y|N):-y                                                

Enter the car type:-
Press 1: for paying car
Press 2: for non-paying car2

Do you want to continue(Y|N):-n

Total number of car passed:-126
Total toll collected:-7504

Featured posts

सौंफ के फायदे

 सौंफ त्रिदोषनाशक है, इसकी तासीर ठंडी है, पर यह जठराग्नि को मंद नहीं करती।            आंखों की रोशनी सौंफ का सेवन करके बढ़ाया जा सकता है। सौ...

Popular posts