google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.

Thursday, February 26, 2015

Write a c++ program using class to read the name of user and number of units consumed and printout charges with name(Electricity)

An electricity board charges the following rates to users
-for first 100 units:40p per unit
-for next 200 units: 50p per unit
-for beyond 300 units :60p per unit
all user are charged a minimum of Rs.150. if the total cost is more
than Rs.250 then an additional charges of 15% are added.
write a c++ program using class to read the name of user and number
of units consumed and printout charges with names.(use array of object)



#include<conio.h>
#include<iostream.h>
class elec
{
char name[20];
int nou;
float charge;
public:void accept()
       {
       cout<<"\n enter name:-";
       cin>>name;
       cout<<"\n enter number of unit:-";
       cin>>nou;
       }
       void calc()
       {
       if(nou<100)
       charge=nou*0.4;
       else
       if(nou<=300)
       {
       int temp=nou-100;
       charge=100*0.4;
       charge=(float)charge+(temp*0.5);
       }
       else
       if(nou>300)
       charge=nou*0.6;
       if(charge<150)
       charge=150;
       else
       if(charge>250)
       charge=(float)charge+(charge*0.15);
       }
       void display()
       {
       cout<<"\n name="<<name;
       cout<<"\n charges="<<charge;
       }
};
int main()
{
elec e[3];
clrscr();
for(int i=0;i<3;i++)
{
e[i].accept();
e[i].calc();
}
for(i=0;i<3;i++)
e[i].display();
getch();
return 0;
}

=============================================
OUTPUT:-

enter name:-XYZ                                                             
                                                                               
enter number of unit:-122                                                      
                                                                               
enter name:-ABC                                                                
                                                                               
enter number of unit:-302                                                      
                                                                               
enter name:-PQR                                                             
                                                                               
enter number of unit:-1000                                                     
                                                                               
name=XYZ                                                                    
charges=150                                                                    
name=ABC                                                                      
charges=181.199997                                                             
name=PQR
charges=690

Write a C++menu driven program to show class hierarchy.

Consider the following class hierarchy.
    Create a base class Employee(empcode, empname). Derive the classes Manager(designation, clubdues), Scientist(deptname, publications) and Labourer from
Employee class. Write a C++ menu driven program
1.    to accept  the details of ‘n’ employees
2.    to display the information
3.    to display all the scientist from “Chemistry Department”.


#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
char ans[20];
int ch,n,a=0,b=0,i,cnt;
class employee
{
protected:int empcode;
       char empname[20];
};
class manager:protected employee
{
protected:char desg[20];
      int dues;
public:void accept1();
       void display1();
}m[10];
void manager::accept1()
{
cout<<"\n enter employee code:-";
cin>>empcode;
cout<<"\n enter employee name:-";
cin>>empname;
cout<<"\n enter designation:-";
cin>>desg;
cout<<"\n enter club-dues:-";
cin>>dues;
}
void manager::display1()
{
cout<<empcode<<"\t"
<<empname<<"\t"
<<desg<<"\t"
<<dues<<"\n";
}
class scientist:protected employee
{
protected:char dept[20],pub[20];
public:void accept2();
       void display2();
       void search(char name[20]);
}s[10];
void scientist::accept2()
{
cout<<"\n enter employee code:-";
cin>>empcode;
cout<<"\n enter employee name:-";
cin>>empname;
cout<<"\n enter department name:-";
cin>>dept;
cout<<"\n enter the publication:-";
cin>>pub;
}
void scientist::display2()
{
cout<<empcode<<"\t"
<<empname<<"\t"
<<dept<<"\t"
<<pub<<"\n";
}
void scientist::search(char name[20])
{
int i,cnt;
for(i=0;i<b;i++)
      {
       cnt=strcmp(name,ans);
       if(cnt==0)
       cout<<"\n scientist is chemistry department is:-"<<s[i].empname;
       }
}
int main()
{
int temp;
clrscr();
while(1)
{
cout<<"\n 1:accept the details";
cout<<"\n 2:display the information";
cout<<"\n 3:to display all the scientist of chemistry department";
cout<<"\n 4:exit";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n enter how many employee:-";
       cin>>n;
       for(i=0;i<n;i++)
       {
       cout<<"\n whose information you want to enter";
       cout<<"\n press 1:for manager";
       cout<<"\n press 2:for scientist";
       cin>>temp;
       if(temp==1)
       m[a++].accept1();
       else
       s[b++].accept2();
       }
       break;
case 2:for(i=0;i<a;i++)
       m[i].display1();
       for(i=0;i<b;i++)
       s[i].display2();
       break;
case 3:strcpy(ans,"chemistry");
       s[i].search(ans);
       break;
case 4:exit(0);
default:cout<<"\n you entered wrong choice";
       }
}
getch();
return 0;
}


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

enter your choice1

enter how many employee:-2

whose information you want to enter
press 1:for manager                                                            
press 2:for scientist                                                          
1                                                                              

enter employee code:-112                                                             
                                                                               
enter employee name:-XYZ                                                            
                                                                               
enter designation:-ABC                                                        
                                                                               
enter club-dues:-75                                                             
                                                                               
whose information you want to enter                                            
press 1:for manager                                                            
press 2:for scientist                                                          
2
                                                                               
enter employee code:-120.                                                            
                                                                               
enter employee name:-                                                                
enter department name:-chemistry                                                
                                                                               
enter the publication:-pqr                                                     

1:accept the details                                                           
2:display the information                                                      
3:to display all the scientist of chemistry department                         
4:exit                                                                         
enter your choice2                                                             
112     XYZ     ABC      75                                                     
120     .       chemistry        pqr                                            
                                                                               
1:accept the details                                                           
2:display the information                                                      
3:to display all the scientist of chemistry department                         
4:exit
enter your choice3

scientist is chemistry department is:-.
1:accept the details
2:display the information
3:to display all the scientist of chemistry department
4:exit
enter your choice4

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...