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

Write a C++ program using class to overload following binary operators

Write a C++ program using class to overload following binary operators (+,-,*,/).

#include<iostream.h>
#include<conio.h>
class number
{ int a;
public:void get();
       void show();
       number operator +(number);
       number operator -(number);
       number operator *(number);
       number operator /(number);
};
void number:: get()
{
 cout<<"\nenter value";
 cin>>a;
 }
 void number:: show()
 {
 cout<<a;
 }
 number number::operator+(number n2)
 {
 number t;
 t.a=a+n2.a;
 return t;
 }
 number number::operator-(number n2)
 {
 number t;
 t.a=a-n2.a;
 return t;
 }
 number number::operator*(number n2)
 {
 number t;
 t.a=a*n2.a;
 return t;
 }
 number number::operator/(number n2)
 {
 number t;
 t.a=a/n2.a;
 return t;
 }
 int main()
 {
 number n1,n2,n3;
 clrscr();
 n1.get();
 n2.get();
 cout<<"\n First num ";
 n1.show();
 cout<<"\n Second num ";
 n2.show();
 n3=n1+n2;
 cout<<"\n Addition is ";
 n3.show();
 n3=n1-n2;
 cout<<"\n Subtraction is ";
 n3.show();
 n3=n1*n2;
 cout<<"\n Multiplication is ";
 n3.show();
 n3=n1/n2;
 cout<<"\n Division is ";
 n3.show();
 getch();
 return 0;
 }







===================================================
                                                             OUTPUT
Enter value4                                                                   
                                                                               
Enter value2                                                                   
                                                                               
First num4                                                                     
Second num 2                                                                    
Addition is 6                                                                   
Subtraction is 2                                                                
Multiplication is 8                                                             
Division is 2

Write Cpp program to represent bank account using member function

Define a class to represent a bank account which include following member
data member:-
1)name
2)Account number
3)type of account
4)bal.amt
member function:
a.to assign initial value
b.to deposit an account
c.to withdraw an account
d.to display name,account number and balance

#include<conio.h>
#include<iostream.h>
#include<string.h>
int w_amt;
class bank
{
public:int acc_no,bal;
char type[30],name[30];
public:void assign()
       {
       strcpy(name,"Vijay");
       acc_no=111;
       strcpy(type,"saving");
       bal=12000;
       }
       void deposit()
       {
       int amt;
       cout<<"\n Enter the amount which you want to deposit:-";
       cin>>amt;
       bal=bal+amt;
       }
       void withdraw()
       {
       cout<<"\n Enter how much money you want to withdraw";
       cin>>w_amt;
       }
       void display()
       {
       cout<<"\n Name:-"<<name
       <<"\n Account number:-"<<acc_no
       <<"\n Type of account:-"<<type
       <<"\n Balance amount:-"<<bal;
       }
};
int main()
{
bank b;
clrscr();
b.assign();
b.deposit();
b.withdraw();
if(w_amt>12000)
{
cout<<"You do not have that much balance try again";
}
else
b.bal=b.bal-w_amt;
b.display();
getch();
return 0;
}


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

OUTPUT

Enter the amount which you want to deposit:-1200
Enter how much money you want to withdraw200
Name:-Vijay
Account number:-111
Type of account:-saving
Balance amount:-13000

Cpp program to create class telephone to search no name and city

Create a class telephone containing name,telephone number and city as
data members and write necessary member function for the following
-search the telephone number with given name.
-search the name with given telephone number
-search all customer in a given city.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class telephone
{
public:char name[30],city[30];
       int tno;
public:void get()
       {
       cout<<"enter name:-";
       cin>>name;
       cout<<"\nenter telephone number";
       cin>>tno;
       cout<<"\nenter city name:-";
       cin>>city;
       }
       void search(int tnum)
       {
       cout<<"\ntelephone number of given customer:-"<<tnum;
       }
       void search(char n[30])
       {
       cout<<"\ncustomer name of the given number"<<n;
       }
       void search(char ct[30],int tnum)
       {
       cout<<"\n"<<ct<<"  "<<tnum;
       }
};
int main()
{
int i,tno,check;
char name[30],city[30];
telephone t[3];
clrscr();
for(i=0;i<3;i++)
t[i].get();
cout<<"\nsearch the telephone number of the given name";
cout<<"\nenter customer name:-";
cin>>name;
for(i=0;i<3;i++)
{
check=strcmp(name,t[i].name);
if(check==0)
t[i].search(t[i].tno);
}
cout<<"\n\n\nsearch the name of the given telephone number";
cout<<"\nenter telephone no.:-";
cin>>tno;
for(i=0;i<3;i++)
{
if(tno==t[i].tno)
t[i].search(t[i].name);
}
cout<<"\n\n\nsearch all customer in a given city";
cout<<"\nenter city name:-";
cin>>city;
for(i=0;i<3;i++)
{
check=strcmp(city,t[i].city);
if(check==0)
t[i].search(t[i].name,t[i].tno);
}
getch();
return 0;
}

Saturday, February 21, 2015

C++ Program to find tax of person salary

Consider the following class person
class person
{
char name[20];
char addr[30];
float salary;
int property;
float tax;
public:\\method
};
calculate tax amount by checking salary and the property of the person
for salary<5000         tax rate=0
for salary>=5000||<=10000    tax rate=14% of salary
for salary>=10000        tax rate=16% of salary
in this tax amount add following amt depending on the size of area
in sqr.foot
for 100 sqr.foot        amt=0
for>1000||<5000            amt=1000
for>5000||<=10000        amt=3000


#include<conio.h>
#include<iostream.h>
class person
{
char name[20],addr[30];
float sal,tax_amt;
int prop;
public:void accept();
       void display();
};
void person::accept()
{
cout<<"\n Enter the name:-";
cin>>name;
cout<<"\n Enter address:-";
cin>>addr;
cout<<"\n Enter salary:-";
cin>>sal;
cout<<"\n Enter how much Rs. of property you have:-";
cin>>prop;
tax_amt=0;
if(sal<5000)
tax_amt=tax_amt+0;
if(sal>=5000&&sal<=10000)
tax_amt=tax_amt+(sal*0.14);
if(sal>=10000)
tax_amt=tax_amt+(sal*0.16);
if(prop==1000)
tax_amt=tax_amt+0;
if(prop>1000&&prop<5000)
tax_amt=tax_amt+1000;
if(prop>5000&&prop<=10000)
tax_amt=tax_amt+3000;
}
void person::display()
{
cout<<"\n Name:-"<<name;
cout<<"\n Address:-"<<addr;
cout<<"\n Salary:-"<<sal;
cout<<"\n Property:-"<<prop;
cout<<"\n Tax Amount:-"<<tax_amt;
}
int main()
{
person p;
clrscr();
p.accept();
p.display();
getch();
return 0;
}

=====================================================
OUTPUT :  -
Enter the name:-ABC
Enter address:-XYZ
Enter salary:-5000
Enter how much Rs. of property you have:-10000

Name:-ABC
Address:-XYZ
Salary:-5000
Property:-10000
Tax Amount:-3700

C++ program for medical shop store

C++ program for medical shop store 



Create a class medical shoppe containing

-medicine

-qty

-price

medicine details are stored into file medical.txt. when any medicine has to be sold it is first searched into file. if found the quantity is decremented that much quantity to be sold.


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
#include<stdlib.h>
#include<stdio.h>
class medical
{
public:char med[20];
       int qty,price;
public:void accept()
       {
       cout<<"\n enter the medicine name:-";
       cin>>med;
       cout<<"\n enter qty:-";
       cin>>qty;
       cout<<"enter price";
       cin>>price;
       }
       void display()
       {
       cout<<"\n medicine name:-"<<med;
       flushall();
       cout<<"\n quantity:-"<<qty;
       cout<<"\n price:-"<<price;
       }
       void search(char n[20],int q)
       {
       int ans=1;
       ans=strcmp(med,n);
       if (ans==0)
       qty=qty-q;
       }

};
int main()
{
medical m[3];
int i,q,ans;
char n[20];
clrscr();
fstream f1;
f1.open("a.doc",ios::trunc|ios::in|ios::out);
cout<<"\n\t\tenter the details\n";
for(i=0;i<3;i++)
{
m[i].accept();
f1.write((char *)&m[i],sizeof(m[i]));
}
f1.seekg(0);
cout<<"\n enter the medicine name which is to be sold:-";
cin>>n;
cout<<"\n how much quantity you want:-";
cin>>q;
for(i=0;i<3;i++)
{
f1.read((char *)&m[i],sizeof(m[i]));
m[i].search(n,q);
f1.write((char *)&m[i],sizeof(m[i]));
}
cout<<"\n\t\t display of all records in a file\n";
for(i=0;i<3;i++)
{
f1.read((char *)&m[i],sizeof(m[i]));
m[i].display();
}

getch();
return 0;
}


--------------------------------------------------------------------

OUTPUT



                enter the details                                              
                                                                               
enter the medicine name:-per                                                   
                                                                               
enter qty:-34                                                                  
enter price 56                                                                  
                                                                               
enter the medicine name:-12                                                    
                                                                               
enter qty:-76                                                                  
enter price 45                                                                  
                                                                               
enter the medicine name:-df                                                    
                                                                               
enter qty:-34                                                                  
enter price 12                                                                  
                                                                               
enter the medicine name which is to be sold:-df                                
                                                                               
how much quantity you want:-12
                                                                               
                display of all records in a file                               
                                                                               
medicine name:-per                                                             
quantity:-34                                                                   
price:-56                                                                      
medicine name:-12                                                              
quantity:-76                                                                   
price:-45                                                                      
medicine name:-df                                                              
quantity:-22
price:-12

C++ program to accept and dispaly data using class date

Create a c++ class date which contain:
-date
-month
-year
write necessary member function to accept and display a data using
<< and >> operator

#include<iostream.h>
#include<conio.h>
class date
{
int d,m,y;
public:void accept()
       {
       cout<<"\nenter date:-";
       cin>>d;
       cout<<"\nenter month:-";
       cin>>m;
       cout<<"\nenter year:-";
       cin>>y;
       }
       void display()
       {
       cout<<"\n\ndate:="<<d<<"-"<<m<<"-"<<y;
       }
};
int main()
{
date d;
clrscr();
d.accept();
d.display();
getch();
return 0;
}


-------------------------------------------------------------------------
OUTPUT


enter date:-21

enter month:-02

enter year:-2015


date:=21-02-2015

write a c++ program to read distance from user. store it in the file and display it to the user.

Create a class  distance containing feet and inches as data member.
write a c++ program to read distance from user. store it in the file
 and display it to the user.
 use operator overloading for the following
 1.<<to write distance object in inches format to a file
 2.>>to read inches from file.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class distance
{
   int feet;
   int inches;

 public:
 friend void operator <<(fstream &,distance &);
 friend void operator >>(fstream &,distance &);

};
void operator <<(fstream &fout,distance &d)
{
cout<<"\nEnter distance : ";
cout<<"\n\nEnter feet : ";
cin>>d.feet;
cout<<"\n\nEnter inches : ";
cin>>d.inches;
int finches=(d.feet*12)+d.inches;
fout<<finches<<"\n";
}
void operator >>(fstream &fin,distance &d)
{
int finches;
fin>>finches;
d.inches=finches%12;
d.feet=finches/12;
cout<<"\nDistance is : ";
cout<<d.feet<<" feet ";
cout<<d.inches<<" inches ";
}
int main()
{
clrscr();
fstream file;
distance d;
int n;
file.open("distance.dat",ios::out);

file.seekg(0);
cout<<"\nEnter how many distance you want to store";
cin>>n;
for(int i=0;i<n;i++)
{
file<<d;
}
file.close();
file.open("distance.dat",ios::in);
file.seekg(0);
for (i=0;i<n;i++)
{
file>>d;
}
getch();
return 0;
}

--------------------------------------------------------------------------------------
Output :-
Enter how many distance you want to store1                                     
                                                                               
Enter distance :                                                               
                                                                               
Enter feet : 2                                                                 
                                                                               
                                                                               
Enter inches : 3                                                               
                                                                               
Distance is : 2 feet 3 inches 

C++ program to find percentage of student using class student

 Write a C++ program to create a class Student which contains data members as Roll_Number, Stud_Name, Percentage.  Write member functions to accept Student information.  Display all details of student along with a class obtained depending on percentage. (Use array of objects)

#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
    int rollno;
    char name[10];
    int no_of_sub;
    int marks_of_each_sub[10];
    float per,m1;
    public:
    student()
    {}
    student(int rno,char nm[],int no_sub)
    {
        rollno=rno;
        strcpy(name,nm);
        no_of_sub=no_sub;
        if(no_of_sub>0)
        {
        for(int i=0;i<no_of_sub;i++)
        {
        cout<<"enter the marks of subject:->";
        cin>>marks_of_each_sub[i];
        m1=m1+marks_of_each_sub[i];
        per=m1/no_of_sub;
        }
        }

    }
      void show()
      {
        cout<<"\n\t"<<rollno<<"\t"<<name<<"\t"<<no_of_sub<<"\t\t"<<per<<"\t";
        for(int i=0;i<no_of_sub;i++)
        {
        cout<<marks_of_each_sub[i];
        cout<<"\n\t\t\t\t\t\t";
        }
      }

};

void main()
{
   clrscr();
   student x[10];
   int rno,i,no;
   char nm[10];
   int no_sub;
   cout<<"\n\thow many no u want?";
   cin>>no;
   for(i=0;i<no;i++)
   {
    cout<<"\nenter the rno:->";
    cin>>rno;
    cout<<"\nenter the name:->";
    cin>>nm;
    cout<<"\nenter the no of subject:->";
    cin>>no_sub;
    x[i]=student( rno,nm,no_sub);
   }
   cout<<"\n\trollno"<<"\tname"<<"\tno_of_sub"<<"\tper"<<"\tmarks_of_each_sub";
   for(i=0;i<no;i++)
   {
    x[i].show();
   }
   getch();
}

Featured posts

Happy Independence Day August 15th

Happy Independence Day August 15th  Here's a message for India's Independence Day (August 15th): "शुभ स्वतंत्रता दिवस! आजादी की...

Popular posts