C++ Programming
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 ‘ *’ ) [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
ostream & star(ostream & str)
{
cout<<"*";
return str;
}
class person
{
private:
char p_name[11];
char p_city[11];
char p_contact_number[10];
public:
void getdata();
void disp();
} ;
void person :: getdata()
{
cout<<"\n Enter name:";
fflush(stdin);
cin>>p_name;
cout<<"\n Enter city:";
fflush(stdin);
cin>>p_city;
cout<<"\n Enter contact:";
fflush(stdin);
cin>>p_contact_number;
}
void person :: disp()
{
cout<<"\n Name:"<<p_name;
cout<<"\n City:"<<p_city;
if ( strlen(p_contact_number) == 10 )
{
cout<<"\n Contact number:"<<setw(10)<<p_contact_number;
}
else if (strlen(p_contact_number) < 10)
{
int a;
a= 10-strlen(p_contact_number);
cout<<"\n Contact number:"<<setw(10);
for(int i=0;i<a;i++)
{
cout<<star;
}
cout<<p_contact_number;
}
}
int main()
{
clrscr();
person p[5];
for(int i=0;i<5;i++)
{
p[i].getdata();
p[i].disp();
}
getch();
return 0;
}
Output
Name:Ravi
City:Lature
Enter contact:8087482248
Name:Ravi
City:Lature
Contact number:8087482248
Enter name:Akshay
Enter city:Pune
Enter contact:9860449276
Name:Akshay
City:Pune
Contact number:9860449276
Enter name:Girish
Enter city:Mumbai
Enter contact:9527280773
Name:Girish
City:mumbai
Contact number:9527280773
Enter name:abhi
Enter city:banglore
Enter contact:9527329765
Name:abhi
City:banglore
Contact number:9527329765
Enter name:sagar
Enter city:delhli
Enter contact:7709728806
Name:sagar
City:delhli
Contact number:7709728806
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) [Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
class learning_info
{
protected:
int roll_no;
char name[20];
char clas[10];
float percentage;
public:
void accept_l()
{
cout<<"Enter student Roll no : ";
cin>>roll_no;
cout<<"Enter studnt Name : ";
cin>>name;
cout<<"Enter class : ";
cin>>clas;
cout<<"Enter percentage : ";
cin>>percentage;
}
void display_l()
{
cout<<"-------------------------------"<<endl;
cout<<"Roll No : " <<roll_no<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Class : "<<clas<<endl;
cout<<"Percentage : "<<percentage<<endl;
cout<<"-------------------------------"<<endl;
}
};
class earning_info
{
protected:
int no_of_hours_worked;
int charges_per_hour;
public:
void accept_e()
{
cout<<"No of hours Worked : ";
cin>>no_of_hours_worked;
cout<<"Charges for per hour : ";
cin>>charges_per_hour;
}
void display_e()
{
cout<<"No of hours Worked : "<<no_of_hours_worked<<endl;
cout<<"Charges for per hour : "<<charges_per_hour<<endl;
cout<<"-------------------------------"<<endl;
}
};
class earn_learn_info : public learning_info, public earning_info
{
int total;
public:
earn_learn_info()
{
total=0;
}
void calculate()
{
total=no_of_hours_worked * charges_per_hour;
cout<<"Total earn money by student : "<<total<<endl;
cout<<"-------------------------------"<<endl;
cout<<"*******************************"<<endl;
}
};
int main()
{ clrscr();
earn_learn_info el;
el.accept_l();
el.accept_e();
el.display_l();
el.display_e();
el.calculate();
getch();
return 0;
}
Output
Enter student Roll no : 22
Enter studnt Name : abc
Enter class : SY BCA
Enter percentage : No of hours Worked : Charges for per hour : -----------------
--------------
Roll No : 22
Name : abc
Class : SY
Percentage : 2.445931e-32
-------------------------------
No of hours Worked : 2784
Charges for per hour : 0
-------------------------------
Total earn money by student : 0
Tags:
c++