0% found this document useful (0 votes)
14 views

Cpp Slip Solution-1

The document contains multiple C++ programming exercises, including checking maximum and minimum values, file handling, function overloading, class creation, and operator overloading. It covers various topics such as calculating volumes of geometric shapes, managing employee records, and implementing inheritance. Each exercise includes code snippets and explanations for implementing specific functionalities.

Uploaded by

rehankhan44700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Cpp Slip Solution-1

The document contains multiple C++ programming exercises, including checking maximum and minimum values, file handling, function overloading, class creation, and operator overloading. It covers various topics such as calculating volumes of geometric shapes, managing employee records, and implementing inheritance. Each exercise includes code snippets and explanations for implementing specific functionalities.

Uploaded by

rehankhan44700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

CPP SLIP SOLUTION

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]

/*In this code we will create 2 text file */


/*lmn.txt*/
Hiii
/*xyz.txt*/
Good Morning

#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");

cout << "first file \n";


f1.display();
cout << "\nsecond file \n";
f2.display();
f3 = f1 + f2;
cout << "\nAfter concatnation file is ";
f3.display();
cout << "\nAfter changes case \n";
!f3;
f3.display();

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;

cout<<"\n\nThe Volume of Cylinder is : "<<volume(cylinder_r,cylinder_h)<<endl;


cout<<"The Volume of Cone is : "<<volume(cone_r,cone_h)<<endl;
cout<<"The Volumer of Sphere is : "<<volume(sphere_r)<<endl;
getch();
return 0;
}
float volume(int r,int h)
{
return(3.14*r*r*h);
}

float volume(float r,float h)


{
return(3.14*r*r*h/3);
}

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);

cout<<"\nEnter the no of records you want to accept : ";


cin>>n;
for(i=0;i<n;i++)
{
m[i].accept();
file.write((char *) &m[i],sizeof(m[i]));
}
cout<<"\nDetils of movie from file :\n ";
for(i=0;i<n;i++)
{
file.read((char *) &m[i],sizeof(m[i]));
m[i].display();
}
file.close();
getch();
}
Slip3
Q 1:
1 .Write a C++ program to interchange values of two integer numbers.
(Use call by reference)[15 Marks]
#include<iostream.h>
#include<conio.h>

void swap(int &,int &);


int main()
{
int a,b;
clrscr();
cout<<"Enter value of a : ";
cin>>a;
cout<<"Enter value of b : ";
cin>>b;

cout<<"\nBefore Swapping value is : ";


cout<<"\n\tA = " << a << "\n\tB = " << b << endl;

swap(a,b);
cout<<"\nOutside Function after swapping value is : ";
cout<<"\n\tA = " << a << "\n\tB = " << b << endl;

getch();
return 0;
}

void swap(int &a,int &b)


{
int c;
c=a;
a=b;
b=c;
cout<<"\nInside function after swapping value is : ";
cout<<"\n\tA = " << a << "\n\tB = " << b << endl;
}

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;

cout<<"\nEnter Co-Ordinates : ";


cout<<"\nEnter X : ";
cin>>a;
cout<<"Enter Y : ";
cin>>b;
p.setpoint(a,b);
p.showpoint();
getch();
return 0;
}

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;
};

class circle:public shape


{
float r;
public:
void area()
{
cout<<"Enter Radius of Circle : " ;
cin>>r;
cout<<"Area of circle is : "<<(3.14*r*r);
}
};

class sphere:public shape


{
float r;
public:
void area()
{
cout<<"\nEnter Radius of Sphere : ";
cin>>r;
cout<<"Area of Sphere is : "<<(4*3.14*r*r);
}
};

class cylinder:public shape


{
float r,h;
public:
void area()
{
cout<<"\nEnter Radius of Cylinder : ";
cin>>r;
cout<<"Enter Height of Cylinder : ";
cin>>h;
cout<<"\nArea of Cylinder is : "<<(2*3.14*r*h + 2*3.14*r*r);
}
};

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

friend void compare(int s,int r);


};

void compare(int s,int r)


{
if(s>r)
{
cout<<"\nThe area of sqaure is greater than area of rectangle"<<endl;
}
else
{
cout<<"\nThe area of rectangle is greater than area of sqaure"<<endl;
}
}

int main()
{
int s_area,r_area;
clrscr();
sqaure s;
rectangle r;

s.getdata();
s_area=s.calarea();

r.getdata();
r_area=r.calarea();

cout<<"\nSquare : " <<s_area<<endl;


cout<<"Rectangle : "<<r_area<<endl;

compare(s_area,r_area);
getch();
return 0;
}

2.Create a class Matrix


{
int **p,r,c;
public: //member functions
};
Write necessary functions to :
1. accept matrix elements
2. display matrix elements
3. calculate transpose of a matrix
(use constructor and destructor)[25 Marks]

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

using namespace std;

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

int mystr::replace(char *str,char c1,char c2='r')


{
int i,n;
while(str!='\0')
{
if(*str==c1)
{
*str==c2;
n++;

}
str++;
i++;
}

str=str-i;
return n;
}

int main()
{
mystr m;
char *str,c1,c2,a;
clrscr();
cout<<"Enter String : ";
cin>>str;

cout<<"Enter character which is to replace : ";


cin>>a;

cout<<"\nNumber of Replacement : "<<m.replace(str,c1,c2);


cout<<"\n\nAfter Replacement string is : "<<str;
getch();
return 0;
}
2.Create a C++ class Vector with data members size & pointer to integer. The size of the
vector varies so the memory should be allocated dynamically.Perform following
operations:.
1. accept vector
2.display vector in format(10,20,30,…….,)
3.calculate union of two matrix.
(use parameterized constructor & copy constructor)[25 Marks]
#include <iostream.h>
#include <conio.h>
#include<stdlib.h>
class Vector {
int *a, *b;
int n, n1;
public:
void create() {
int i,j;
cout << "Enter dimension of first vector: ";
cin >> n;
a = new int[n];
cout << "Enter elements of first vector: ";
for ( i = 0; i < n; i++) {
cin >> a[i];
}

cout << "Enter dimension of second vector: ";


cin >> n1;
b = new int[n1];
cout << "Enter elements of second vector: ";
for ( j = 0; j < n1; j++) {
cin >> b[j];
}
}

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;

for ( i = 0; i < n; i++) {


unionArray[unionSize++] = a[i];
}

for (j = 0; j < n1; j++)


{
bool found = false;
for ( k = 0; k < n; k++) {
if (b[j] == a[k]) {
found = true;
break;
}
}
if (!found) {
unionArray[unionSize++] = b[j];
}
}

cout << "\nThe union of the vectors is: (";


for ( i = 0; i < unionSize - 1; i++) {
cout << unionArray[i] << ", ";
}
if (unionSize > 0) {
cout << unionArray[unionSize - 1] << ")";
}
delete[] unionArray;
}

~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 acc(int mno) //function overloading


{
if(mno==mob)
{
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;
}

time time::operator -(time t2)


{
time t;
t.h=h-t2.h;
t.m=m-t2.m;
t.s=s-t2.s;
return t;
}

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 sort(city &r1,city &r2)


{
city temp;
if(r1.population>r2.population)
{
temp=r1;
r1=r2;
r2=temp;
}
}

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;
}

cout<<"\nDo you want to continue : ";


cin>>cont;
}
while(cont=='Y' || cont=='y');
getch();
}
Slip11
1 . Create a C++ class MyArray, which contains single dimensional integer array of a given
size. Write a member function to display sum of given array elements. (Use Dynamic
Constructor and Destructor)[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

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

class result:public theory,public practical


{
int total_marks;
float per;
char grade[10];

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;
}

weight operator+=(weight &d)


{
weight t;
t.kilogram=d.kilogram+kilogram;
t.gram=d.gram+gram;
return t;
}

int operator==(weight &d)


{
if(kilogram==d.kilogram || gram==d.gram)
{
return 1;
}
else
{
return 0;
}
}
};

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;
}

static void nob()


{
cout<<"\nNumber of object created for class are : "<<cnt;
}
};

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;
}

inline void volume()


{
cout<<"\nThe Volume of cuboid is : "<<len*bre*hei;
}

inline void surface_area()


{
cout<<"\nThe Surface area of cuboid is : "<<(2*len*bre + 2*bre*hei + 2*len*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>

inline float diameter(float r)


{
return(r/2);
}

inline float circlearea(float r)


{
return(3.14*r*r);
}

inline float circumeference(float r)


{
return(3.14*2*r);
}

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;
}

// Function to reverse the case of each alphabet


MyString& reverseCase() {
for (int i = 0; i < length; ++i) {
if (isalpha(str[i])) {
if (islower(str[i]))
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
}
return *this;
}

// Function to compare length of two strings


bool operator<(const MyString &other) const {
return length < other.length;
}

// Function to add a constant 'n' to each alphabet of a string


MyString& operator+(int n) {
for (int i = 0; i < length; ++i) {
if (isalpha(str[i])) {
str[i] += n;
}
}
return *this;
}

// Function to display the string


void display() const {
cout << str << endl;
}
};

int main() {
MyString str1("Hello World!");
MyString str2("Goodbye");

cout << "Original strings:" << endl;


str1.display();
str2.display();

// Reverse the case of each alphabet


str1.reverseCase().display();

// Compare lengths of two strings


if (str1 < str2) {
cout << "String 1 is shorter than String 2." << endl;
} else {
cout << "String 1 is longer than or equal to String 2." << endl;
}

// Add a constant 'n' to each alphabet of a string


str2 + 1;
cout << "After adding 1 to each alphabet of String 2:" <<endl;
str2.display();

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

void fraction::operator+(fraction f2)


{
cout<<"\nAddiiton : ";
num=num*f2.dnm+f2.num*dnm;
dnm=dnm*f2.dnm;
cout<<num<<"\\"<<dnm;
}

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;

cout<<"\nEnter product name : ";


cin>>pname;

cout<<"\nEnter sales quantity : ";


cin>>squantity;

cout<<"\nEnter target : ";


cin>>target;
}

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 matrix::operator -(matrix z)


{
int mat[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat[i][j]=a[i][j]-z.a[i][j];
}
}
cout<<"\n\nSubtraction of matrix is \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat[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;
}

class studcomp:public student,public competition


{
public:
int rank;
void accept_sc();
void display_sc();
friend void sort(studcomp,int);
};

void studcomp::accept_sc()
{
cout<<"\nEnter rank : ";
cin>>rank;
}

void studcomp:: display_sc()


{
cout<<"\nRank : "<<rank<<endl;
}

void sort(studcomp *sc,int num)


{
int i,j,temp,tempr;
for(i=0;i<num;i++)
{
for(j=0;j<num-i-1;j++)
{
if(sc[j].rank > sc[j+1].rank)
{
temp=sc[j].rank;
sc[j].rank=sc[j+1].rank;
sc[j+1].rank=temp;

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;
}

// Function to accept array elements


void acceptElements() {
int i;
cout << "Enter " << size << " elements:\n";
for ( i = 0; i < size; ++i) {
cin >> arr[i];
}
}

// Function to display array elements


void displayElements() {
int i;
cout << "Array elements are:\n";
for ( i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << std::endl;
}

// Overloading unary operator to reverse array elements


MyArray operator-() {
int i;
MyArray temp(size);
for ( i = 0; i < size; ++i) {
temp.arr[i] = arr[size - 1 - i];
}
return temp;
}

// Overloading unary operator to add a constant to all array elements


MyArray operator+(int n) {
int i;
MyArray temp(size);
for (i = 0; i < size; ++i) {
temp.arr[i] = arr[i] + n;
}
return temp;
}
};

int main() {
int size;
cout << "Enter size of the array: ";
cin >> size;

MyArray arr(size);
arr.acceptElements();

// Display original array


cout << "Original ";
arr.displayElements();

// Reverse array elements


MyArray reversedArr = -arr;
cout << "Reversed ";
reversedArr.displayElements();

// Add constant to array elements


int constant;
cout << "Enter constant to add to array elements: ";
cin >> constant;
MyArray modifiedArr = arr + constant;
cout << "Modified ";
modifiedArr.displayElements();
getch();
return 0;
}
Slip19:
1.Write a C++ program to create a class Distance with data members meter and centimeter
to represent distance. Write a function Larger( ) to return the larger of two distances. (Use
this pointer)[15 Marks]
#include<iostream.h>
#include<conio.h>

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;
}

distance larger(distance d1,distance d2)


{
if(d1.meter>d2.meter)
{
return d1;
}
else if(d1.meter<d2.meter)
{
return d2;
}
else
{
if(d1.centimeter>d2.centimeter)
{
return d1;
}
else
{
return d2;
}
}
}
};

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;
};

class newspaper:public media


{
public:
int no_of_pages,n_price;
char n_name[20],n_editor[20];
void getnewspaper()
{
cout<<"\nEnter no of pages : ";
cin>>no_of_pages;
cout<<"Enter newspaper price : ";
cin>>n_price;
cout<<"Enter newspaper name : ";
cin>>n_name;
cout<<"Enter newspaper editor : ";
cin>>n_editor;
}
void display();
};

class magazine:public media


{
public:
char m_name[20],m_editor[20];
int m_price;
void getmagazine()
{
cout<<"\n\nEnter magazine name : ";
cin>>m_name;
cout<<"Enter magazine editor : ";
cin>>m_editor;
cout<<"Enter magazine price : ";
cin>>m_price;
}
void display();
};

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

cout<<"\nOriginal Number = "<<endl;


o.display();
++o;
cout<<"\n\nAfter pre-increment number = "<<endl;
o.display();
o1++;
cout<<"\n\nAfter post-increment number = "<<endl;
o1.display();
getch();
return 0;
}
2. Create a C++ class for inventory of Mobiles with data members Model, Mobile
Company, Color, Price and Quantity. Mobile can be sold, if stock is available, otherwise
purchase will be made.
Write necessary member functions for the following:
i.To accept mobile details from user.
ii. To sale a mobile. (Sale contains Mobile details & number of mobiles to be sold.)
iii. To Purchase a Mobile. (Purchase contains Mobile details & number of mobiles to be
purchased)[25 Marks]
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

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;
}

int sale(int no,int q)


{
if(mno==no)
{
if(stock>=q)
{
stock=stock-q;
cout<<"\nSale is done";
display();
return 2;
}
else
{
cout<<"\nPurchase is required";
purchase(q,no);
return 1;
}

/* else
{
cout<<"\nPlease enter correct model no";
} */
}
}

void purchase(int no,int q)


{

if(no==mno)
{
stock=stock+q;
cout<<"\nPurchse is done..";
display();
}
}
};

int main()
{
inventory in[20];
int qt,ans,no,i,n;
clrscr();

cout<<"\nEnter no of models : ";


cin>>no;

for(i=0;i<no;i++)
{
in[i].accept();
}

for(i=0;i<no;i++)
{
in[i].display();
}

cout<<"\n\nEnter model number to be purchase and quantity : ";


cin>>n>>qt;
for(i=0;i<no;i++)
{
ans=in[i].sale(n,qt);
if(ans==1)
{
cout<<"\nEnter model to be purchse and quantity : ";
cin>>n>>qt;
for(i=0;i<no;i++)
{
in[i].purchase(n,qt);
}
}
}
getch();
return 0;
}
Slip21:
1 . Create a C++ class Employee with data members Emp id, Emp Name, Company Name
and Salary. Write member functions to accept and display Employee information. Design
User defined Manipulator to print Salary.
(For Salary set right justification, maximum width to 7 and fill remaining spaces with
‘*’)[15 Marks]
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

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:

Operator Example Purpose


+ (Binary) P3=P 1+P2 Adds coordinates of point pl and p2.
-(Unary) -P1 Negates coordinates of point pl
*(Binary) P2=P1 * n Multiply coordinates of point pl by constant ‘n ‘.[25 Marks]
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

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;
}

point operator+(point &t)


{
point tp;
tp.x=x+t.x;
tp.y=y+t.y;
return tp;
}
void operator-()
{
x=-x;
y=-y;
put();
}

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);

cout<<"\nInt result : ";


intcal.display();
cout<<"\n\nFloat result : ";
floatcal.display();
getch();
return 0;
}
2.Write a C++ program to overload ‘display str’ function as follows:
i. void display str(char * ) – Display a string in double quotes.
ii. void display str (int n, char * )- Display first n characters from a given string.
iii. void display str (int m, int n,char * )- Display substring of a given string from position m
to n.[25 Marks]
#include <iostream.h>
#include <cstring.h>

class Display {
public:
// Display a string in double quotes
void display_str(char *str) {
cout << "\"" << str << "\"" <<endl;
}

// Display first n characters from a given string


void display_str(int n, char *str) {
for (int i = 0; i < n && str[i] != '\0'; ++i) {
cout << str[i];
}
cout << endl;
}

// Display substring of a given string from position m to n


void display_str(int m, int n, char *str) {
if (m < 0 || m >= strlen(str) || n < 0 || n >= strlen(str) || m > n) {
cout << "Invalid position!" <<endl;
return;
}
for (int i = m; i <= n; ++i) {
cout << str[i];
}
cout << endl;
}
};

int main() {
Display display;

char str[] = "Hello, World!";

// Displaying string in double quotes


display.display_str(str);

// Displaying first 7 characters


display.display_str(7, str);

// Displaying substring from position 7 to 12


display.display_str(7, 12, str);

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";
}

mystring operator+(mystring ob)


{
mystring a;
int len1,len2;
len1=strlen(str);
len2=strlen(ob.str);
a.str=new char[len1+len2];
strcpy(a.str,str);
strcat(a.str,ob.str);
return a;
}
};

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;
}

void sum(complex c1,complex c2)


{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
};

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 calculate(int rate=20)


{
double yr;
yr=mnth/12.0;
m_amt=amt*pow((1+rate/100),yr);
cout<<"\n\nMaturity amount details is : "<<m_amt;
}
};

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;
}
};

const int size=2;

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy