Sunteți pe pagina 1din 63

NITIN KUMAR

SYBBA(CA)

Index
SLIP PROGRAMS SIGNATURE
NO.
1 A. Functions to calculate and display the salary of worker.
B. Create class Person which contains data member as Passport_Id,
Person_name,Nationality, Gender, Date_of_Birth, Date_of_Issue,
Date_of_expiry
3 A. Create and initialize the object by using parameterized
constructor and display date in dd-mm-yyyy format.
B. Create a base class Student(Roll_No, Name) which derives two
classes
4 A. Create a class Part which contains data members as Part_Id,
Part_Name, Part_Price
B. Create a base class Account Derive a two classes as
Saving_Account and Current_Account from Account.
5 A. Create a class Item with data members Item_Code, Item_Name,
Item_Price. Write member functions to accept and display Item
information also display number of objects created for a class.
6 A. Create a class which contains two dimensional integer array of
size mXn.
B.Create a class Time which contains data members as: Hours,
Minutes and Seconds.
7 A. Create a class which contains single dimensional integer array of
given size
B. Create a Base class Train containing protected data members as
Train_no, Train_Name. Derive a class Route (Route_id, Source,
Destination) from Train class.
8 A. Create a class Employee which contains data members as
Emp_Id, Emp_Name, Basic_Salary, HRA, DA, Gross_Salary
B. Overloading operators >> and << to accept and display a Date
also write a member function to validate a date.

9 A. Create a class Person which contains data members as P_Name,


P_City, P_Contact_Number
B. Create two base classes Learning_Info( Roll_No, Stud_Name,
Class, Percentage) and Earning_Info(No_of_hours_worked,
Charges_per_hour
11 A. Find area and volume of cylinder using Inline function.
B. Create a class College containing data members as College_Id,
College_Name,
Establishment_year, University_Name
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

13
A. Calculate area of cone, sphere and circle by using function
overloading
B. Create a class Matrix

14 A. Create a class Book which contains data members as B_Id,


B_Name, B_Author, B_Publication
B. Create a base class Shape. Derive three different classes Circle,
Rectangle and Triangle from Shape class.
15 A. Create a class which contains two data members. Write member
functions to accept display and swap two entered numbers using
call by reference.
B. Create a base class Student(Roll_No, Name, Class) which derives
two classes
16 A. Accept n numbers from user through Command Line Argument.
Store all positive and negative numbers in two different arrays.
B. Define a class Product that contains data member as Prod_no,
Prod_Name, Prod_Price.
17 A. Create a class Student which contains data members as
Roll_Number, Stud_Name, Percentage.
B. Create a class Distance which contains data members as:
kilometer, meter
22 A. Write the definition for a class called point that has x & y as
integer data members.
B. Create a base class Conversion. Derive three different classes
Weight (Gram, Kilogram), Volume(Milliliter, Liter), Currency(Rupees,
Paise) from Conversion class
24 A. Create a C++ class Sumdata to perform following functions:
int sum( int, int) returns the addition of two integer arguments
B. Create two classes Class1 and Class2. Each class contains one float
data member.
25 A. Create a class Clock that contains integer data members as hours,
minutes and seconds.
B. Create a class Person that contains data members as
Person_Name, City, Mob_No.
27 A. Write a class sales (Salesmam_Name, Product_name,
Sales_Quantity, Target). Each salesman deals with a separate
product and is assigned a target for a month
B. Create a class MyString which contains a character pointer (Use
new and delete operator).

30 A.Sort integer and float array elements in ascending order by using


function overloading
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

Q.1A) Write a C++ program to create a class Worker with data members as
Worker_Name, No_of_Hours_worked, Pay_Rate. Write necessary member
functions to calculate and display the salary of worker. (Use default
value for Pay_Rate)
Q.1B) Create class Person which contains data member as Passport_Id,
Person_name, Nationality, Gender, Date_of_Birth, Date_of_Issue,
Date_of_expiry .
Write a c++ program to perform following member functions:
i. Enter details of all persons
ii. Display passport details of one person
iii. Display passport details of all persons
(Use Function overloading and Array of object).
1A)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Worker
{
char wname[20];
int n;
float pr;
public:
void accept(char lw[20], int ln, float lpr = 10)
{
strcpy(wname, lw);
n = ln;
pr = lpr;
}
void calculate_display()
{
cout<<"\nSalary of worker ="<<n * pr;
}
};
int main()
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

{ clrscr();
char wname[20];
int n;
Worker wobj;
cout<<"\nEnter wname, hours =";
cin>>wname>>n;
wobj.accept(wname, n);
wobj.calculate_display();
getch();
return 0;
}

1B)
#include<iostream.h>
#include<conio.h>
class Person
{
int pid;
char pname[20];
public:
void accept();
void display(int pid);
void display();
};
void Person :: accept()
{
cout<<"\nEnter pid, pname ";
cin>>pid>>pname;
}
void Person :: display()
{
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<"\n PID = "<<pid;


cout<<"\n PNAME = "<<pname;
}
void Person :: display(int temp_pid)
{
if(temp_pid == pid)
{
cout<<"\n PID = "<<pid;
cout<<"\n PNAME = "<<pname;
}
}
int main()
{
clrscr();
Person parr[2];//array of object
for(int i = 0; i < 2; i++)
{
cout<<"\n Enter details of person number "<<i + 1;
parr[i].accept();
}
cout<<"\n Details of all the persons are below ...";
for(i = 0; i < 2; i++)
{
cout<<"\n Details of person number "<<i + 1;
parr[i].display();
}
int id;
cout<<"\n Enter person id of one person.";
cin>>id;
for(i = 0; i < 2; i++)
{
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

parr[i].display(id);
}
getch();
return 0;
}

Q.3A) Write a C++ program to create a class Date which contains three data
members as dd, mm, yyyy. Create and initialize the object by using
parameterized constructor and display date in dd-mon-yyyy format.
(Input: 19-12-2014 Output: 19-Dec-2014) Perform validation for
month.
Q.3B) Create a base class Student(Roll_No, Name) which derives two
classes Academic_Marks(Mark1, Mark2,
Mark3) and Extra_Activities_Marks(Marks). Class Result(Total_Marks,
Grade) inherits both Academic_Marks and
Extra_Activities_Marks classes. (Use Virtual Base Class)
Write a C++ menu driven program to perform the following functions:

i. Build a master table


Calculate Total_marks and grade
3A)
#include<iostream.h>
#include<conio.h>
class date
{
int dd,mm,yy;
public:
date(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
void display()
{
cout<<"\ngiven date is\t";
cout<<dd<<"-"<<mm<<"-"<<yy;
cout<<"\nAfter formating date is\t";
switch(mm)
{
case 1:
cout<<"\n"
<<dd<<"-Jan-"<<yy;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

break;
case 2:
cout<<"\n"
<<dd<<"-Feb-"<<yy;
break;
case 3:
cout<<"\n"
<<dd<<"-Mar-"<<yy;
break;
case 4:
cout<<"\n"
<<dd<<"-Apr-"<<yy;
break;
case 5:
cout<<"\n"
<<dd<<"-May-"<<yy;
break;
case 6:
cout<<"\n"
<<dd<<"-Jun-"<<yy;
break;
case 7:
cout<<"\n"
<<dd<<"-Jul-"<<yy;
break;
case 8:
cout<<"\n"
<<dd<<"-Aug-"<<yy;
break;
case 9:
cout<<"\n"
<<dd<<"-Sep-"<<yy;
break;
case 10:
cout<<"\n"
<<dd<<"-Oct-"<<yy;
break;
case 11:
cout<<"\n"
<<dd<<"-Nov-"<<yy;
break;
case 12:
cout<<"\n"
<<dd<<"-Dec-"<<yy;
break;
default:
cout<<"\nI
nvalid month";
}
}

};
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

void main()
{
clrscr();int m,dt,y;
cout<<"\n Enter date : ";
cin>>dt;
cout<<"\n Enter month : ";
cin>>m;
cout<<"\n Enter year : ";
cin>>y;
date d(dt,m,y);
d.display();
getch();
}

3B)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
public:
int roll_no;
char name[20];
student()
{
}
student(int rn,char na[20])
{
roll_no=rn;
strcpy(name,na);
}
};
class academic_marks:virtual public student
{
public:
int mark1,mark2,mark3;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

academic_marks()
{
}
academic_marks(int m1,int m2,int m3)
{
mark1=m1;
mark2=m2;
mark3=m3;
}
};
class extra_activities_marks:virtual public student
{
public:
int mark4;
extra_activities_marks()
{
}
extra_activities_marks(int m4)
{
mark4=m4;
}
};
class result:public academic_marks,public extra_activities_marks
{
int total_marks;
float perc;
public:
result()
{
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

result(int rn,char na[20],int m1,int m2,int m3,int


m4):student(rn,na),academic_marks(m1,m2,m3),extra_activities_marks(m4)
{
}
void cal()
{
total_marks=mark1+mark2+mark3+mark4;
perc=total_marks/4;
cout<<"\ntotal marks:"<<total_marks;
cout<<"\npercentage:"<<perc;
}
};
int main()
{
clrscr();
int rn, m1, m2, m3, m4;
rn = 10;
char na[20];
cout<<"\nenter name:";
cin>>na;
cout<<"enter marks:";
cin>>m1>>m2>>m3>>m4;
result r(rn,na,m1,m2,m3,m4);
r.cal();
getch();
return 0;
}

Q.4A) Write a C++ program to create a class Part which contains data
members as Part_Id, Part_Name, Part_Price. Create and Initialize all
values of Part object by using parameterized constructor and copy
constructor. Display the values of Part object. (Part_price should
be right justified with a precision of two digits)
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

Q.4B) Create a base class Account (Acc_Holder_Name,


Acc_Holder_Contact_No). Derive a two classes as
Saving_Account(S_Acc_No., Balance) and Current_Account( C_Acc_No.,
Balance) from Account. Write a C++ menu driven program to perform
following functions :
i. Accept the details for ‘n’ account holders.
Display the details of ‘n’ account holders by adding interest amount where
interest rate for Saving account is 5% of balance and interest rate for
Current account is 1.5% of balance.
4A)
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class part
{
int part_id;
char part_name[10];
float part_price;
public:
part()
{
}
part(int pi,char pn[10],float pp)
{
part_id=pi;
strcpy(part_name,pn);
part_price=pp;
}
part(part &p1)
{
part_id=p1.part_id;
strcpy(part_name,p1.part_name);
part_price=p1.part_price;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

}
void putdata()
{
cout<<"\npartid="<<part_id;
cout<<"\npartname="<<part_name;
cout<<"\npartprice="<<setw(10)<<setprecision(2)<<part_price;
}
};
int main()
{
clrscr();
int n,pi;
char pn[10];
float pp;
part p1;
cout<<"\nenter partid=";
cin>>pi;
cout<<"\nenter partname=";
cin>>pn;
cout<<"\nenter partprice=";
cin>>pp;
cout<<"\nobject for parameterized constructor:";
p1=part(pi,pn,pp);
p1.putdata();
cout<<"\nobject for copy constructor:";
part p2(p1);
p2.putdata();
getch();
return 0;
}
4B)
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<iostream.h>
#include<conio.h>

class Account
{
public:
char name[10];
long cno;
public:
void getdata()
{
cout<<"\n Account holder
name : ";
cin>>name;
cout<<"\n contact no : ";
cin>>cno;
}
void display()
{
cout<<"\n Name :"<<name;
cout<<"\n contact No
:"<<cno;
}
};
class saving_Account:public Account
{
int acc_no;
float bal,T_bal;
public:
void getdata()
{ Account::getdata();
cout<<"\n Account no : ";
cin>>acc_no;
cout<<"\n Enter balance:
";
cin>>bal;
}
void cal()
{
T_bal=bal+(bal*0.05);
}
void display()
{ Account::display();

cout<<"\nAccount no :
"<<acc_no;
cout<<"\nbalance
:"<<bal<<endl;
cout<<"\nTotal balance
:"<<T_bal<<endl;
}
};
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

class current_Account:public Account


{ int acc_no;
float bal;
public:
void getdata()
{ Account::getdata();
cout<<"\n Enter current
account number";
cin>>acc_no;
cout<<"\n Enter available
balance : ";
cin>>bal;

void cal()
{
bal=bal+(bal*(1.5/100));
}
void display()
{ Account::display();
cout<<"\n Account no :
"<<acc_no;
cout<<"\n Total balance
:"<<bal<<endl;
}

};
void main()
{
int i,n,n1,ch;
clrscr();
current_Account c[10];saving_Account s[10];

do
{cout<<"\n 1.Enter Current Account datails \n 2.Enter
Saving Account Details \n 3.Display Current Account Holder \n 4.Display
Saving Account Holders \n 0.Exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<"\n Enter
how many current Account holders ? ";
cin>>n;
for(i=0;i<
n;i++)
{

c[i].getdata();
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

c[i].getdata();
}
break;
case 2 : cout<<"\n Enter
how many saving Account holders ? ";
cin>>n1;
for(i=0;i<
n1;i++)
{

s[i].getdata();

s[i].cal();
}
break;
case 3 : cout<<"\n Details
of curent account holders : ";
for(i=0;i<
n;i++)
{
c[i].cal();

c[i].display();
}
break;
case 4 : cout<<"\n Details
of saving account holders : ";
for(i=0;i<
n1;i++)
{

s[i].display();
}
break;
case 0 : break;
}
}while(ch!=0);
getch();
}

Q.6A) Write a C++ program to create a class which contains two dimensional
integer array of size mXn. Write a member function to display sum of
all elements of entered matrix. (Use Dynamic Constructor for
allocating memory and Destructor to free memory of an object)
Q.6B) Create a class Time which contains data members as: Hours, Minutes
and Seconds. Write C++ program to perform following necessary member
functions:
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

i. To read time
ii. To display time in format like: hh:mm:ss
To add two different times (Use Objects as argument)
6A)
#include<conio.h>
#include<iostream.h>
main()
{
int r,c,sum=0;
clrscr();
cout<<"\n Enter dimension ";
cin>>r;
cin>>c;
int **a;
a=new int*[r];//[s];
for(int i=0;i<r;i++)
{ a[i]=new int[c];
}
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\n enter data : ";
cin>>a[i][j];
}
}
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}

for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

sum=sum+a[i][j];
}
}
cout<<"\n sum of all elements of matrix : "<<sum;
getch();
}

6B)
#include<iostream.h>
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<conio.h>

class Time
{
int hr,min,sec;
public:
void settime(int x,int y,int z)
{ hr=x;
min=y;
sec=z;
}

void showtime()
{ cout<<"\n"<<hr<<":"<<min<<":"<<sec<<endl;
}

Time add(Time t)
{
Time t1;
t1.sec=sec+t.sec;
t1.min=t1.sec/60;
t1.sec=t1.sec%60;
t1.min=t1.min+min+t.min;

t1.hr=t1.min/60;
t1.min=t1.min%60;
t1.hr=t1.hr+hr+t.hr;
return t1;
}

};

void main()
{
int h,m,s;
cout<<"\n Enter hr:\t";
cin>>h;
cout<<"\n Enter min:\t";
cin>>m;
cout<<"\n Enter sec:\t";
cin>>s;
Time t1;
t1.settime(h,m,s);
t1.showtime();
Time t2;
cout<<"\n Enter hr:\t";
cin>>h;
cout<<"\n Enter min:\t";
cin>>m;
cout<<"\n Enter sec:\t";
cin>>s;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

t2.settime(h,m,s);
t2.showtime();

Time t3;
cout<<"\t Addition of 2 times : ";
t3=t1.add(t2);
t3.showtime();
getch();
}

Q.7A) Write a C++ program to create a class which contains single


dimensional integer array of given size. Write a member function to
display even and odd numbers from a given array. (Use Dynamic
Constructor to allocate and Destructor to free memory of an object)
Q.7B) Create a Base class Train containing protected data members as
Train_no, Train_Name. Derive a class Route (Route_id, Source,
Destination) from Train class.Also derive a class
Reservation(Number_Of_Seats, Train_Class, Fare, Travel_Date) from
Route. Write a C++ program to perform following necessary functions
:
i. Enter details of ‘n’ reservations
ii. Display details of all reservations
Display reservation details of a specified Train class
7A)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class dynamic
{
int size,*ptr,*p;
public:
dynamic(int no)
{
size=no;
ptr=new int[size];
for(int i=0;i<size;i++)
{
cout<<"enter element";
cin>>ptr[i];
}
}
void display()
{
cout<<"elements are";
for(int i=0;i<size;i++)
{
cout<<ptr[i]<<"\t";
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

void evnodd()
{
int i;
cout<<"even nos are : \t";
for(i=0;i<size;i++)
{
if(ptr[i]%2==0)
{
cout<<ptr[
i];
cout<<"\t"
;
}
}
cout<<"\n Odd nos are : \t ";
for(i=0;i<size;i++)
{
if(ptr[i]%2!=0)
{
cout<<ptr[
i];
cout<<"\t"
;
}
}
}

~dynamic()
{delete ptr;
}
};
void main()
{
int n;
clrscr();
cout<<"enter size";
cin>>n;
dynamic d(n);
d.display();
d.evnodd();
getch();
}

7B)
#include<iostream.h>
#include<conio.h>
#include<string.h>
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

class Train
{
protected:
char tname[20];
public:
void accept_td()
{
cout<<"\nEnter train name ";
cin>>tname;
}
};
class Route : public Train
{
};
class Reservation: public Route
{
char tclass[20];
public:
void accept_resd()
{
accept_td();
cout<<"\nEnter train class :";
cin>>tclass;
}
void display_trainclass_wise(char temp_class[20])
{
if(strcmp(temp_class,tclass) == 0)
{
display_res();
}
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

void display_res()
{
cout<<"\nT class = " <<tclass;
}
};
int main()
{
clrscr();
Reservation rarr[1];
char ttclass[20];
for(int i = 0;i < 1;i++)
rarr[i].accept_resd();
cout<<"\n\nEnter specified class ....";
cin>>ttclass;
for(i = 0; i<1;i++)
rarr[i].display_trainclass_wise(ttclass);
getch();
return 0;
}

Q.8A) Write a C++ program to create a class Employee which contains data
members as Emp_Id, Emp_Name, Basic_Salary, HRA, DA, Gross_Salary.
Write member functions to accept Employee information. Calculate and
display Gross salary of an employee. (DA=12% of Basic salary and HRA
= 30% of Basic salary) (Use appropriate manipulators to display
employee information in given format :- Emp_Id and Emp_Name should
be left justified and Basic_Salary, HRA, DA, Gross salary Right
justified with a precision of two digits)
Q.8B) Create a class Date containing members as:
- dd
- mm
- yyyy
Write a C++ program for overloading operators >> and << to accept and
display a Date also write a member function to validate a date.
8A)
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class emp
{
int eid;
float bs,hra,da,gs;
char name[10];
public:
void accept()
{
cout<<"enter id";
cin>>eid;
cout<<"enter name";
cin>>name;
cout<<"enter bs";
cin>>bs;
}

void display()
{
cout<<"\n eid="<<eid<<"\n
name="<<name<<"\n bssic salary = "<<bs;
cout<<"\n HRA = "<<(bs*0.30);
cout<<"\n DA = "<<(bs*0.12);
cout<<"\n Gross Salary = "<<(bs-
((bs*0.12)+(bs*0.30)));
}
};

void main()
{
emp ob;
clrscr();
ob.accept();
ob.display();
getch();
}

8B)
#include<iostream.h>
#include<conio.h>
class date
{
int dd,mm,yyyy;
public:
friend ostream &operator<<(ostream &out, date &dboj);
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

friend istream &operator>>(istream &in ,date &dobj);


void validation();
};
istream &operator>>(istream &in,date &dobj)
{
cout<<"\n enter date:";
in>>dobj.dd;
cout<<"\n enter month and year:";
in>>dobj.mm>>dobj.yyyy;
return in;
}
ostream &operator<<(ostream &out ,date &dobj)
{
out<<"date="<<dobj.dd<<"/"<<dobj.mm<<"/"<<dobj.yyyy;
return out;
}
void date:: validation()
{
if(mm<=12 && dd<=31)
cout<<"\n date is valid..........."<<endl;
else
cout<<"\n date is invalid........."<<endl;
}
int main()
{
clrscr();
date obj;
cin>>obj;
obj.validation();
cout<<obj;
getch();
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

return 0;
}

Q.9A) 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
‘*’)
Q.9B) 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)
9A)
#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()
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

{
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();
}

9B)
#include<iostream.h>
#include<conio.h>
class learning_info
{
int roll;
char sname[20],class1[20];
float per;
public:
void accept()
{
cout<<"\n enter the roll no,name ,class,per=";
cin>>roll>>sname>>class1>>per;
}
void display()
{
cout<<"\n roll no="<<roll<<"\n name="<<sname<<"\n
class="<<class1<<"\n percentage="<<per;
}
};
class earning_info
{
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

int noh;
float cph;
public:
void accept1()
{
cout<<"\n enter the number of hours and chargers per hours=";
cin>>noh>>cph;
}
void display1()
{
cout<<"\n no of hours="<<noh<<"\n chargers per hours="<<cph;
}
void calculate()
{
cout<<"\n total earning money="<<noh*cph;
}
};
class earn_learn: public learning_info,public earning_info
{
public:
void accept2()
{
accept();
accept1();
}
void display2()
{
display();
display1();
calculate();
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

};
int main()
{
clrscr();
earn_learn eobj;
eobj.accept2();
eobj.display2();
getch();
return 0;
}

Q.11A) Write a C++ program to find area and volume of cylinder using
Inline function.
Q.11B) Create a class College containing data members as College_Id,
College_Name,
Establishment_year, University_Name. Write a C++ program with
following member functions:
i. To accept ‘n’ College details
ii. To display College details of a specified University
iii. To display College details according to a specified
establishment year
(Use Array of Object and Function overloading)
11A)
#include<iostream.h>
#include<conio.h>
#define pi 3.14
class cylinder
{
float r,h;
public:
void getdata()
{
cout<<"\n Enter radius and height=";
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cin>>r>>h;
}
inline void area()
{
cout<<"\n area of cylinder="<<2*pi*r*h;
}
inline void volume()
{
cout<<"\n volume of cylinder="<<pi*r*r*h;
}
};
int main()
{
clrscr();
cylinder cobj;
cobj.getdata();
cobj.area();
cobj.volume();
getch();
return 0;
}
11B)
#include<conio.h>
#include<iostream.h>
#include<string.h>
class college
{
int id,n,yr;
char name[10],uname[10];
public:
void accept()
{
cout<<"\n Enter id : ";
cin>>id;
cout<<"\n Enter name : ";
cin>>name;
cout<<"\n Enter univercity name : ";
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cin>>uname;
cout<<"\n Enter year : ";
cin>>yr;

void display()
{
cout<<"\n id = "<<id<<"\n name = "<<name<<"\n Univercity name =
"<<uname<<"\n year = "<<yr<<endl;
}

int display(char unin[])


{
if(strcmp(uname,unin)==0)
{
display();
return 1;
}
else return 0;
}

int display(int year)


{
if(yr==year)
{
display();
return 1;
}
else return 0;
}
};

void main()
{
int n,yr,a;
college ob[10];
char uni[10];
clrscr();
cout<<"enter no of college";
cin>>n;
for(int i=0;i<n;i++)
{
ob[i].accept();
}

for(i=0;i<n;i++)
{
ob[i].display();
}

cout<<"\n Enter uname to search : "<<endl;


NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cin>>uni;
int cnt=0;
for(i=0;i<n;i++)
{
a=ob[i].display(uni);
if(a==1)
cnt++;
}
if(cnt==0)
cout<<"\n Univercity name is NOT found "<<endl;

cout<<"\n Enter establishment year to search : "<<endl;


cin>>yr;
cnt=0;
for(i=0;i<n;i++)
{
a=ob[i].display(yr);
if(a==1)
cnt++;
}
if(cnt==0)
cout<<"\n Year NOT found ";

getch();
}

Q.13A) Write a C++ program to calculate area of cone, sphere and


circle by using function overloading.
Q.13B) Create a class Matrix and Write a C++ program with following
functions:
i. To accept a Matrix
ii. To display a Matrix
iii. Overload unary minus ‘–‘ operator to calculate transpose of a
Matrix
Overload binary multiplication '*’ operator to calculate multiplication of
two matrices

13A)
#include<iostream.h>
#include<conio.h>
class calculate
{
float r_cn,r_s,side;
int r_c;
public:

void area(float r, float s)


{
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

r_c=r;
side=s;
cout<<"\nArea of cone
=\t\t"<<3.14*r_c*side;
}

void area(float r)
{
r_s=r;
cout<<"\nArea of sphere
=\t"<<4*3.14*r_s*r_s;
}

void area(int r)
{
r_c=r;
cout<<"\nArea of circle
=\t"<<3.14*r_c*r_c;
}
};
void main()
{
float r_cone,side;
int r_circle;
float sp;
clrscr();
calculate c;
cout<<"\nEnter radius of cone\t";
cin>>r_cone;
cout<<"\nEnter side of cone\t";
cin>>side;
cout<<"\nEnter radius of sphere\t";
cin>>sp;
cout<<"\nEnter radius of circle\t";
cin>>r_circle;
c.area(r_cone,side);
c.area(sp);
c.area(r_circle);
getch();
}

13B)
#include<iostream.h>
#include<conio.h>
class matrix
{
public:
int r,c,a[10][10],i,j;
void getdata()
{
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<"\n Enter number of rows to be inserted in matrix :


";
cin>>r;
cout<<"\n Enter number of columns to be inserted in matrix
: ";
cin>>c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\n Enter matrix elements of "<<i<<"
row";
cin>>a[i][j];
}
}
}
void display()
{
for(i=0;i<r;i++)
{
cout<<endl;
for(j=0;j<c;j++)
{
cout<<a[i][j];
cout<<"\t";
}
}
}
void operator-()
{
matrix m(r,c);
for(i=0;i<r;i++)
{
cout<<endl;
for(j=0;j<c;j++)
{
m.a[i][j]=a[j][i];
}
}
m.display();
}

matrix()
{}
matrix(int row,int column)
{
r=row;
c=column;
}
friend matrix operator*(matrix,matrix);
};
matrix operator*(matrix obj,matrix obj1)
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

{
int i,j,k,sum;
if(obj.c==obj1.r)
{
matrix k1(obj.r,obj1.c);
for(i=0;i<obj.r;i++)
{
//cout<<endl;
for(j=0;j<obj1.c;j++)
{
//k.a[i][j]=0;
sum=0 ;
for(k=0;k<obj.c;k++)
{
sum=sum+obj.a[i][k] *
obj1.a[k][j];
}
k1.a[i][j]=sum;
}
}
return k1;
}
else
{
cout<<"multipliction is not possible";
matrix k(0,0);
return k;
}
}
void main()
{
clrscr();
matrix m1,m2,m3;
m1.getdata();
m2.getdata();
cout<<"\n first matrix is :";
m1.display();
cout<<"\n 2nd matrix is : ";
m2.display();
m3=m1*m2;
cout<<"\n Transpose of 1st matrix is : ";
-m1;
cout<<"\n mutiplication of matrix is : \n";
m3.display();
getch();
}

Q.14A) Write a C++ program to create a class Book which contains data
members as B_Id, B_Name, B_Author, B_Publication. Write member
functions to accept and display Book information also display Count
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

of books. (Use Static data member to maintain Count of books)

Q.14B) Create a base class Shape. Derive three different classes


Circle, Rectangle and Triangle from Shape class. Write a C++ program to
calculate area of Circle, Rectangle and Triangle. (Use pure virtual
function).
14A)
#include<iostream.h>
#include<conio.h>
class book
{
int id;
char name[20],author[20],pub[20];
static int cnt;
public:
void getdata()
{
cout<<"\nEnter book id : ";
cin>>id;
cout<<"\nEnter book name : ";
cin>>name;
cnt++;
cout<<"\nEnter author name : ";
cin>>author;
cout<<"\nEnter publication : ";
cin>>pub;
}
void display()
{
//cout<<"\n\n*************OUTPUT**********
*******";
cout<<"\nbook id = "<<id;
cout<<"\nbook name = "<<name;
cout<<"\nAuthor name = "<<author;
cout<<"\npublication = "<<pub;
}
static void no_of_book()
{
cout<<"\nNumber of book = "<<cnt;
}
};

int book::cnt;

void main()
{
clrscr();
book b[20];int n;
cout<<"\nEnter no f Books : ";
cin>>n;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

for(int i=0;i<n;i++)
b[i].getdata();
cout<<"\n Book Information are : \n \n";
for( i=0;i<n;i++)
b[i].display();
b[i-1].no_of_book();
getch();
}

14B)
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void accept()=0;
virtual void area()=0;
};

class circle:public shape


{
public:
int r;
void accept()
{
cout<<"\n Enter radius for circle : ";
cin>>r;
}
void area()
{
cout<<"\n Area of circle="<<3.14*r*r;
}
};

class rectangle:public shape


{
public:
int l,b;
void accept()
{
cout<<"\n Enter length and breadth for rectangle : ";
cin>>l;
cin>>b;
}

void area()
{
cout<<"\n Area of rectangle="<<l*b;

}
};
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

class triangle:public shape


{
public:
int b,h;
void accept()
{
cout<<"\n Enter base and height for triangle : ";
cin>>b;
cin>>h;
}

void area()
{
cout<<"\n Area of triangle="<<0.5*b*h;
}
};

void main()
{
triangle t;
circle c;
rectangle r;
shape *ptr;
clrscr();
ptr=&t;
ptr->accept();
ptr->area();
ptr=&c;
ptr->accept();
ptr->area();
ptr=&r;
ptr->accept();
ptr->area();
getch();
}

Q.15B) Write a C++ program to create a class which contains two data
members. Write member functions to accept display and swap two
entered numbers using call by reference.
Q.15B) Create a base class Student(Roll_No, Name, Class) which
derives two classes Internal_Marks(IntM1, IntM2, IntM3, IntM4,
IntM5) and External_Marks(ExtM1 ExtM2, ExtM3, ExtM4, ExtM5). Class
Result(T1, T2, T3, T4, T5) inherits both Internal_Marks and
External_Marks classes. (Use Virtual Base Class)
Write a C++ menu driven program to perform the following functions:
i. To Accept and display student details
ii. Calculate Subject wise total marks obtained. Check
whether student has passed in Internal and External
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

Exam of each subject. Also check whether he has


passed in respective subject or not and display
result accordingly.
15A)
#include<iostream.h>
#include<conio.h>
class swapp
{
public:
int a,b;
void accept()
{
cout<<"enter two numbers";
cin>>a>>b;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void display()
{
cout<<"\n no1 = "<<a<<"\t
no2 = "<<b;
swap(a,b);
}
};
void main()
{
swapp s;
clrscr();
s.accept();
cout<<"\n Before swapping : ";
s.display();
cout<<"\n After swapping : ";
s.display();
getch();
}

15B)
#include<iostream.h>
#include<conio.h>
#include<string.h>

class student
{
protected:
int rno;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

char name[10],cls[10];
public:
void accept()
{
cout<<"\n Enter roll no if
sudent : ";
cin>>rno;
cout<<"\n Enter the name
of student :";
cin>>name;
cout<<"\n Enter the class
of student :";
cin>>cls;
}
void display()
{
cout<<"\n Roll no.
:"<<rno<<"\n Name :"<<name<<"\n class : "<<cls<<endl;
}
};
class Internal_Mark:public virtual student
{
protected:
int i_mark[6];
public:
int internal_mark()
{ int i;
cout<<"\n Enter the 6
subjects marks :";
for(i=0;i<6;i++)
{ cout<<"subj
ect "<<i+1<<" : ";
cin>>i_mar
k[i];
}
for(i=0;i<6;i++)
{ if(i_mark[i
]>20)
return 1;
}
return 0;
}
void i_display()
{
cout<<"\n Internal marks
is : \n ";
for(int i=0;i<6;i++)
{
cout<<"sub
ject "<<i+1<<"\t"<<i_mark[i]<<endl;
}
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

};
class External_Mark:public virtual student
{
protected:
int e_mark[6];
public:
int external_mark()
{ int i;
cout<<"\n Enter the 6
subjects External marks :";

for(i=0;i<6;i++)
{ cout<<"\n
subject "<<i+1<<" : ";
cin>>e_mar
k[i];
}
for(i=0;i<6;i++)
{ if(e_mark[i
]>80 || e_mark[i]<32)
return 1;
}
return 0;

}
void e_display()
{
cout<<"\n eXternal marks
are : \n ";
for(int i=0;i<6;i++)
{
cout<<"sub
ject "<<i+1<<"\t"<<e_mark[i]<<endl;
}
}
};

class result: public Internal_Mark,public External_Mark


{
int total[6];
char grade[10];
float per;
public:
void cal_res()
{ for(int i=0;i<6;i++)
{

total[i]=i_mark[i]+e_mark[i];
}
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

void dis_res()
{
display();
i_display();
e_display();
cout<<"Total marks : \n ";
for(int i=0;i<6;i++)
{cout<<"subject
"<<i+1<<"\t"<<total[i]<<endl;}

};
int main()
{
clrscr();

int n,ch,a,b;
result obj;
do
{
cout<<"\n 1.student info \n 2.Internal mark \n 3.Extranal
mark \n 4. Result \n 0. exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{ case 1: obj.accept();
break;
case 2:
a=obj.internal_mark();
break;
case 3:
b=obj.external_mark();
break;
case 4: if(a==0 && b==0)
{obj.cal_r
es();
obj.dis_re
s();}
else
cout<<"Fail";
break;
case 0: break;
default: cout<<"\n Invalid
choice : ";
}
}while(ch!=0);
getch();
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

Q.16A) Write a C++ program to accept ‘n’ numbers from user through
Command Line Argument. Store all positive and negative numbers in
two different arrays. Display contents both arrays.
Q.16B) Define a class Product that contains data member as Prod_no,
Prod_Name, Prod_Price. Derive a class Discount(discount_in_Percentage)
from class Product. A Customer buys ‘n’ products. Accept quantity for each
product , calculate Total Discount and accordingly generate Bill. Display
the bill using appropriate Manipulators.
16A)
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int pos[20],neg[20],n,j=0,k=0;
clrscr();
for(int i=1;i<argc;i++)
{
n=atoi(argv[i]);
if(n>=0)
{
pos[k]=n;
k++;
}
else
{
neg[j]=n;
j++;
}
}

cout<<"\n positive no array is : ";


for(i=0;i<k;i++)
{
cout<<"\t"<<pos[i];
}

cout<<"\n Negative no array is : ";


for(i=0;i<j;i++)
{
cout<<"\t"<<neg[i];
}

getch();
}

16B)
#include<iostream.h>
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<conio.h>
#include<iomanip.h>
class Product
{ public:
int no;
char pname[20];
float price;
public :
void accept()
{ cout<<"\n Enter product id : ";
cin>>no;
cout<<"\n Enter product name : ";
cin>>pname;
cout<<"\n Enter product price : ";
cin>>price;
}

};

class Discount : public Product


{
public :
int d;
void accept_D()
{
cout<<"\n Enter product discount : ";
cin>>d;
}
void display()
{ cout<<setiosflags(ios::left)<<setw(15
)<<pname<<setw(10)<<price<<setiosflags(ios::right)<<setw(10)<<d<<endl;
}
};

void main()
{
Discount ob[10];
int i,n,total=0,discnt=0;
char cname[20];clrscr();
cout<<"\n Enter name of customer : ";
cin>>cname;
cout<<"\n Enter no product to be puraches : ";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].accept();
ob[i].accept_D();
}
cout<<"\n **********YOUR BILL**************\n";
cout<<"\n Name of customer : "<<cname<<endl;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<setiosflags(ios::left)<<setw(15)<<"Product name
"<<setw(10)<<"Price"<<setiosflags(ios::right)<<setw(10)<<"Discount"<<endl;
for(i=0;i<n;i++)
{
ob[i].display();
total=total+ob[i].price;
discnt=discnt+((ob[i].price * ob[i].d)/100);

}
cout<<"\n=====================================\n";
cout<<"Total : ";
cout<<setw(25)<<setiosflags(ios::right)<< total-discnt;
getch();
}

Q.17A) 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)
Q.17B) Create a class Distance which contains data members as:
kilometer, meter. Write C++ program to perform following functions:
i. To accept a distance
ii. To display a distance
iii. To overload += operator to add two distances.
To overload > operator to compare two distances
17A)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class student
{
char name[10];
float per;
int rno;
public:
void accept()
{
cout<<"\nEnter rno : ";
cin>>rno;
cout<<"\nEnter name : ";
cin>>name;
cout<<"\nEnter percentage : ";
cin>>per;
}

void display()
{
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<"\nrno = "<<rno;
cout<<"\nName = "<<name;
cout<<"\nPercantage = "<<per<<"%";
if(per>=70&&per<=100)
cout<<"\nDistinction";
else if(per<70&&per>=60)
cout<<"\n1st class";
else if(per<60&&per>=50)
cout<<"\n2nd class";
else if(per<50&&per>40)
cout<<"\npass class";
else
cout<<"\nfail";
cout<<"\n=================================
";
}

};

void main()
{
int n,i;
student ob[10];
clrscr();
cout<<"enter no of students";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].accept();
}

for(i=0;i<n;i++)
{
ob[i].display();
}

getch();
}

17B)
#include<iostream.h>
#include<conio.h>
class distance
{
public:
int km,m;
void getdata()
{
cout<<"enter distance in kilometer\t";
cin>>km;
cout<<"enter distance in meters\t";
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cin>>m;
}
void display()
{
cout<<"entered distance is\t";
cout<<km<<"."<<m;
cout<<endl;
}
void operator+=(distance d)
{
int kilometer=0,meter=0;
kilometer=km+d.km;
meter=m+d.m;
if(meter>=1000)
{
kilometer=km+d.km+(meter/1000);
meter=meter%1000;
}
cout<<"\naddition of two distances is\t"<<kilometer<<"."<<meter;
}
void operator>(distance d)
{
if(km>d.km)
{
cout<<"\ngreater distance is\t"<<km<<"."<<m;
}
else
{
cout<<"\ngreater distance is\t"<<d.km<<"."<<d.m;
}
}
};
void main()
{
clrscr();
distance d,d1;
d.getdata();
d1.getdata();
d.display();
d1.display();
d+=d1;
d>d1;
getch();
}

Q.22A) Write the definition for a class called ‘point’ that has x & y
as integer data members. Overload the assignment operator (=) to
copy one object to another. (Use Default and parameterized
constructor to initialize the appropriate objects)
Write a C++ program to illustrate the use of above class.
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

Q.22B) Create a base class Conversion. Derive three different classes


Weight (Gram, Kilogram), Volume(Milliliter, Liter), Currency(Rupees,
Paise) from Conversion class.
Write a C++ program to perform read, convert and display operations. (Use
Pure virtual function)
22A)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class point
{
int x,y;
public:
point()
{
}

point(int a,int b=200)


{
x=a;
y=b;
}

void operator=(point ob2)


{
x=ob2.x;
y=ob2.y;
}

void display()
{
cout<<"\nx = "<<x<<"\t y = "<<y;
}
};

void main()
{
point ob1(100,200),ob2;
int n,m;
clrscr();

//cin>>m;
ob1.display();
//ob2.display();
ob2=ob1;
cout<<"\nAfter overload = operator .\n";
ob2.display();

cout<<"\nEnter number : ";


cin>>n;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<"\nEnter number : ";


cin>>m;
point ob3(n,m);
ob3.display();
ob2=ob3;
cout<<"\nAfter overload = operator .\n";
ob2.display();

getch();
}

22B)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class conversion
{
public:
virtual void accept1()=0;
virtual void accept2()=0;
virtual void convert1()=0;
virtual void convert2()=0;
};
class weight:public conversion
{
public:
float gm,kgm;
void accept1()
{
cout<<"Enter grams to convert : ";
cin>>gm;
}
void accept2()
{
cout<<"Enter kilograms to convert : ";
cin>>kgm;
}
void convert1()
{
cout<<"After conversion gm = "<<gm/1000<<" kgm\n";
}
void convert2()
{
cout<<"After conversion kgm = "<<kgm*1000<<" gm\n";
}
};

class volume:public conversion


{
public:
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

float ml,ltr;
void accept1()
{
cout<<"\n Enter mililiter to convert : ";
cin>>ml;
}
void accept2()
{
cout<<"\n Enter liter to convert : ";
cin>>ltr;
}
void convert1()
{
cout<<"After conversion ml = "<<ml/1000<<" ltr\n";
}
void convert2()
{
cout<<"After conversion ltr = "<<ltr*1000<<" ml\n";
}
};

class currency:public conversion


{
public:
float ps,rs;
void accept1()
{
cout<<"Enter paise to convert :";
cin>>ps;
}
void accept2()
{
cout<<"Enter rupees to convert : ";
cin>>rs;
}
void convert1()
{
cout<<"After conversion ps = "<<ps/100<<" rs\n";
}
void convert2()
{
cout<<"After conversion rs = "<<rs*100<<" ps\n";
}
};

void main()
{
int ch;
weight w;
volume v;

currency c;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

clrscr();
do
{
cout<<"\nEnter your choice\n";
cout<<"1.weight\t2.volume\t3.currency\t4.exit\n";
cin>>ch;
switch(ch)
{
case 1: int ch1;
cout<<"Enter your choice to convert weight\n";
cout<<"1.gm to kgm\t2.kgm to gm \n";
cin>>ch1;
if(ch1==1)
{
w.accept1();
w.convert1();
break;
}
else if(ch1==2)
{
w.accept2();
w.convert2();
break;
}
else
{
cout<<"wrong choice";
break;
}

case 2: int ch2;


cout<<"Enter choice to convert to volume\n";
cout<<"1.ml to ltr\t2.ltr to ml \n";
cin>>ch2;
if(ch2==1)
{
v.accept1();
v.convert1();
break;
}
else if(ch2==2)
{
v.accept2();
v.convert2();
break;
}
else
{
cout<<"wrong choice";
break;
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

case 3:int ch3;


cout<<"Enter choice to convert currency";
cout<<"\n1psp to rp\t2.rp to ps \n";
cin>>ch3;
if(ch3==1)
{
c.accept1();
c.convert1();
break;
}
else if(ch3==2)
{
c.accept2();
c.convert2();
break;
}
else
{
cout<<"wrong choice";
break;
}
case 4:break;

default: cout<<"Enter valid choice";


break;
}
}while(ch!=4);

getch();
}

Q.24A) Create a C++ class Sumdata to perform following functions:


int sum( int, int) – returns the addition of two integer
arguments.
float sum(flaot, float, float) – returns the addition of three
float arguments.
int sum( int [ ] ,int) – returns the sum of all elements in an
array of size ‘n’.
Q.24B) Write a C++ program to create two classes Class1 and Class2.
Each class contains one float data member. Write following
functions:
i. To accept float numbers
ii. To display float numbers in right justified format with
precision of two digits
To Exchange the private values of both these classes by using Friend
function.
24A)
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class sumdata
{
public:
void sum(int a,int b)
{
int c;
c=a+b;
cout<<"\n Addition = "<<c;
}

void sum(float a,float b,float c)


{
float d;
d=a+b+c;
cout<<"\n Addition = "<<d;
}

void sum(int a[],int n)


{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[
i];
}
cout<<"sum="<<sum;
}

void accept(int a[],int n)


{
int sum=0;
cout<<"enter elements";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}

};

void main()
{
int a,b,j,k[19];
sumdata ob;
float x,y,z;
clrscr();
cout<<"\n Enter 2 numbers";
cin>>a;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cin>>b;
ob.sum(a,b);
cout<<"\n How many numbers you want to enter";
cin>>j;
ob.accept(k,j);
ob.sum(k,j);
cout<<"\n Enter 3 float values";
cin>>x;
cin>>y;
cin>>z;
ob.sum(x,y,z);
getch();
}

24B)
#include<iostream.h>
#include<conio.h>
class class2;
class class1
{
float a;
public:
void accept()
{
cout<<"\n Enter number : ";
cin>>a;
}
void display()
{
cout.setf(ios::left,ios::adjustfield);
cout.width(20);
cout<<"\n a="<<a;
}
friend void swap(class1 &,class2 &);
};

class class2
{
float b;
public:
void accept()
{
cout<<"\n Enter number : ";
cin>>b;
}
void display()
{
cout.setf(ios::left,ios::adjustfield);
cout.width(20);
cout<<"\n b="<<b;
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

friend void swap(class1 &,class2 &);


};

void swap(class1 &ob1,class2 &ob2)


{
float temp;
temp=ob1.a;
ob1.a=ob2.b;
ob2.b=temp;

void main()
{
class1 ob1;
class2 ob2;
clrscr();
ob1.accept();
ob2.accept();
ob1.display();
ob2.display();
swap(ob1,ob2);
cout<<"\n Ater swapping :\n";
ob1.display();
ob2.display();
getch();
}

Q.25A) Create a class Clock that contains integer data members as


hours, minutes and seconds. Write a C++ program to perform following
member functions:
void setclock(int, int, int ) to set the initial time of clock
object.
void showclock() to display the time in hh:min:sec format.
Write a function tick( ) which by default increment the value of
second by 1 or according to user specified second. The clock uses 24
hours format.
Q.25B) Create a class Person that contains data members as
Person_Name, City, Mob_No. Write a C++ program to perform following
functions:
i. To accept and display Person information
ii. To search the mobile number of a given person
iii. To search the Person details of a given mobile number
(Use Function Overloading)
25A)
#include<iostream.h>
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<conio.h>
class Clock
{
int h,m,s,sec;
public :
void setclock(int hr,int min,int sec)
{
h=hr;
m=min;
s=sec;
}
void showclock()
{
cout<<"\n"<<h<<":"<<m<<":"<<s;
}
void tick()
{
cout<<"\n Enter sec to be incremented : ";
cin>>sec;
s=s+sec;
if(s>=60)
{ m=m+s/60;
s=s%60;
if(m>=60)
{
h=h+m/60;
m=m%60;
}

}
}
};

void main()
{
Clock c;
int hr,min,sec;
cout<<"\n Enter hr : ";
cin>>hr;
cout<<"\n Enter min : ";
cin>>min;
cout<<"\n Enter sec : ";
cin>>sec;
c.setclock(hr,min,sec);
c.showclock();
c.tick();
c.showclock();
getch();
}

25B)
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

#include<conio.h>
#include<iostream.h>
#include<string.h>
class person
{
char name[10],city[10],mno[12];
public:
void accept()
{
cout<<"\n Enter name : ";
cin>>name;
cout<<"\n Enter city : ";
cin>>city;
cout<<"\n Enter mob no : ";
cin>>mno;
}

void display()
{
cout<<"\n Name of person = "<<name;
cout<<"\n city = "<<city<<"\n mobile no = "<<mno;
cout<<"\n=======================================";
}

int display(char a[])


{
if(strcmp(name,a)==0)
{ cout<<"mno="<<mno;
return 1;
}
return 0;
}

int display(char mbno[],int no)


{
if(strcmp(mno,mbno)==0)
{
display();
return 1;
}
return 0;
}

};

void main()
{
int n,i,cnt=0,ans;
char sname[20],mbno[12];
person ob[20];
clrscr();
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<"\n Enter no of persons : ";


cin>>n;
for(i=0;i<n;i++)
{
ob[i].accept();
}

for(i=0;i<n;i++)
{
ob[i].display();
}

cout<<"\n Enter name of person to be search : ";


cin>>sname;
for(i=0;i<n;i++)
{
ans=ob[i].display(sname);
if(ans==1)
cnt++;
}
if(cnt==0)
cout<<"\n person not found\n ";

cout<<"\n Enter mobile no to be search : ";


cin>>mbno;
cnt=0,ans=0;
for(i=0;i<n;i++)
{
ans=ob[i].display(mbno,1);
if(ans==1)
cnt++;
}
if(cnt==0)
cout<<"\n person not found\n";

getch();
}

Q.27A) Write a class sales (Salesmam_Name, Product_name,


Sales_Quantity, Target). Each salesman deals with a separate product
and is assigned a target for a month. At the end of the month his
monthly sales is compared with target and commission is calculated
as follows:

● If Sales_Quantity > target then commission is 25% of extra


sales made + 10% of target
● If Sales_Quantity ==target then commission is 10% of
target.
● Otherwise commission is zero
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

Display the salesman information along with commission obtained.


(Use array of objects)
Q.27B) Create a class MyString which contains a character pointer
(Use new and delete operator). Write a C++ program to overload
following operators:
! To change the case of each alphabet from given string
[ ] To print a character present at specified index

27A)
#include<conio.h>
#include<iostream.h>
class sales
{
char sname[20],pname[20];
int s_qty,t;
float com;
public :
void getdata()
{
cout<<"\n Enter Salesmam_Name : ";
cin>>sname;
cout<<"\n Enter Product Name : ";
cin>>pname;
cout<<"\n Enter sales Quantity : ";
cin>>s_qty;
cout<<"\n Enter target : ";
cin>>t;
}
void cal()
{
if(s_qty>t)
com=(s_qty+(s_qty*0.25))+(
t+(t*0.10));
else if(s_qty==t)
com=t+(t*0.10);
else
com=0;

}
void display()
{
cout<<"\n Salesmam_Name : "<<sname;
cout<<"\n Product Name : "<<pname;
cout<<"\n Sales Quantity : "<<s_qty;
cout<<"\n Target : "<<t;
cout<<"\n Monthly comission is : "<<com;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

cout<<"\n
========================================";
}

};

void main()
{
sales s[10];int i,n;
cout<<"\n Enter how many Salesman : ";
cin>>n;
for(i=0;i<n;i++)
{ s[i].getdata();
s[i].cal();
}

for(i=0;i<n;i++)
{ s[i].display();

}
getch();
}

27B)
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class mystring
{
char *str;
int len;
public:
mystring()
{
}
mystring(char s[])
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
}
void display()
{
cout<<"\nstring = "<<str;
}
void operator !()
{
int i;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

for(i=0;str[i]!='\0';i++)
{
if(islower(str[i]))
{
str[i]=toupper(str[i]);
}
else
{
str[i]=tolower(str[i]);
}
}
}
char operator [](int &index)
{
if(index>0 && index<len)
return str[index];
else
return '_';
}
};
void main()
{
clrscr();
int i;
char str[30];
cout<<"\n Enter string : ";
cin>>str;
mystring m(str);
m.display();
!m;
m.display();
cout<<"\n Enter index : ";
cin>>i;
char ch = m[i];
if(ch!='_')
cout<<"\n Chracter at given position "<<ch;
else
cout<<"\n Invalid index ";
getch();
}

Q.30A) Write a C++ program to sort integer and float array elements
in ascending order by using function overloading.

Q.30B) Create a class Fraction containing data members as Numerator


and Denominator.
Write a C++ program to overload operators ++ , -- and * to increment ,
decrement a Fraction and multiply two Fraction respectively. (Use
constructor to initialize values of an object).
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

30A)
#include<iostream.h>
#include<conio.h>
class sorting
{
public:
double a[10];
int n,i;
- public:
void sort(int [],int);
void sort(float[],int);
};

void sorting::sort(float a[],int n)


{
float temp;
for(i=n-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{

if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1
];
a[j+1]=tem
p;
}//if
}//2nd for
} //1st for
}

void sorting::sort(int a[],int n)


{
int temp;
for(i=n-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1
];
a[j+1]=tem
p;
}
}
}
}
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

void main()
{
int n,i;
int a[10];float a1[10];
sorting s1;
clrscr();
cout<<"\nEnter how many element :";
cin>>n;
for(i=0;i<n;i++)
{ cout<<"\nEnter element";
cin>>a[i];
}
s1.sort(a,n);
cout<<"\Sorted element For integer array: \n";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}

cout<<"\nEnter how many element :";


cin>>n;

for( i=0;i<n;i++)
{ cout<<"\n Enter element :";
cin>>a1[i];
}
s1.sort(a1,n);
cout<<"\Sorted element For float array: \n";
for(i=0;i<n;i++)
{
cout<<"\t"<<a1[i];
}
getch();
}

30B)
#include<iostream.h>
#include<conio.h>
class fraction
{
public:
int num,den;
fraction()
{
num=0;
den=0;
}
fraction(int n,int d)
{
num=n;
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

den=d;
}
void display()
{
cout<<num<<"/"<<den;
cout<<endl;
}
fraction operator++() //pre increment
{
num=den+num;
den=den;
return *this;
}
fraction operator--() //pre decrement
{
num=den-num;
den=den;
return *this;
}
fraction operator++(int) //post increment
{
fraction temp=*this;
num=num+den;
den=den;
return temp;
}
fraction operator--(int) //post decrement
{
fraction temp=*this;
num=num-den;
den=den;
return temp;
}
friend fraction operator*(fraction,fraction);
};
fraction operator*(fraction f1,fraction f2)
{
fraction temp;
temp.num=f1.num*f2.num;
temp.den=f1.den*f2.den;
return temp;
}
void main()
{
clrscr();
fraction f1(5,10),f2(15,20),f4(25,30),f5(35,40);
f1.display();
f2.display();
fraction f3=f1*f2;
cout<<"multiplication of fraction is";
f3.display();
cout<<"pre increment is";
NITIN KUMAR
SYBBA(CA)
CPP PROGRAMS

fraction f6=++f1;
f6.display();
cout<<"post increment is";
fraction f7=f2++;
f7.display();
cout<<"pre decrement is";
fraction f8=--f4;
f8.display();
cout<<"post decrement is";
fraction f9=f5--;
f9.display();
getch();
}

S-ar putea să vă placă și