Cpp Slip Solution-1
Cpp Slip Solution-1
Slip1
Q1:
1. Write a C++ program to check maximum and minimum of two integer numbers. (Use Inline
function and Conditional operator) [15 Marks]
#include<iostream.h>
#include<conio.h>
class maxin
{
public:
inline int maximum(int a, int b)
{
return a>b?a:b;
}
inline int minimum(int a,int b)
{
return a<b?a:b;
}
};
void main()
{
int a,b;
maxin m;
clrscr();
cout<<"Enter Two numbers :\n";
cin>>a>>b;
cout<<"\n Number 1 is : "<<a<<endl;
cout<<"\n Number 2 is :"<<b<<endl;
cout<<"\n\nMaximum Number is : "<<m.maximum(a,b)<<endl;
cout<<"\nMinimum Number is: "<<m.minimum(a,b)<<endl;
getch();
}
Q 2: Create a C++ class MyFile with data members file pointer and filename. Write
necessary member functions to accept and display File. Overload the following operators:
operator example purpose + f3=f1+f2 concatenation of file f1 and file f2 ! !f3 changes case
of alternate character of file f3. [25 Marks]
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#define MAXSIZE (10)
class myfile
{
FILE *fp;
char fn[MAXSIZE];
public:
myfile(const char *fname)
{
strcpy(fn, fname);
}
myfile operator+(myfile);
void operator!();
void display();
};
void myfile::display()
{
fp = fopen(fn, "r");
char ch;
while ((ch = fgetc(fp)) != EOF)
{
cout << ch;
}
fclose(fp);
}
void myfile::operator!()
{
myfile f4("sy.txt");
char ch;
fp = fopen(fn, "r");
f4.fp = fopen(f4.fn, "w");
while ((ch = fgetc(fp)) != EOF)
{
if (isupper(ch))
fputc(tolower(ch), f4.fp);
else if (islower(ch))
fputc(toupper(ch), f4.fp);
else
fputc(ch, f4.fp);
}
fclose(fp);
fclose(f4.fp);
remove("abc.txt");
rename("sy.txt", "abc.txt");
}
myfile myfile::operator+(myfile f2)
{
myfile f3("abc.txt");
fp = fopen(fn, "r");
f2.fp = fopen(f2.fn, "r");
f3.fp = fopen(f3.fn, "w");
char ch;
while ((ch = fgetc(fp)) != EOF)
{
fputc(ch, f3.fp);
}
fclose(fp);
while ((ch = fgetc(f2.fp)) != EOF)
{
fputc(ch, f3.fp);
}
fcloseall();
return f3;
}
int main()
{
myfile f1("xyz.txt");
myfile f2("lmn.txt");
myfile f3("abc.txt");
return 0;
}
Slip2
Q 1:
1. Write a C++ program to find volume of cylinder, cone and sphere.(Use function
overloading).[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<math.h>
float volume(int,int); //cylinder
float volume(float,float); //cone
float volume(float); //sphere
int main()
{
int cylinder_h,cylinder_r,cone_h,cone_r;
float sphere_r;
clrscr();
cout<<"Enter Dimension"<<endl;
cout<<"1. Cylinder"<<endl;
cout<<"Height : ";
cin>>cylinder_h;
cout<<"Radius : ";
cin>>cylinder_r;
cout<<endl;
cout<<"2. Cone"<<endl;
cout<<"Height : ";
cin>>cone_h;
cout<<"Radius : ";
cin>>cone_r;
cout<<endl;
cout<<"3. Sphere"<<endl;
cout<<"Radius : ";
cin>>sphere_r;
cout<<endl;
float volume(float r)
{
return(0.33*3.14*r*r*r);
}
2.Write a C++ program to create a class Movie with data members M Name, Release Year,
Director Name and Budget. (Use File Handling) Write necessary member functions to: i.
Accept details for ‘n’ Movies from user and store it in a file “Movie.txt . ii.Display Movie
details from a file. iii. Count the number of objects stored in a file. [25 Marks]
/* Create movie.txt file for storing movie details*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class movie
{
public:
int year;
char mname[20];
char dname[20];
int budget;
public:
void accept()
{
cout<<"\n\nEnter Movie Name : ";
cin>>mname;
cout<<"Enter Release Year : ";
cin>>year;
cout<<"Enter Director Name : ";
cin>>dname;
cout<<"Enter Budget : ";
cin>>budget;
}
void display()
{
cout<<"\n\nThe Movie Name : "<<mname<<endl;
cout<<"The Release Year : "<<year<<endl;
cout<<"The Director Name : "<<dname<<endl;
cout<<"The Budget : "<<budget<<endl;
}
};
void main()
{
movie m[5];
int n,i;
clrscr();
fstream file;
file.open("movie.txt",ios::in | ios::out);
swap(a,b);
cout<<"\nOutside Function after swapping value is : ";
cout<<"\n\tA = " << a << "\n\tB = " << b << endl;
getch();
return 0;
}
2.Write a C++ program to accept ‘n’ numbers from user through Command Line
Argument. Store all Even and Odd numbers in file “Even.txt” and “Odd.txt”
respectively. [25 Marks]
/*In this program we will create also 2 text file like odd.txt for storing odd numbers and even.txt
for storing even numbers*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<fstream.h>
#include<iostream.h>
int main(int argc, char *argv[])
{
int num;
ofstream f1("even.txt"),f2("odd.txt");
clrscr();
if (argc == 1)
printf("No command line arguments found.\n");
else
{
for(int i = 1; i < argc; i++)
{
num = atoi(argv[i]);
if(num%2==0)
{
f1<<num;
}
else
{
f2<<num;
}
}
}
getch();
return 0;
}
Slip4 A:1.Write a C++ program accept Worker information Worker Name, No of Hours
worked, Pay Rate and Salary. Write necessary functions to calculate and display the salary
of Worker.(Use default value for Pay_Rate)[15 Marks]
#include<iostream.h>
#include<conio.h>
class WorkerInformation
{
char Worker_Name[50];
int No_Of_Hours_Worked, Salary;
public:
void acccept()
{
cout << "Enter name of the worker: ";
cin >> Worker_Name;
cout << "Enter number of hours worked: ";
cin >> No_Of_Hours_Worked;
}
void display()
{
cout << endl << "Worker details" << endl;
cout << "Name: " << Worker_Name << endl;
cout << "Salary: " << calSal(No_Of_Hours_Worked) << endl;
}
int calSal(int work_hrs, int pay_rate=100)
{
return (work_hrs*pay_rate);
}
};
int main()
{
WorkerInformation w;
w.acccept();
w.display();
return 0;
}
Slip 4 - B) Write a C++ program to create a base class Employee (Emp-code, name, salary).
Derive two classes as Fulltime (daily_wages, number_of_days) and Parttime
(number_of_workinghours, hourly_wages). Write a menu driven program to perform
following functions: 1. Accept the details of ‘n’? Employees and calculate the salary. 2.
Display the details of ‘n’ employees. 3. Display the details of employee having maximum
salary for both types of employees.
#include<iostream.h>
#include<conio.h>
const int m=50;
class emp
{
public:
int empcode;
char empname[30];
int salary;
public:
void get()
{
cout<<"Enter Employee No. : ";
cin>>empcode;
cout<<"Enter Employee Name : ";
cin>>empname;
}
};
class fulltime:public emp
{
public:
float daily_wages;
int no_of_days;
public:
void getdata()
{
cout<<"Enter Daily Rate : ";
cin>>daily_wages;
cout<<"Enter No. Of Days : ";
cin>>no_of_days;
}
void cal()
{
salary=daily_wages*no_of_days;
cout<<"\nSalary : " <<salary;
}
void show()
{
cout<<"\n\nEmployee Number : "<<empcode;
cout<<"\nEmployee Name : "<<empname;
cout<<"\nSalary : " <<salary;
cout<<"\nStatus : Full Time";
}
};
class parttime:public emp
{
public:
int hourly_wages;
int working_hours;
public:
void get1()
{
cout<<"\nEnter Hourly Rate : ";
cin>>hourly_wages;
cout<<"\nEnter Working Hours : ";
cin>>working_hours;
}
void call()
{
salary=hourly_wages*working_hours;
cout<<"\nSalary : " <<salary;
}
void show1()
{
cout<<"\n\nEmployee No : "<<empcode;
cout<<"\nEmployee Name : "<<empname;
cout<<"\nSalary : "<<salary;
cout<<"\nStatus : Part Time";
}
};
int main()
{
int const cnt=5;
int var=0;
int var1=0;
fulltime f1[cnt];
parttime p1[cnt];
int ch,i;
clrscr();
do
{
cout<<"\n1.Enter Record";
cout<<"\n2.Display Record";
cout<<"\n3.search Record";
cout<<"\n4.Quit";
cout<<"\n\nEnter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
int y;
cout<<"1.Fulltime Employee";
cout<<"2.Parttime Employee";
cout<<"\nEnter Choice : ";
cin>>y;
switch(y)
{
case 1:
f1[var].get();
f1[var].getdata();
f1[var].cal();
var++;
break;
case 2:
p1[var1].get();
p1[var1].get1();
p1[var1].call();
var1++;
break;
}
case 2:
for(i=0;i<var;i++)
{
f1[i].show();
}
for(i=0;i<var1;i++)
{
p1[var1].show1();
}
break;
case 3:
int a;
cout<<"\nEnter Employee No : ";
cin>>a;
for(i=0;i<var;i++)
{
if(f1[i].empcode==a)
{
f1[i].show();
}
if(p1[i].empcode==a)
{
p1[i].show1();
}
}
}
}while(ch!=4);
getch();
return 0;
}
Slip5:
1.Consider the following C++ class
class Point
{
int x,y;
public:
void setpoint(int,int); //set values of x and y co-ordinates
void showpoint(); // display co-ordinate in point p in format(x,y)
}[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class point
{
int x,y;
public:
void setpoint(int a,int b)
{
x=a;
y=b;
}
void showpoint()
{
cout<<"("<<x<<","<<y<<")";
}
};
int main()
{
int a,b;
clrscr();
point p;
2.Create a C++ base class Shape. Derive three different classes Circle,Sphere and Cylinder
from shape class. Write a C++ program to calculate area of Circle, Sphere and Cylinder.
(Use pure virtual function). [25 Marks]
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void area()=0;
};
int main()
{
clrscr();
circle c;
c.area();
cout<<endl;
sphere s;
s.area();
cout<<endl;
cylinder cyli;
cyli.area();
cout<<endl;
getch();
return 0;
}
Slip6
1.Write a C++ program create two Classes Square and Rectangle. Compare area of both
the shapes using friend function. Accept appropriate data members for both the classes.[15
Marks]
#include<iostream.h>
#include<conio.h>
class sqaure
{
public:
int s;
void getdata()
{
cout<<"Enter side of square : ";
cin>>s;
}
int calarea()
{
return(s*s);
}
friend void compare(int s,int r);
};
class rectangle
{
public:
int l,w;
void getdata()
{
cout<<"Enter Length of Rectangle : ";
cin>>l;
cout<<"Enter Breadth of Rectangle : ";
cin>>w;
}
int calarea()
{
return(l*w);
}
int main()
{
int s_area,r_area;
clrscr();
sqaure s;
rectangle r;
s.getdata();
s_area=s.calarea();
r.getdata();
r_area=r.calarea();
compare(s_area,r_area);
getch();
return 0;
}
#include<iostream>
#include<conio.h>
class matrix
{
int **a,i,j,r,c;
public:
matrix() // Dynamic Constructor
{
cout<<"Enter Row and Coloum of Matrix :\n";
cin>>r>>c;
a=new int*[r];
for(i=0; i<r; i++)
{
a[i]=new int[c];
}
}
void accept()
{
cout<<"Enter elements for matrix:\n";
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
cin>>a[i][j];
}
}
}
void display()
{
cout<<"Elements of matrix are:\n";
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
void transpose()
{
cout<<"Transpose of Matrix is :\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
}
};
int main()
{
matrix obj1;
obj1.accept();
obj1.display();
obj1.transpose();
}
Slip7
1.Write a C++ program using class with data member char str[50] and function replace
(char chl, char ch2) every occurrence of chl in str should be replaced with ch2 and return
number of replacement it makes use default value for char ch2. (Use ch2 as Default
Argument)[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.h>
class mystr
{
public:
int replace(char *str,char,char);
};
}
str++;
i++;
}
str=str-i;
return n;
}
int main()
{
mystr m;
char *str,c1,c2,a;
clrscr();
cout<<"Enter String : ";
cin>>str;
void display() {
int i,j;
cout << "\nThe first vector is: (";
for ( i = 0; i < n - 1; i++) {
cout << a[i] << ", ";
}
if (n > 0) {
cout << a[n - 1] << ")";
}
cout << "\nThe second vector is: (";
for ( j = 0; j < n1 - 1; j++) {
cout << b[j] << ", ";
}
if (n1 > 0) {
cout << b[n1 - 1] << ")";
}
}
void calculateUnion() {
int i,j,k,bool,found,true,false;
int *unionArray = new int[n + n1];
int unionSize = 0;
~Vector() {
delete[] a;
delete[] b;
}
};
int main() {
Vector v;
int choice;
do {
cout << "\n1. Accept vectors";
cout << "\n2. Display vectors";
cout << "\n3. Calculate union";
cout << "\n4. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
v.create();
break;
case 2:
v.display();
break;
case 3:
v.calculateUnion();
break;
case 4:
cout << "Exiting...";
break;
default:
cout << "Invalid choice!";
}
} while (choice != 4);
return 0;
}
Slip8
1.Write a C++ program create a class Number, which contain static data member ‘cnt’ and
member function ‘Display()’. Display() should print number of times display operation is
performed irrespective of the object responsible for calling Display().[15 Marks]
#include<iostream.h>
#include<conio.h>
class number
{
public:
void display()
{
static int cnt=1;
cout<<"\nDisplay Function is called "<< cnt << " times " <<endl;
cnt++;
}
};
int main()
{
number n1,n2;
clrscr();
n1.display();
n1.display();
n2.display();
n2.display();
getch();
return 0;
}
2.Create a C++ class Person with data members Person name, Mobile number, Age, City.
Write necessary member functions for the following:
i. Search the mobile number of given person.
ii. Search the person name of given mobile number.
iii. Search all person details of given city.
(Use function overloading)[25 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{
public:
int mob;
char name[10],city[20];
void acc() //function overloading
{
cout<<"Enter Person Name : ";
cin>>name;
cout<<"Enter Person City : ";
cin>>city;
cout<<"Enter Person Mobile Number : ";
cin>>mob;
}
void acc(char nme[]) //function overloading
{
if(strcmp(nme,name)==0)
{
cout<<"\nPerson Name : "<<name;
cout<<"\nPerson Mobile Number : "<<mob<<endl;
}
}
void dis()
{
cout<<"\nPerson Details :"<<endl;
cout<<"\n\nPerson Name : "<<name;
cout<<"\nPerson Mobile Number : "<<mob;
cout<<"\nPerson City : "<<city;
}
};
int main()
{
char name[10];
int mno,i,ch,no;
clrscr();
person p[20];
do
{
cout<<"\n1.Accept Person Details";
cout<<"\n2.Display Person Details";
cout<<"\n3.To Search mobile number of a given person";
cout<<"\n4.To Search Person details of a given mobile number";
cout<<"\n5.Exit";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter how many record to insert : ";
cin>>no;
for(i=0;i<no;i++)
{
p[i].acc();
}
break;
case 2:
for(i=0;i<no;i++)
{
p[i].dis();
}
break;
case 3:
cout<<"Enter person name search for mobile number : ";
cin>>name;
for(i=0;i<no;i++)
{
p[i].acc(name);
}
break;
case 4:
cout<<"Enter mobile number to search for person name : ";
cin>>mno;
for(i=0;i<no;i++)
{
p[i].acc(mno);
}
break;
}
}while(ch!=5);
getch();
return 0;
}
Slip9
1.Consider the following C++ class class Person { char Name [20]; charAddr [30]; float
Salary; float tax amount; public: // member functions };
Calculate tax amount by checking salary of a person
For salary <=20000 tax rate=0
For salary>20000|| <=40000 tax rate =5% of salary.
For salary>40000 tax rate=10% of salary[15 Marks]
#include<iostream.h>
#include<conio.h>
class person
{
char name[20];
char addr[20];
float sal,tax;
public:
void get()
{
cout<<"Enter Name : ";
cin>>name;
cout<<"Enter Address : ";
cin>>addr;
cout<<"Enter Salary : ";
cin>>sal;
}
void put()
{
cout<<"\n\n-----Person information-----";
cout<<"\nPerson Name : "<<name;
cout<<"\nPerson Address : "<<addr;
cout<<"\nPerson Salary : "<<sal;
cout<<"\nTax is : "<<tax;
}
void cal_tax()
{
if(sal<=20000)
{
tax=0;
}
else if(sal>20000 || sal<=40000)
{
tax=(sal*5)/100;
}
else if(sal>40000)
{
tax=(sal*10)/100;
}
}
};
void main()
{
person p;
clrscr();
p.get();
p.cal_tax();
p.put();
getch();
}
2.Create a C++ class Time with data members Hours, Minutes and Seconds.
Write necessary member functions for the following: (Use Objects as arguments)
1. To accept a time.
2. To display a time In format hh:mm:ss.
3. To find difference between two time and display it in format hh:mm:ss.[25 Marks]
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class time
{
int h,m,s;
public:
void get();
void display();
time operator -(time t2);
};
void time::get()
{
cout<<"\nEnter Hour, Minutes and Seconds : ";
cin>>h>>m>>s;
}
void time::display()
{
cout<<"\n----Time is----"<<setfill('0')<<setw(2)<<h;
cout<<":"<<setfill('0')<<setw(2)<<m;
cout<<":"<<setfill('0')<<setw(2)<<s<<endl;
}
void main()
{
clrscr();
time t1,t2,t3;
t1.get();
t1.display();
t2.get();
t2.display();
t3=t1-t2;
cout<<"\nTime1-Time2:\n";
t3.display();
getch();
}
Slip10
1.Write a C++ program to create a class Account with data members Acc number, Acc type
and Balance.
Write member functions to accept and display ‘n’ account details. (Use dynamic memory
allocation)[15 Marks]
#include<iostream.h>
#include<conio.h>
class account
{
public:
int acc_no,balance;
char acc_type[30];
public:
account()
{
cout<<"Constructor"<<endl;
}
~account()
{
cout<<"Destructor"<<endl;
}
void get()
{
cout<<"\nEnter Account no : ";
cin>>acc_no;
cout<<"Enter Account Type : ";
cin>>acc_type;
cout<<"Enter Balance : ";
cin>>balance;
}
void display()
{
cout<<"\n\nAccount No : "<<acc_no;
cout<<"\nAccount Type : "<<acc_type;
cout<<"\nBalance : "<<balance;
}
};
int main()
{
clrscr();
int num,i;
account *a=new account[4];
delete []a; //delete array
cout<<"\nHow many Records You Want : ";
cin>>num;
for(i=0;i<num;i++)
{
a[i].get();
}
for(i=0;i<num;i++)
{
a[i].display();
}
getch();
return 0;
}
2.Create a C++ class City with data members City code, City name, population. Write
necessary member functions for the following:
i.Accept details of n cities
ii. Display details of n cities in ascending order of population.
iii. Display details of a particular city.
(Use Array of object and to display city information use manipulators.)[25 Marks]
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
void searchcity();
char n[10],c[10];
class city
{
public:
int population,city_code;
char name[40],e[10];
void accept()
{
cout<<"\nEnter name of city : ";
cin>>name;
cout<<"Enter city code : ";
cin>>city_code;
cout<<"Enter Population of city : ";
cin>>population;
}
void display()
{
cout<<"\n\nName of city : "<<name<<endl;
cout<<"Population : "<<population<<endl;
cout<<"City code : "<<city_code;
}
void searchcity()
{
if(strcmp(name,c)==0)
{
cout<<"\n\nName : "<<name;
cout<<"\nPopulation : "<<population;
}
}
};
void main()
{
clrscr();
city t[30];
int num,ch,population,i,j;
char cont;
cout<<"\n1.Accept and Display";
cout<<"\n2.Ascending";
cout<<"\n3.Search by City";
do
{
cout<<"\nEnter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nHow many records you want to insert : ";
cin>>num;
for(i=0;i<num;i++)
{
t[i].accept();
}
for(i=0;i<num;i++)
{
t[i].display();
}
break;
case 2:
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
t[i].sort(t[i],t[j]);
}
t[i].display();
}
break;
case 3:
cout<<"\nEnter city name : ";
cin>>c;
for(i=0;i<num;i++)
{
t[i].searchcity();
}
break;
}
class MyArray
{
int size,*ptr,*p;
public:
MyArray(int no)
{
size=no;
int i;
ptr=new int[size];
for(i=0;i<size;i++)
{
cout<<"\nEnter elements : \n";
cin>>ptr[i];
}
}
void display()
{
int i;
cout<<"\nElements are :\n";
for(i=0;i<size;i++)
{
cout<<ptr[i]<<"\t";
}
void calculatesum()
{
int arr[10];
int i,sum=0;
cout<<"\nEnter 10 array elements : \n";
for(i=0;i<10;i++)
{
cin>>arr[i];
sum=sum+arr[i];
}
cout<<"\nThe array elements are :\n";
for(i=0;i<10;i++)
{
cout<<arr[i]<<"\t";
}
cout<<"\n\nSum of all elements are : "<<sum;
}
~MyArray()
{
delete ptr;
}
};
void main()
{
int n;
clrscr();
cout<<"\nEnter size : ";
cin>>n;
MyArray d(n);
d.display();
d.calculatesum();
getch();
}
2 . Create a base class Student with data members Roll No, Name. Derives two classes from
it, class Theory with data members Ml, M2, M3, M4 and class Practical with data members
PI, P2. Class Result(Total Marks, Percentage, Grade) inherits both Theory and Practical
classes.
(Use concept of Virtual Base Class and protected access specifiers)
Write a C++ menu driven program to perform the following functions:
i. Accept Student Information
ii. Display Student Information
iii. Calculate Total marks, Percentage and Grade.[25 Marks]
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
class student
{
protected :
int rno;
char name[20];
public:
void getdetails();
};
class theory:public virtual student
{
protected:
int mark1,mark2,mark3,mark4;
public:
void getmarks();
};
class practical:public virtual student
{
protected:
int p1,p2;
public:
void getpractical();
};
public:
void cal();
void display();
};
void student::getdetails()
{
cout<<"\nEnter Roll no and Name :\n ";
cin>>rno>>name;
}
void theory::getmarks()
{
cout<<"\nEnter marks of 4 subject : \n";
cin>>mark1>>mark2>>mark3>>mark4;
}
void practical::getpractical()
{
cout<<"\nEnter practical details : \n";
cin>>p1>>p2;
}
void result::cal()
{
int i;
total_marks=mark1+mark2+mark3+mark4+p1+p2;
per=total_marks/6;
if(per<50)
strcpy(grade,"C");
else if(per<60)
strcpy(grade,"B");
else if(per<75)
strcpy(grade,"A");
else
strcpy(grade,"A+");
cout<<"\nCalculation completed..";
}
void result::display()
{
cout<<"\n\nRoll no = "<<rno<<"\nName = "<<name;
cout<<"\n\nTheory Marks : \n"<<"Marks 1 = "<<mark1<<"\n"<<"Marks 2 =
"<<mark2<<"\nMarks 3 = "<<mark3<<"\nMarks 4 = "<<mark4;
cout<<"\n\nPractcal : \n"<<"Practical p1 = "<<p1<<"\nPractical p2 = "<<p2;
cout<<"\n\nPercentage = "<<per;
cout<<"\nGrade = "<<grade;
}
int main()
{
int n,i,ch,j;
result r[40];
clrscr();
do
{
cout<<"\n1.Accept student Information";
cout<<"\n2.Display student Infromation";
cout<<"\n3.Calculate percenatge and grade";
cout<<"\n4.Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter Number of student : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter student details :\n";
r[i].getdetails();
r[i].getmarks();
r[i].getpractical();
}
break;
case 2:
for(i=0;i<n;i++)
{
r[i].display();
}
break;
case 3:
for(i=0;i<n;i++)
{
r[i].cal();
}
break;
case 4:
exit(0);
}
}while(ch<=4);
getch();
return 0;
}
Slip12:
1 .Write a C++ program to create a class Date with data members day, month, and year.
Use default and parameterized constructor to initialize date and display date in dd-Mon-
yyyy format. (Example: Input: 04-01-2021 Output: 04-Jan-2021)[15 Marks]
#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<<"\n---Given Date---\n";
cout<<dd<<"-"<<mm<<"-"<<yy;
cout<<"\n\nAfter formating date is :";
switch(mm)
{
case 1:
cout<<"\n"<<dd<<"-Jan-"<<yy;
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<<"\nInvalid month";
}
}
};
int main()
{
int m,dt,y;
clrscr();
cout<<"\nEnter date : ";
cin>>dt;
cout<<"\nEnter month : ";
cin>>m;
cout<<"\nEnter year : ";
cin>>y;
date d(dt,m,y);
d.display();
getch();
return 0;
}
2.Create a C++ class Weight with data members kilogram, gram. Write a C++ program
using operator overloading to perform following functions:
i. To accept weight.
ii. To display weight in kilogram and gram format.
iii. Overload +— operator to add two weights.
iv. Overload +— operator to check equality of two weights [25 Marks]
#include<iostream.h>
#include<conio.h>
class weight
{
int kilogram,gram;
public:
void getdata()
{
cout<<"\nEnter kilogram : ";
cin>>kilogram;
cout<<"\nEnter gram : ";
cin>>gram;
}
void display()
{
cout<<"\nAddition is : ";
cout<<kilogram<<"."<<gram;
}
void display2()
{
cout<<"\n"<<kilogram<<"."<<gram;
}
void main()
{
weight c1,c2,c3,c4,c5;
clrscr();
c1.getdata();
c2.getdata();
c3=c1+=c2;
c3.display();
c4.getdata();
c5.getdata();
if(c4==c5)
{
cout<<"\n";
c4.display2();
c5.display2();
cout<<"\nBoth are same";
}
else
{
cout<<"\n";
c5.display2();
c4.display2();
cout<<"\nBoth are not same";
}
getch();
}
Slip13:
1 .Write a C++ program to create a class Product with data members Product id, Product
Name, Qty, Price. Write member functions to accept and display Product information also
display number of objects created for Product class. (Use Static data member and Static
member function)[15 Marks]
#include<iostream.h>
#include<conio.h>
class product
{
int id,price,qty;
char i_name[20];
static int cnt;
public:
void getdata()
{
cout<<"\nEnter Producr Id : ";
cin>>id;
cout<<"Enter Product Name : ";
cin>>i_name;
cout<<"Enter Product Price : ";
cin>>price;
cout<<"Entre Product Qty : ";
cin>>qty;
cnt++;
}
void display()
{
cout<<"\n\nProcut Id = "<<id;
cout<<"\nProduct Name = "<<i_name;
cout<<"\nProduct Price = "<<price;
cout<<"\nProduct Qty = "<<qty;
}
int product::cnt;
void main()
{
product ob[10];
clrscr();
int n,i;
cout<<"\nEnter No of product :";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].getdata();
}
for(i=0;i<n;i++)
{
ob[i].display();
}
ob[n-1].nob();
getch();
}
2.Create a C++ class Cuboid with data members length, breadth, and height. Write
necessary member functions for the following:
i. void setvalues(float,float,float) to set values of data members.
ii. void getvalues( ) to display values of data members.
iii. float volume( ) to calculate and return the volume of cuboid.
iv. float surface area( ) to calculate and return the surface area of cuboid.
(Use Inline function)[25 Marks]
#include<iostream.h>
#include<conio.h>
class cuboid
{
float len,bre,hei;
public:
void getdata()
{
cout<<"\nEnter Length,Breadth and Heigth of cuboid : \n";
cin>>len>>bre>>hei;
}
void setdata()
{
cout<<len<<bre<<hei;
}
void main()
{
clrscr();
cuboid c;
c.getdata();
c.volume();
c.surface_area();
getch();
}
Slip14:
1.Write a C++ program to accept radius of a Circle. Calculate and display diameter,
circumference as well as area of a Circle. (Use Inline function)[15 Marks]
#include<iostream.h>
#include<conio.h>
int main()
{
float radius;
clrscr();
cout<<"\nEnter Radius of circle : ";
cin>>radius;
cout<<"\n\nDiameter of circle : "<<diameter(radius);
cout<<"\nArea of circle : "<<circlearea(radius);
cout<<"\nCircumeference of circle : "<<circumeference(radius);
getch();
return 0;
}
2 .Create a C++ class MyString with data members a character pointer and str length. (Use
new and delete operator).
Write a C++ program using operator overloading to perform following operation:
i. To reverse the case of each alphabet from a given string.
ii. To compare length of two strings.
iii. To add constant ‘n’ to each alphabet of a string.[25 Marks]
#include <iostream.h>
#include <cstring.h>
class MyString {
private:
char *str;
int length;
public:
// Constructor
MyString(const char *s) {
length = strlen(s);
str = new char[length + 1];
strcpy(str, s);
}
// Destructor
~MyString() {
delete[] str;
}
int main() {
MyString str1("Hello World!");
MyString str2("Goodbye");
return 0;
}
Slip15:
1.Create a C++ class Fraction with data members Numerator and Denominator. Write a
C++ program to calculate and display sum of two fractions. (Use Constructor)[15 Marks]
#include<iostream.h>
#include<conio.h>
class fraction
{
int num,dnm;
public:
fraction(){}
fraction(int n,int d)
{
num=n;
dnm=d;
}
void operator+(fraction);
};
int main()
{
int n1,d1,n2,d2;
clrscr();
cout<<"\nEnter first fraction : ";
cin>>n1>>d1;
cout<<"\nsecond second fraction : ";
cin>>n2>>d2;
fraction f1(n1,d1);
fraction f2(n2,d2);
fraction f3;
f1+f2;
getch();
return 0;
}
2.Write a C++ class Seller (S Name, Product name, Sales Quantity, Target Quantity,
Month, Commission). 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:
i. If Sales Quantity>Target Quantity then commission is 25% of extra sales made + 10% of
target.
ii.If Sales Quantity = Target Quantity then commission is of target.
iii. Otherwise commission is zero.
Display salesman information along with commission obtained.
(Use array of objects)[25 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.h>
class seller
{
char sname[20],pname[20];
int squantity,target;
float commission;
public:
void get()
{
cout<<"\nEnter salesman name : ";
cin>>sname;
void put()
{
cout<<"\n\nSalesman name : "<<sname;
cout<<"\nProduct name : "<<pname;
cout<<"\nSales quantity : "<<squantity;
cout<<"\nTarget : "<<target;
commission=0;
if(squantity>target)
{
commission=commission+((squantity-target)*0.25)+(target*0.10);
}
else if(target==squantity)
{
commission=commission+(target*0.10);
}
else
{
commission=0;
}
cout<<"\nCommission : "<<commission;
}
};
void main()
{
seller sman[10];
int i,n;
clrscr();
cout<<"\nEnter how many salesman : ";
cin>>n;
for(i=0;i<n;i++)
{
sman[i].get();
}
for(i=0;i<n;i++)
{
sman[i].put();
}
getch();
}
Slip16:
1.Write a C++ program to create a class Machine with data members Machine Id,
Machine Name, Price. Create and initialize all values of Machine object by using
parameterized constructor and copy constructor.
Display details of Machine using setw( ) and setprecision( ).[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class machine
{
private:
int machine_id,price;
char name[20];
public:
//parameterized constructor
machine(int x1,int y1,char *z1)
{
strcpy(name,z1);
machine_id=x1;
price=y1;
}
//copy constructor
machine(const machine &same)
{
strcpy(name,same.name);
machine_id=same.machine_id;
price=same.price;
}
void display()
{
cout<<"\nName : "<<name;
cout<<"\nMachine id : "<<setprecision(2)<<machine_id;
cout<<"\nPrice : "<<setw(3)<<price;
}
};
int main()
{
clrscr();
machine o1(13,345,"deep"); //normal or parameterised constructor
machine o2=o1; //copy constructor
cout<<"\n\nNormal constructor : ";
o1.display();
cout<<"\n\nCopy constructor : ";
o2.display();
getch();
return 0;
}
2.Create a class Matrix Write necessary member functions for the following:
i. To accept a Matrix
ii. To display a Matrix
iii. Overload unary ‘-‘ operator to calculate transpose of a Matrix.
iv. Overload unaryoperator to increment matrix elements by I[25 Marks]
#include<iostream.h>
#include<string.h>
#include<conio.h>
class mystring
{
int a[3][3],i,j;
public:
void get();
void put();
void operator-();
void operator++();
};
void mystring::get()
{
int i,j;
cout<<"\nEnter matrix elements : \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
void mystring::put()
{
int i,j;
cout<<"\nMatrix is : \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j];
cout<<"\t";
}
}
}
void mystring::operator-()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=-a[i][j];
}
}
}
void mystring::operator++()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=++a[i][j];
}
}
}
int main()
{
mystring m1;
clrscr();
cout<<"\nEnter matrix value ";
m1.get();
m1.put();
-m1;
cout<<"\nAfter negetion matrix : \n";
m1.put();
++m1;
cout<<"\nAfter increment : \n";
m1.put();
m1++;
m1.put();
getch();
return 0;
}
Slip17:
1. Create a C++ class MyMatrix. Write C++ program to accept an display matrix.
Overload Binary “-” operator to calculate subtraction of matrix.[15 Marks]
#include<iostream.h>
#include<conio.h>
class matrix
{
int a[3][3];
public:
void get();
void put();
void operator-(matrix z);
};
void matrix::get()
{
int i,j;
cout<<"\nEnter matrix elements : \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
void matrix::put()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j];
cout<<"\t";
}
}
}
void main()
{
matrix m,n;
clrscr();
m.get();
n.get();
cout<<"\nMatrix 1 is : \n";
m.put();
cout<<"\n\nMatric 2 is : \n";
n.put();
m-n;
getch();
}
2.Design two base classes Student (S id, Name, Class) and Competition (C id, C Name).
Derive a class Stud Comp(Rank) from it. Write a menu driven program to perform
following functions:
i. Accept information.
ii. Display information.
iii. Display Student Details In the ascending order of Rank of a specified competition.
(Use array of objects)[25 Marks]
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
class student
{
public:
int roll;
char name[10],std[10];
void accept_s();
void display_s();
};
void student::accept_s()
{
cout<<"\nEnter roll no : ";
cin>>roll;
cout<<"\nEnter name of student : ";
cin>>name;
cout<<"\nEnter class of student : ";
cin>>std;
}
void student::display_s()
{
cout<<"\nStudent Details : \n";
cout<<"Roll No : "<<roll;
cout<<"\nName : "<<name;
cout<<"\nStd : "<<std;
}
class competition
{
public:
int cid;
char cname[20];
void accept_c();
void display_c();
};
void competition::accept_c()
{
cout<<"\nEnter competition id : ";
cin>>cid;
cout<<"\nEnter competition name : ";
cin>>cname;
}
void competition::display_c()
{
cout<<"\nCompetition id : "<<cid;
cout<<"\nCompetition name : "<<cname;
}
void studcomp::accept_sc()
{
cout<<"\nEnter rank : ";
cin>>rank;
}
tempr=sc[j].student::roll;
sc[j].student::roll=sc[j+1].student::roll;
sc[j+1].student::roll=tempr;
}
}
}
cout<<"\nSorted deatils : "<<endl;
for(i=0;i<num;i++)
{
cout<<"\nRank = "<<sc[i].rank<<" " <<"Roll no = "<<sc[i].student::roll<<" "<<endl;
}
}
int main()
{
int num,ch;
studcomp *sc=new studcomp[num];
clrscr();
do
{
cout<<"\n1.Accept infromation";
cout<<"\n2.Display information";
cout<<"\n3.Display information in ascending order";
cout<<"\n4.Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
int i;
case 1:
cout<<"\nEnter number of student :\n";
cin>>num;
for(i=0;i<num;i++)
{
sc[i].student::accept_s();
sc[i].competition::accept_c();
sc[i].studcomp::accept_sc();
}
break;
case 2:
for(i=0;i<num;i++)
{
sc[i].student::display_s();
sc[i].competition::display_c();
sc[i].studcomp::display_sc();
}
break;
case 3:
for(i=0;i<num;i++)
{
sort(sc,num);
}
break;
case 4:
exit(0);
}
}while(ch<=4);
getch();
return 0;
}
Slip18:
1.Create a C++ class Student with data members Roll no, S Name, Class, Percentage.
Accept two students information and display information of student having maximum
percentage.
(Use this pointer)[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class student
{
public:
int id,per;
char name[40],class1[40];
void accept()
{
cout<<"\nEnter id : ";
cin>>id;
cout<<"Enter name : ";
cin>>name;
cout<<"Enter class : ";
cin>>class1;
cout<<"Enter per : ";
cin>>per;
}
void display()
{
cout<<"\n\nStudent id = "<<this->id;
cout<<"\nStudent name = "<<this->name;
cout<<"\nStudent class = "<<this->class1;
cout<<"\nStudent per = "<<this->per;
}
};
void main()
{
clrscr();
student s[20];
int num,ch,per,i,temp=0;
do
{
cout<<"\n1.Accept information";
cout<<"\n2.Display information";
cout<<"\n3.Maximum percentage";
cout<<"\n4.Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter no of student : ";
cin>>num;
for(i=0;i<num;i++)
{
s[i].accept();
}
break;
case 2:
for(i=0;i<num;i++)
{
s[i].display();
}
break;
case 3:
for(i=0;i<1;i++)
{
if(s[temp].per<s[i].per)
{
temp=i;
}
cout<<"\n\nStudent with highest percentage is : "<<s[temp].per;
cout<<"\nStudent name is : "<<s[temp].name<<endl;
}
break;
case 4:
exit(0);
}
}while(ch<=4);
getch();
}
2.Create a C++ class MyArray with data members
– int *arr
– int size
Write necessary member functions to accept and display Array elements. Overload the
following operators:
operator example purpose
-(Unary) -A1 Reverse Array elements
+(Binary) A2=A1+n Add constant a to all array elements
[25 Marks]
#include <iostream.h>
#include<conio.h>
class MyArray {
private:
int *arr;
int size;
public:
// Constructor
MyArray(int s) : size(s) {
arr = new int[size];
}
// Destructor
~MyArray() {
delete[] arr;
}
int main() {
int size;
cout << "Enter size of the array: ";
cin >> size;
MyArray arr(size);
arr.acceptElements();
class distance
{
int meter;
float centimeter;
public:
void get()
{
cout<<"\nEnter meter value : ";
cin>>meter;
cout<<"Enter centimeter : ";
cin>>centimeter;
}
void put()
{
cout<<"\nMeter = "<<this->meter<<endl;
cout<<"Centimeter = "<<this->centimeter;
}
int main()
{
distance d1,d2,d3;
clrscr();
cout<<"\nEnter First distance : ";
d1.get();
cout<<"\nEnter Second distance : ";
d2.get();
d3=d3.larger(d1,d2);
cout<<"\n\nLarger Distance is : \n";
d3.put();
getch();
return 0;
}
2. Create a C++ base class Media. Derive two different classes from it, class NewsPaper
with data members N Name, N Editor, N Price, No of Pages and class Magazine with data
members M Name, M Editor, M Price. Write a C++ program to accept and display
information of both NewsPaper and Magazine.
(Use pure virtual function)[25 Marks]
#include<iostream.h>
#include<conio.h>
class media
{
public:
virtual void display()=0;
};
void newspaper::display()
{
cout<<"\n\nNo_of_pages : "<<no_of_pages;
cout<<"\nNewspaper price : "<<n_price;
cout<<"\nNewspaper name : "<<n_name;
cout<<"\nNewspaper editor : "<<n_editor;
}
void magazine::display()
{
cout<<"\n\nMagazine name : "<<m_name;
cout<<"\nMagazine editor : "<<m_editor;
cout<<"\nMagazine price : "<<m_price;
}
void main()
{
clrscr();
newspaper n;
n.getnewspaper();
n.display();
magazine m;
m.getmagazine();
m.display();
getch();
}
Slip20:
1.Create a C++ class Number with integer data member. Write necessary member
functions to overload the operator unary pre and post increment ‘++’[15 Marks]
#include<iostream.h>
#include<conio.h>
class number
{
private:
int i;
public:
number(){}
number(int num)
{
i=num;
}
number operator++()
{
++i;
return *this;
}
number operator++(int)
{
number temp=*this;
i++;
return temp;
}
void display()
{
cout<<"i = "<<i<<endl;
}
};
int main()
{
number o(5),o1(5);
clrscr();
class inventory
{
int mno;
char mname[20];
int stock;
float price;
public:
void accept()
{
cout<<"\nEnter model no : ";
cin>>mno;
cout<<"Enter model name : ";
cin>>mname;
cout<<"Enter price : ";
cin>>price;
cout<<"Enter stock : ";
cin>>stock;
}
void display()
{
cout<<"\n\nModel no : "<<mno;
cout<<"\nModel name :"<<mname;
cout<<"\nModel price : "<<price;
cout<<"\nModel stock : "<<stock;
}
/* else
{
cout<<"\nPlease enter correct model no";
} */
}
}
if(no==mno)
{
stock=stock+q;
cout<<"\nPurchse is done..";
display();
}
}
};
int main()
{
inventory in[20];
int qt,ans,no,i,n;
clrscr();
for(i=0;i<no;i++)
{
in[i].accept();
}
for(i=0;i<no;i++)
{
in[i].display();
}
class employee
{
char ename[10],cname[10];
int empid,salary;
public:
void accept()
{
cout<<"\nEnter employee id : ";
cin>>empid;
cout<<"Enter employee name : ";
cin>>ename;
cout<<"Enter company name : ";
cin>>cname;
cout<<"Enter salary : ";
cin>>salary;
}
void display()
{
cout<<"\n\nEmployee Id : "<<empid;
cout<<"\nEmployee Name : "<<ename;
cout<<"\nCompany Name : "<<cname;
cout.width(7);
cout.fill('*');
cout.right;
cout<<"\nSalary : "<<setw(7)<<salary;
}
};
int main()
{
employee *e;
int i,n;
clrscr();
cout<<"\nEnter number of employee : ";
cin>>n;
for(i=0;i<n;i++)
{
e[i].accept();
}
for(i=0;i<n;i++)
{
e[i].display();
}
getch();
return 0;
}
2.Create a C++ class for a two dimensional points. Write necessary member functions to
accept & display the point object. Overload the following operators:
class point
{
int x,y;
public:
void get()
{
cout<<"\nEnter 2-Dimesniaonal point : ";
cin>>x>>y;
}
void put()
{
cout<<"x= "<<x<<"\t"<<"y= "<<y<<endl;
}
void operator*()
{
int n;
cout<<"\nEnter value of n : ";
cin>>n;
x=-x;
y=-y;
x=x*n;
y=y*n;
put();
}
};
int main()
{
point p1,p2,p3;
int ch;
clrscr();
p1.get();
p2.get();
cout<<"\nFirst 1-dimensional point : ";
p1.put();
cout<<"\nSecond 2-dimensional point : ";
p2.put();
do
{
cout<<"\n1.Addition";
cout<<"\n2.Negates coordinates of point";
cout<<"\n3.Multiplication of coordinates by constant n";
cout<<"\n4.Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
p3=p1+p2;
cout<<"\nAddition of point is : ";
p3.put();
break;
case 2:
cout<<"\nNegates of point is : ";
-p1;
break;
case 3:
*p1;
break;
case 4:
exit(0);
}
}while(ch!=4);
getch();
return 0;
}
Slip22:
1. Write a C++ program to define two function templates for calculating the square and
cube of given numbers with different data types.[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template <class T>
class calculator
{
private:
T num1;
public:
calculator(T n1)
{
num1=n1;
}
void display()
{
cout<<"\nNumbers are : "<<num1;
cout<<"\nSquare is : "<<square()<<endl;
cout<<"Cube is : "<<cube()<<endl;
}
T square()
{
return num1*num1;
}
T cube()
{
return num1*num1*num1;
}
};
int main()
{
clrscr();
calculator<int> intcal(2);
calculator<float> floatcal(3.1);
class Display {
public:
// Display a string in double quotes
void display_str(char *str) {
cout << "\"" << str << "\"" <<endl;
}
int main() {
Display display;
return 0;
}
Slip23:
1. Create a C++ class MyString with data member character pointer. Write a C++ program
to accept and display a string. Overload operator to concatenate two strings.[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.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<<str<<"\n";
}
int main()
{
char s1[10],s2[20];
clrscr();
cout<<"\nEnter 1st string : ";
cin>>s1;
cout<<"\nEnter 2nd string : ";
cin>>s2;
mystring m1(s1),m2(s2),m3;
m1.display();
m2.display();
m3=m1+m2;
m3.display();
getch();
return 0;
}
2.Create a C++ class Complex Number with data members real and imaginary. Write
necessary functions:
i. To accept Complex Number using constructor.
ii. To display Complex Number in format [x + iy].
iii. To add two Complex Numbers by using friend function.[25 Marks]
#include<iostream.h>
#include<conio.h>
class complex
{
public:
int real,imag;
void setvalue()
{
cin>>real;
cin>>imag;
}
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
int main()
{
complex c1,c2,c3;
clrscr();
cout<<"\nEnter real and imaginary first complex number : ";
c1.setvalue();
cout<<"\nEnter real and imaginary second complex number : ";
c2.setvalue();
cout<<"\nSum of two complex number is : ";
c3.sum(c1,c2);
c3.display();
getch();
return 0;
}
Slipp24:
1.Create a C++ class FixDeposit with data members FD No, Cust Name, FD Amt, Interest
rate, Maturity amt, Number of months. Create and Initialize all values of FixDeposit object
by using parameterized constructor with default value for interest rate. Calculate maturity
amt using interest rate and display all the details.[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
class fixdeposit
{
int fdno,mnth;
float amt,rate,m_amt;
char name[20];
public:
fixdeposit(int fno,int mnt,float am,float rt,float mamt,char *nm)
{
fdno=fno;
mnth=mnt;
amt=am;
rate=rt;
m_amt=mamt;
strcpy(name,nm);
}
void accept()
{
cout<<"\nEnter FD No : ";
cin>>fdno;
cout<<"Enter Amount : ";
cin>>amt;
cout<<"Enter month : ";
cin>>mnth;
cout<<"Enter maturity amount : ";
cin>>m_amt;
cout<<"Enter name : ";
cin>>name;
}
void display()
{
cout<<"\nFD no : "<<fdno;
cout<<"\nMonth : "<<mnth;
cout<<"\nAmount : "<<amt;
cout<<"\nMaturity Amount : "<<m_amt;
cout<<"\nName : "<<name;
}
void main()
{
int fdno,mnth,rate,amt;
char name[20];
clrscr();
fixdeposit f(fdno,mnth,amt,rate,0,name);
clrscr();
f.accept();
f.calculate();
f.display();
getch();
}
2.Create a C++ class InvoiceBill with data members Order id, O Date, Customer Name, No
of Products, Prod Name[], Quantity[], Prod Price[]. A Customer can buy ‘n’ products.
Accept quantity for each product. Generate and Display the bill using appropriate
Manipulators.
(Use new operator) [25 Marks]
#include<iostream.h>
#include<conio.h>
#include<string.h>
class invoicebill
{
int oid,no_of_products;
int quantity[10],price[10],k;
char name[10],pname[10][10];
public:
void getdata()
{
cout<<"\nEnter Customer name : ";
cin>>name;
cout<<"Enter order id : ";
cin>>oid;
cout<<"Enter No of products : ";
cin>>no_of_products;
for(k=0;k<no_of_products;k++)
{
cout<<"\nEnter product name : ";
cin>>pname[k];
cout<<"Enter quantity : ";
cin>>quantity[k];
cout<<"Enter price : ";
cin>>price[k];
}
}
void display()
{
cout<<"\nOrder Id : "<<oid;
cout<<"\nCustomer name : "<<name;
cout<<"Number of products : "<<no_of_products;
for(k=0;k<no_of_products;k++)
{
cout<<"\nProduct name : "<<pname[k];
cout<<"\nQuantity : "<<quantity[k];
cout<<"\nPrice : "<<price[k];
}
}
void calculate()
{
int total=0;
for(k=0;k<no_of_products;k++)
{
cout<<"\n\nBill "<<quantity[k]*price[k];
total=total+quantity[k]*price[k];
}
cout<<"\nTotal Amount : "<<total;
}
};
void main()
{
clrscr();
invoicebill *p=new invoicebill[10];
invoicebill s[10];
int i,n;
cout<<"\nEnter number of records : ";
cin>>n;
for(i=0;i<n;i++)
{
p[i].getdata();
}
for(i=0;i<n;i++)
{
p[i].display();
}
for(i=0;i<n;i++)
{
p[i].calculate();
}
getch();
}
Slip25:
1. Write a C++ program to calculate mean, mode and median of three integer numbers.
(Use Inline function)[15 Marks]
#include <iostream.h>
#include<stdlib.h>
#include<conio.h>
// Inline function to calculate the mean
inline float calculateMean(int a, int b, int c) {
return (a + b + c) / 3.0;
}
// Inline function to calculate the median
inline float calculateMedian(int a, int b, int c) {
int arr[3] = {a, b, c};
return arr[1];
}
// Inline function to calculate the mode
inline int calculateMode(int a, int b, int c) {
if (a == b && b == c)
return a;
else if (a == b || b == c)
return b;
else
return c;
}
int main() {
int num1, num2, num3;
clrscr();
cout << "Enter three integers: ";
cin >> num1 >> num2 >> num3;
float mean = calculateMean(num1, num2, num3);
float median = calculateMedian(num1, num2, num3);
int mode = calculateMode(num1, num2, num3);
cout << "Mean: " << mean << endl;
cout << "Median: " << median << endl;
cout << "Mode: " << mode << endl;
getch();
return 0;
}
2. Write a C++ program to read the contents of a “SampleData.txt” file. Create “Upper.txt”
file to store uppercase characters, “Lower.txt” file to store lowercase characters, “Digit.txt”
file to store digits and “Other.txt” file to store remaining characters of a given file.[25
Marks]
/*In this program we will create various for executing code*/
/* sample.txt for entering data*/
/*upper.txt , lower.txt , digit.txt , other.txt*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
#include<ctype.h>
int main()
{
char fname[10];
clrscr();
ifstream fin;
ofstream fc,fu,fl,fd;
cout<<"\nEnter file name : ";
cin>>fname;
fin.open(fname,ios::in);
fc.open("other.txt",ios::out);
fu.open("upper.txt",ios::out);
fl.open("lower.txt",ios::out);
fd.open("digit.txt",ios::out);
if(fin.fail())
{
cout<<"File does not exists";
exit(1);
}
char ch;
while(!fin.eof())
{
ch=fin.get();
if(islower(ch))
{
fl.put(ch);
}
else if(isupper(ch))
{
fu.put(ch);
}
else if(isdigit(ch))
{
fd.put(ch);
}
else if((ch>31 && ch<48) || (ch>57 && ch<65) || (ch>122 && ch<127))
{
fc.put(ch);
}
}
cout<<"Task completed";
getch();
return 0;
}