Thursday, 3 January 2019

CPP-Slip9

Slip9
Q.1)     Write a C++ program to create a class Person which contains data members as P_Name, P_City, P_Contact_Number.  Write member functions to accept and display five Persons information. Design User defined Manipulator to print P_Contact_Number. (For Contact Number set right justification, maximum width to 10 and fill remaining spaces with ‘*’)


#include<iostream.h>
#include<conio.h>
//#include<manip.h>
class person
{
                char p_name[20],p_city[10];
                long int p_c_no;
                public:
                void getdata()
                {
                                cout<<"\n enter person name\t";
                                cin>>p_name;
                                cout<<"\n enter persons city\t";
                                cin>>p_city;
                                cout<<"enter person contact no\t";
                                cin>>p_c_no;
                }


                void display()
                {
                                cout<<"\n\n*************OUTPUT******************\n";
                                cout<<"\nperson name =\t"<<p_name;
                                cout<<"\nperson city =\t"<<p_city;
                                //cout<<"\npersons contact number is\t"<<p_c_no;
                                cout.width(10);
                                cout.fill('*');
                                cout<<p_c_no;
                                //cout<<cout.setw(10)<<p_c_no;
                }
};
void main()
{
                int i;
                clrscr();
                person p[5];
                for(i=0;i<1;i++)
                {
                                p[i].getdata();
                }
                for(i=0;i<1;i++)
                {
                                p[i].display();
                }
                getch();
}
                                                                                                                                        

Q.2)     Create  two base classes Learning_Info( Roll_No, Stud_Name, Class, Percentage) and Earning_Info(No_of_hours_worked, Charges_per_hour). Derive a class Earn_Learn_info from above two classes. Write necessary member functions to accept and display Student information. Calculate total money earned by the student. (Use constructor in derived class) 

#include<iostream.h>
#include<conio.h>
class learning
{
public :
int roll_no;
char name[30],cclass[30];
float percenatge;

void getlearning()
{
cout<<"\nenter roll number\t";
cin>>roll_no;
cout<<"\nenter student name\t";
cin>>name;
cout<<"\nenter class\t";
cin>>cclass;
cout<<"\nenter percentage\t";
cin>>percenatge;
}
void putlearning()
{
cout<<"\n roll number is \t"<<roll_no;
cout<<"\n student name is \t"<<name;
cout<<"\n class is\t"<<cclass;
cout<<"\n percentage is\t"<<percenatge;
}

};
class earning
{
public :
int charge,no_of_hour;

void getearning()
{
cout<<"\nenter no of hour worked\t";
cin>>no_of_hour;
cout<<"\nenter charges\t ";
cin>>charge;
}
void putearning()
{
cout<<"\ no of hour worked\t"<<no_of_hour;
cout<<"\n charges is \t "<<charge;
}
};

class earnlearn: public learning, public earning
{
public:
float money;

earnlearn() {}
void tmoney()
{
money=no_of_hour*charge;
cout<<"\n total earing money is \t"<<money;
}

};
void main()
{
clrscr();
 earnlearn ec;


ec.getlearning();
ec.getearning();
cout<<"\n********learning detail ******\n";
ec.putlearning();
cout<<"\n********earning detail is***********\n";
ec.putearning();
ec.tmoney();
getch();
}


 Another way












#include<conio.h>
 #include<iostream.h>
#include<string.h>
class learning_info
{
   public:
   int rno;
   char name[20],cls[10];
   float per;
   learning_info(int roll,char sname[],char cl[],float perc)
   {
      rno=roll;
      strcpy(name,sname);
      strcpy(cls,cl);
      per=perc;
   }
   void display()
   {
      cout<<"\n Roll no = "<<rno<<"\n Name="<<name<<"\n class = "<<cls;
      cout<<"\n Percentage = "<<per;
   }
};

class earning_info
{
    public:
   int hrs,charges;
   earning_info(int hr,int chrgs)
   {
     hrs=hr;
     charges=chrgs;
   }
   void display()
   {
     cout<<"\n Hours = "<<hrs;
     cout<<"\t charges = "<<charges;
   }
};

class earn_learn_info:public learning_info,public earning_info
{
   public:
   earn_learn_info(int roll,char sname[],char cl[],float perc,int hr,int chrgs):learning_info(roll,sname,cl,perc),earning_info(hr,chrgs)
   {
   }
   void display()
   {
      learning_info::display();
      earning_info::display();
   }
   void calculate()
   {
                  int total;
                  total=charges*hrs;
                  cout<<"\n Money earned by student:= "<<total;
   }
};

void main()
{
    clrscr();
    int rno,ch,hr;
    float p;
    char nm[10],c[10];
    earn_learn_info ob1(1,"abc","sy",77,5,50);
    ob1.display();
    ob1.calculate();
    cout<<"\n Enter rno : ";
    cin>>rno;
    cout<<"\n enter name : ";
    cin>>nm;
    cout<<"\n Enter class : ";
    cin>>c;
    cout<<"\n Enter percentage : ";
    cin>>p;
    cout<<"\n Enter working hr : ";
    cin>>hr;
    cout<<"\n Enter charges : ";
    cin>>ch;
    earn_learn_info ob2(rno,nm,c,p,hr,ch);
    ob2.display();
    ob2.calculate();
    getch();
}