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

Innovative Lab Cse 18bme1225

The document contains solutions to 11 questions on C++ programming concepts. The questions cover topics like class templates, operator overloading, inheritance, friend functions, structures, unions, and more. Code snippets are provided as answers to each question to demonstrate the concept. For example, one question involves creating two classes Employee and Department with Department as a friend class of Employee to access its private members. The code defines the classes and shows how to get input, display output by passing an object between the classes.

Uploaded by

Shrey Jain
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)
69 views

Innovative Lab Cse 18bme1225

The document contains solutions to 11 questions on C++ programming concepts. The questions cover topics like class templates, operator overloading, inheritance, friend functions, structures, unions, and more. Code snippets are provided as answers to each question to demonstrate the concept. For example, one question involves creating two classes Employee and Department with Department as a friend class of Employee to access its private members. The code defines the classes and shows how to get input, display output by passing an object between the classes.

Uploaded by

Shrey Jain
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/ 33

CSE-1002

Innovative Lab – Oct 29th 2020


Shrey s jain
18bme1225
Q1) There is one meeting room in a firm. There are N
meetings in the form of (S[i], F[i]) where S[i] is start time of
meeting i and F[i] is finish time of meeting i. What is the
maximum number of meetings that can be accommodated
in the meeting room?
#include <bits/stdc++.h>
using namespace std;
struct meeting
{
int start;
int end;
int pos;
};
bool comparator(struct meeting m1, meeting m2)
{
return (m1.end < m2.end);
}
void maxMeeting(int s[], int f[], int n)
{
struct meeting meet[n];
for (int i = 0; i < n; i++)
{
meet[i].start = s[i];
meet[i].end = f[i];
meet[i].pos = i + 1;
}
sort(meet, meet + n, comparator);
vector<int> m;
m.push_back(meet[0].pos);
int time_limit = meet[0].end;
for (int i = 1; i < n; i++) {
if (meet[i].start >= time_limit)
{

m.push_back(meet[i].pos);
time_limit = meet[i].end;
}
}
for (int i = 0; i < m.size(); i++) {
cout << m[i] << " ";
}
}
int main()
{
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
int n = sizeof(s) / sizeof(s[0]);
maxMeeting(s, f, n);
return 0;
}
Q2) An electricity board charges the following rates to
domestic users to
discourage large
consumption of energy:
- 60P per unit
- 80P per unit
- 90P per unit
than
Rs.300.00 than an additional surcharge of 15% is added
Write a C++ program to read the names of users and
number of units
consumed and print out
the charges with names.( Use Static data
members,Constructor,Exception error
handling )

#include <iostream>
using namespace std;
int main()
{
cout<<"\n\n\n\tElectricity Board Charges\n";
cout<<"\n\tTo Discourage Large Consumption of
energy\n\n";
float tc;
char name[20];
cout<<"\n\nEnter USER name :-";
cin>>name;
cout<<"\n\nEnter Number of Units Consumed:-";
float unit;
cin>>unit;
if(unit<=100)
tc=unit*60;

else if(unit<=300)
tc=unit*80;
else
tc=unit*90;
float surchase=0;
if(tc>300)
surchase=tc*15;
float total_cost;
total_cost = 50 + surchase + tc;
cout<<"\n\nYOUR BILL AMOUNT IS "<<total_cost;
return 0;
}
Q3) Develop a C++ program in which user is asked to enter
two time periods and these two periods are stored in
structure variables. The program calculates the difference
between these two time periods(Use Friend function and
Friend class)
#include <iostream>
using namespace std;
struct TIME
{
int seconds;
int minutes;
int hours;
};
void computeTimeDifference(struct TIME, struct TIME, struct
TIME *);
int main()
{
struct TIME t1, t2, difference;
cout << "Enter start time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t1.hours >> t1.minutes >> t1.seconds;
cout << "Enter stop time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t2.hours >> t2.minutes >> t2.seconds;
computeTimeDifference(t1, t2, &difference);
cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" <<
t1.minutes << ":"
<< t1.seconds;
cout << " - " << t2.hours << ":" << t2.minutes << ":" <<
t2.seconds;
cout << " = " << difference.hours << ":" <<
difference.minutes << ":" <<
difference.seconds;
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2,
struct TIME
*difference){

if(t2.seconds > t1.seconds)


{
--t1.minutes;
t1.seconds += 60;
}
difference->seconds = t1.seconds - t2.seconds;
if(t2.minutes > t1.minutes)
{
--t1.hours;
t1.minutes += 60;
}
difference->minutes = t1.minutes-t2.minutes;
difference->hours = t1.hours-t2.hours;
}
Q4) Write a program for Library Management in C++. It
contains all the basic transaction that occurred in Library
day to day life. Create a union to represent library book
details(Use Function overload and operator overload)
#include<iostream>
using namespace std;
union Library
{
char name[20];
int bookid;
int cost;
int days;
};
int main()
{
int i,n,days;
union Library d[20];
cout<<" \n Enter the Number of books : " ;
cin>> n ;
for (i =0; i<n; i++)
{
cout<<"\nEnter name of "<<i+1<<" book : " ;
cin>>d[i].name;
cout<<"\nEnter Book id " ;
cin>>d[i].bookid;
cout<<"\nEnter cost of book "<<i+1<<" " ;
cin>>d[i].cost;
cout<<"\nEnter overdue of book "<<i+1<<" " ;
cin>>days;
cout<<"\nTotal Book cost due of book " <<i+1<<"
"<<d[i].cost+days*2 ;
}
return 0;
}
Q5) Write a C++ program to perform different arithmetic
operation such as addition, subtraction,division, modulus
and multiplication using inline function

#include<iostream>
using namespace std;
inline int add(int a, int b)
{
return a+b;
}
inline double division(double x, double y)
{
return x/y;
}
inline float difference(float a, float b)
{
return a-b;
}
inline int mod(int x, int y)
{
return x%y;
}
inline long int multiplication(long int a, long int b)
{
return a*b;
}
int main()
{
int a,b;
double x,y;
float i,j;
long int e,f;
cout<<"\nEnter 2 integer values for calculating there
sum:\n";
cin>>a>>b;
cout<<"\nSum of "<<a<<"and "<<b<<" = "<<add(a,b)<<"\n";
cout<<"\nEnter 2 double values for performing division:\n";
cin>>x>>y;
cout<<"\nValue of "<<x<<" divided by "<<y<<" =
"<<division(x,y)<<"\n";
cout<<"\nEnter 2 float values for calculating there difference
:\n";
cin>>i>>j;
cout<<"\nDifference between "<<i<<" and "<<j<<" =
"<<difference(i,j)<<"\n";
cout<<"Enter 2 integer values for calculating there
modulus:\n";
cin>>a>>b;
cout<<"\n"<<a<<" modulus "<<b<<" = "<<mod(a,b)<<"\n";
cout<<"\nEnter 2 long integer values for calculating there
product:\n";
cin>>e>>f;
cout<<"\nProduct of "<<e<<" and "<<f<<" =
"<<multiplication(e,f)<<"\n";
return 0;
}
Q6) Write a C++ program to implement flight class with data
member as flight no.,source, destination and fare. Write a
member function to display the flight information using this
pointer

#include <iostream>
using namespace std;
void setRoute(char *path[], float *fare, float *tfare){
int i=0;
for(i=0;i<5;i++){
cout<<"Enter flight route:"<<endl;
cout<<"Route ["<<i+1<<"] : ";
path[i]=new char[100];
cin.ignore(1);
cin.getline(path[i],100);
cout<<"Enter fare: ";
cin>>fare[i];
tfare[i]=fare[i]+(fare[i]*19/100);
}
}
void displayPath(char *path[],float *fare, float *tfare){
int i=0;
cout<<"Available flight routes are:"<<endl;
for(i=0;i<5;i++){
cout<<"Route ["<<i+1<<"] : "<<path[i]
<<" - Fare: "<<fare[i]<<",Total Fare: "<<tfare[i]<<endl;
}
}
int main(){
//variable to store flight route
char *route[5];
float fare[5],totalFare[5];
char name[30],path[100];
int d,m,y;
int routeId;
setRoute(route,fare,totalFare);
cout<<endl<<endl;
cout<<"Enter name: ";
cin.ignore(1);
cin.getline(name,30);
cout<<"Enter date of travel (d m y): ";
cin>>d>>m>>y;
displayPath(route,fare,totalFare);
cout<<"Choose flight route (1 to 5): ";
cin>>routeId;
if(routeId<1 || routeId>5){
cout<<"Invalid flight route!!!"<<endl;
return 0;
}
cout<<endl<<endl;
cout<<"Congratulations... "<<name<<" your ticket has been
booked."<<endl;
cout<<"Travel date is: "<<d<<"/"<<m<<"/"<<y<<endl;
cout<<"Flight route: "<<route[routeId-1]<<endl;
cout<<"Total fare is: "<<totalFare[routeId-1]<<endl;
return 0;
}
Q7) Write a program to count the words and characters in
given text using virtual function
#include <iostream>
#include <ctype.h>
using namespace std;

class words {
public:
char a[200];
virtual void countl()
{}

void countw()
{int count=1;
cin.getline(a,200);
for(int i=0;a[i]!='\0';i++)
{
if(isspace(a[i]))
count++;
}
cout<<"There are "<<count<<" words"<<endl;
}
};

class letters : public words {


public:
void countl()
{int count=0;
cin.getline(a,200);
for(int i=0;a[i]!='\0';i++)
{if(!isspace(a[i]))
count++;
}
cout<<"There are "<<count<<" letters"<<endl;
}
};

int main()
{
words* bptr;
letters d;
bptr = &d;
bptr->countl();
bptr->countw();
}

Q8) Implement a C++ program to overload unary minus


operator to find the negation of a vector.

#include <iostream>
using namespace std;
class Minus{
public:
int val;
Minus operator-(){
val=-1*val;
cout<<val;
}
};
int main()
{
Minus m;
cin>>m.val;
-m;
return 0;
}

Q9) Write a program to accept five different numbers by


creating a class called friendfunc1 and friendfunc2 taking 2
and 3 arg respectively and calculate the average of these
numbers by passing object of the class to friend function

#include <iostream>
using namespace std;
class frindfunc2;
class frindfunc1{
public:
int a,b;
void input1(){
cin>>a>>b;
}
friend void add(frindfunc1,frindfunc2);
};
class frindfunc2{
public:
int c,d,e;
void input2(){
cin>>c>>d>>e;
}
friend void add(frindfunc1,frindfunc2);
};
void add(frindfunc1 obj1,frindfunc2 obj2)
{
int tot;
float avg;
tot=obj1.a+obj1.b+obj2.c+obj2.d+obj2.e;
avg=tot/5;
cout<<"average is="<<avg;
}
int main()
{
frindfunc1 f1;
frindfunc2 f2;
f1.input1();
f2.input2();
add(f1,f2);
return 0;
}

Q10) Create two classes Employee and Department. Make


Department class, a friend class of Employee class.In order
to access the private and protected members of Employee
class into Department class explicitly pass an object of
Department class to the member functions of Employee
class. Display the net salary of employee

#include <iostream>
#include <fstream>
using namespace std;
class Employee{
char name[100];
int empid;
int salary;
friend class Department;
public:
void getdetails()
{
cout<<"Enter Employee name "<<endl;
cin>>name;
cout<<"Enter employee id"<<endl;
cin>>empid;
cout<<"Enter employee salary"<<endl;
cin>>salary;
}
};
class Department{
char deptname[100];
public:
void getdeets()
{
cout<<"Enter department name"<<endl;
cin>>deptname;
}
void showall(Employee obj)
{cout<<"Employee Name "<<obj.name<<endl;
cout<<"Employee ID "<<obj.empid<<endl;
cout<<"Employee Salary "<<obj.salary<<endl;
cout<<"Department Name "<<deptname<<endl;
}
};
int main(){
Employee obj;
Department obj2;
obj.getdetails();
obj2.getdeets();
obj2.showall(obj);
return 0;
}
Q11) Write a C++ program to find the read two data items
and find the sum using class template

#include <iostream>
using namespace std;
template<class t1,class t2>
void sum(t1 a,t2 b)
{
cout<<"\nSum="<<a+b<<endl;
}
int main()
{
int a,b;
float x,y;
cout<<"\nEnter two integer data: ";
cin>>a>>b;
cout<<"\nEnter two float data: ";
cin>>x>>y;
sum(a,b);
sum(x,y);
sum(a,x);
return 0;
}

Q12) Write a program to impement Stack and its operations


using dynamic memeory allocation

#include <iostream>
#include<stdlib.h>
using namespace std;
int count = 0;
struct Node{
int num;
struct Node* next;
}*head=NULL;
void insert(int val){
struct Node* temp=(struct Node*)malloc(sizeof(struct
Node));
if(head==NULL && count<10){
head=(struct Node*)malloc(sizeof(struct Node));
head->num=val;
head->next=NULL;
count++;
}
else if(count<100){
temp->num=val;
temp->next=head;
head=temp;
count++;
}
else if(count>=100){
cout<<"Stack is Full"<<endl;
}
}
void del(){
if(head!=NULL)
head=head->next;
else
cout<<"Stack is empty"<<endl;
}
void show(){
if(head==NULL)
cout<<"Underflow";
struct Node* temp=(struct Node*)malloc(sizeof(struct
Node));
temp=head;
while(temp!=NULL){
cout<<"->"<<temp->num;
temp=temp->next;
}
}
int main()
{
int c=1;
while(c==1){
int val;
cin>>val;
insert(val);
cin>>c;
}
del();
show();
return 0;
}

Q13) Write a program to perform read/write binary I/O


operation on a file (i.e. Write the object of a structure /class
to file).
#include <iostream>
#include <fstream>
#define FILE_NAME "emp.dat"
using namespace std;
class Employee {
private :
int empID;
char empName[100] ;
char designation[100];
int ddj,mmj,yyj;
int ddb,mmb,yyb;
public :
void readEmployee(){
cout<<"EMPLOYEE DETAILS"<<endl;
cout<<"ENTER EMPLOYEE ID : " ;
cin>>empID;
cin.ignore();
cout<<"ENTER NAME OF THE EMPLOYEE : ";
cin.getline(empName,100);
cout<<"ENTER DESIGNATION : ";
cin.getline(designation,100);
cout<<"ENTER DATE OF JOIN:"<<endl;
cout<<"DATE : "; cin>>ddj;
cout<<"MONTH: "; cin>>mmj;
cout<<"YEAR : "; cin>>yyj;
cout<<"ENTER DATE OF BIRTH:"<<endl;
cout<<"DATE : "; cin>>ddb;
cout<<"MONTH: "; cin>>mmb;
cout<<"YEAR : "; cin>>yyb;
}
void displayEmployee(){
cout<<"EMPLOYEE ID: "<<empID<<endl
<<"EMPLOYEE NAME: "<<empName<<endl
<<"DESIGNATION: "<<designation<<endl
<<"DATE OF JOIN: "<<ddj<<"/"<<mmj<<"/"<<yyj<<endl
<<"DATE OF BIRTH: "<<ddb<<"/"<<mmb<<"/"<<yyb<<endl;
}
};
int main(){
Employee emp;
emp.readEmployee();
fstream file;
file.open(FILE_NAME,ios::out|ios::binary);
if(!file){
cout<<"Error in creating file...\n";
return -1;
}
file.write((char*)&emp,sizeof(emp));
file.close();
cout<<"Date saved into file the file.\n";
file.open(FILE_NAME,ios::in|ios::binary);
if(!file){
cout<<"Error in opening file...\n";
return -1;
}
if(file.read((char*)&emp,sizeof(emp))){
cout<<endl<<endl;
cout<<"Data extracted from file..\n";
//print the object
emp.displayEmployee();
}
else{
cout<<"Error in reading data from file...\n";
return -1;
}
file.close();
return 0;
}

You might also like