Name-Shivam Kumar
Roll no.-18115076
Lab Work
Q1.Write a function power() to raise a number m to a power n. The function takes a
double value for m and int value for n and returns the result correctly. Use a default
value of 2 for n to make the function to calculate squares when this argument is
omitted. Write a main that gets the values of m and n from the user to test the
function.
Ans1-
#include<iostream>
#include<math.h>
using namespace std;
int power(double m,int n);
int main()
{
double res,m;
int n;
cout<<"Enter m and n value =\n";
cin>>m>>n;
res=power(m,n);
cout<<res;
return 0;
}
int power(double m,int n=2)
{
double res;
res=pow(m,n);
return res;
}
Q2.Define a class to represent a bank account. Include the following members
Data members
(i) Name of depositor (ii) Account number (iii) Type of Account (iv) Balance amount
in
the account
Member Functions
(i) To assign initial values (ii) To deposit an amount (iii) To withdraw an amount after
checking the balance (iv) To display name and balance.
Ans2-
Code-
//2 Bank account
#include<iostream>
#include<math.h>
using namespace std;
class bank
{
public:
char nm[20];
char acno[15];
char tyac[20];
int depo,with;
int bal;
bank()
{
bal=0;
}
void take();
void deposite();
void withdraw();
void show();
};
int main()
{
bank ob;
ob.take();
ob.show();
cout<<endl<<"SHIVAM MANIHAR SAHU \nRoll no.-18115077";
return 0;
}
void bank::take()
{
cout<<"Enter Depositor name = ";
gets(nm);
cout<<"Enter Account number = ";
cin>>acno;
void bank::deposite()
{
}
c ut<<"Enter Type of account = ";
o cin>>tyac;
cout<<"Enter amount to be deposited = ";
cin>>depo;
bal=depo+bal;
show();
}
void bank::show()
{
char y='Y';
cout<<"Depositor name = ";
cout<<nm<<"\n";
cout<<"Account number = ";
cout<<acno<<"\n";
cout<<"Type of account = ";
cout<<tyac<<"\n";
cout<<"Balance amount = ";
cout<<bal<<"\n";
cout<<"Do you want to withdraw(W) or deposite(D) or exit(0)
money W or D or 0 = ";
cin>>y;
if(y=='w'||y=='W')
withdraw();
else if(y=='D'||y=='d')
deposite();
else
cout<<"Thankyou for visiting us ...";
}
void bank::withdraw()
{
cout<<"Enter amount to be withdraw = "<<"\n";
cin>>with;
if(bal-with<0)
{
cout<<"Not have enough money to be withdraw \n";
}
else
{
bal=bal-with;
}
show();
}
Q3-A book shop maintains the inventory of books that are being sold at the shop.
The list includes details such as author, title, price, publisher and stock position.
Whenever a customer wants a book, the sales person inputs the title and author and
the system searches the list and displays whether it is available or not. If it is not, an
appropriate message is displayed. If it is, then the system displays the book details
and requests for the number of copies required. If the requested copies book details
and requests for the number of copies required. If the requested copies are
available, the total cost of the requested copies is displayed; otherwise the message
“Required copies not in stock” is displayed.
Design a system using a class called books with suitable member functions and
Constructors. Use new operator in constructors to allocate memory space required.
Ans3-
Code-
//3 BOOK SHOP
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
class book
{
public:
char atnm[20],anm[20];
char tit[15],ti[15];
char pub[15];
int pri;
int stk;
int totpri;
int nocopy;
void take();
void cal();
void show();
};
int power(double m,int n);
int main()
{
book ob;
ob.take();
ob.show();
return 0;
}
void book::take()
{
cout<<"Please give book info\n";
cout<<"Enter Author name = ";
gets(atnm);
cout<<"Enter Book Title = ";
gets(tit);
cout<<"Enter Publisher name = ";
gets(pub);
cout<<"Enter Book price = ";
cin>>pri;
cout<<"Enter stock = ";
cin>>stk;
cal();
}
void book::cal()
{
fflush(stdin);
cout<<"Enter Author name = ";
gets(anm);
cout<<"Enter Book Title = ";
gets(ti);
if(strcmpi(anm,atnm)==0&&strcmpi(anm,atnm)==0)
{
if(stk>0)
{
else
} {
cout<<"book is available\n";
} show();
cout<<"sorry book is unavailable";
}
else
{
cout<<"please enter correct author and title
name\n";
cal();
}
void book::show()
{
cout<<"Book Details\n";
cout<<"Author name = ";
cout<<atnm<<"\n";
cout<<"Book Title = ";
cout<<tit<<"\n";
cout<<"Publisher name = ";
cout<<pub<<"\n";
cout<<"Book Price = ";
cout<<pri<<"\n";
cout<<"Stock = ";
cout<<stk<<"\n";
cout<<"Enter the no.of copies you want";
cin>>nocopy;
if(nocopy>stk)
{
cout<<"This many of copies are not avaible";
}
else
{
totpri=nocopy*pri;
cout<<"Total price is = "<<totpri;
}
cout<<"\nThankyou for coming to book shop \n";
}
Q4-Create two classes DM and DB which store the value of distances. DM stores in
meters and centimeters and DB in feet and inches. Write a program that can read
values for the class objects and add one object of DM with another object of DB.
Use a friend function to carry out the addition operation. The object that stores the
results maybe DM object or DB object depending on the units in which the results
are required.
The display should be in the format of feet and inches or meters and centimeters
depending on object on display.
Ans4-
Code-
#include<bits/stdc++.h>
using namespace std;
class DB;
class DM{
private:
friend class DB;
float m,cm;
public:
void set()
{
int a;
cout<<"enter the length in centi";
cin>>a;
m=a/100;
cm=a%100;
}
void display()
{
cout<<"meter- "<<m<<"centimeter "<<cm<<endl;
}
};
class
DB{ private
: float
in,feet;
public:
void set()
{
int b;
cout<<"enter the length in inches";
cin>>b;
feet=b/12;
in=b%12;
}
void display()
{
cout<<"feet- "<<feet<<"inches- "<<in<<endl;
}
void add(DM d,DB c)
{
float f;
c.in=12*c.feet+c.in;
d.cm=c.in*2.54;
f=d.cm;
d.m=(int)d.m;
if(f>=100)
{
float m;
int rem,in;
in=int(f);
m=f-in;
d.cm=m;
rem=f/100;
d.m=d.m+rem;
}
/* d.cm=d.cm+(c.in)*2.54;
d.m=(d.m+(c.feet)*0.305);
d.cm=d.m-(int)d.m+d.cm;
*/
cout<<"meter = "<<d.m;
cout<<"centimeter = "<<d.cm;
}
};
int main()
{
DB d1,d4;
DM d2,d3;
d1.set();
d2.set();
d1.display();
d2.display();
d4.add(d2,d1);
return 0;
}
Q5-Create a class FLOAT that contains one float data member. Overload all four
arithmetic operators so that they operate in objects of float.
Ans5-
Code-
//FLOAT CLASS constructor overloading
#include<iostream>
using namespace std;
class FLOAT
{
public:
float a;
FLOAT()
{
a=10;
}
FLOAT(float b)
{
a=b;
}
FLOAT(const FLOAT &ob2)
{
a=ob2.a;
}
void take()
{
cout<<"Enter value please = ";
cin>>a;
}
void show()
{
cout<<"All arthematic operations are \n ";
cout<<"Addition = "<<a+a<<"\n";
cout<<"Subtraction = "<<a-a<<"\n";
cout<<"Multiplication = "<<a*a<<"\n";
cout<<"Division = "<<a/a<<"\n";
}
};
int main()
{
FLOAT ob,ob1(12),ob2(20);
FLOAT ob3=ob2;
ob2.take();
cout<<"By user input \n";
ob2.show();
cout<<"By user default constructor \n";
ob.show();
cout<<"By user parameterised constructor \n";
ob1.show();
cout<<"By user copy constructor \n";
ob3.show();
cout<<endl<<"SHIVAM KUMAR \nRoll no.-18115076";
}
Q6-An electricity board charges the following rates to domestic users to discourage
large consumption of energy : –
For first 100 units : 60p per unit
For next 200 units : 80p per unit
Beyond 300 units : 90p per unit
All users are charged a minimum of Rs.50. If the total amount is more than
Rs.300.00 then an additional surcharges of 15% are added. Write a program to read
the names of users & number of units consumed & print out the charges with names.
Ans6-
Code-
// 8 Power consumption
#include<iostream>
using namespace std;
int main()
{
float n,sum=50,cal;
char nm[20];
cout<<"Enter your name please = ";
gets(nm);
cout<<"Enter Power consumption = ";
cin>>n;
if(n<100)
{
sum=sum+n*0.6;
}
else if(n<200)
{
sum=sum+n*0.8;
}
else if(n<300)
{
sum=sum+n*0.9;
}
else
{
sum=sum+n*0.9+n*0.15;
}
cout<<"User name ="<<nm<<endl;
cout<<"Total Power consumotion ="<<n<<endl;
cout<<"Total Money to be paid ="<<sum<<endl;
Q7-Rewrite the following codes after removing any/all errors with each correction
underline:
#include<iostream.h>
void main()
{
int num[]={1,2,3,4,5,6};
num[1]==[1]num ? cout<<”Success” : cout<<”Error”;
}
Ans7-
#include<iostream> //.h doest required
using namespace std; //for accessing cout library
int main() //main must be int return type
{
int num[]={1,2,3,4,5,6};
num[1]==num[1] ? cout<<"success" : cout<<"error"; //inverted comma is
required and [1]num is invalid array name should come first
cout<<endl<<"SHIVAM KUMAR \nRoll no.-18115076";
}
Q8-Rewrite the following codes after removing any/all errors with each correction
underline:
Ans8-
#include<iostream>//.h doest required
using namespace std;////for accessing cout library
class staticFuntion
{
static int count;
public:
static void setCount()
{
count++;
}
void displayCount()
{
cout<<count;
}
};
int staticFuntion::count=10;//'staticFuntion' was not declared in this scope
int main()
{
staticFuntion obj1;
obj1.setCount();
staticFuntion::setCount();
obj1.displayCount();
cout<<endl<<"SHIVAM KUMAR \nRoll no.-18115076";
}
Q9-Rewrite the following codes after removing any/all errors with each correction
underline:
#include<iostream.h>
class Room
{
int length;
int width;
public:
Room ( )
{
length=0;
width=0;
}
Room(int value=8)
{
length=width=8;
}
void display()
{
Cout<<length<<’ ‘<<width;
}
};
void main()
{
Room objRoom1
objRoom1.display();
}
Ans9-
Code-
#include<iostream>//.h doest required
using namespace std;//for accessing cout library
class Room
{
private:
int length;
int width;
public:
Room()
{
length=0;
width=0;
}
Room(int l,int w)// for valid constructor overloading
{
length=l;
width=w;
}
void display()
{
cout<<length<<" "<<width<<" ";//C shold be small and double inverted
comma should be used
}
};
int main()//sholud be int
{
Room obj;//terminator is required AND Invalid declaration of constructor
overlaoding
Room obj1(8,8);
obj.display();
obj1.display();
cout<<endl<<"SHIVAM KUMAR \nRoll no.-18115076";
return 0;
}
Q1. C++ program to read and print employee information with department and
print information using hierarchical inheritance.
Ans 1.
#include
<iostream> using
namespace std;
class A
{
public:
float x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B :
public A
{
public:
void product()
{
cout << "\nProduct = " << x * y;
}
};
class C :
public A
{
public:
void sum()
{
cout << "\nSum = " << x + y;
}
};
class D : public A
{
public:
void divide()
{
cout << "\nDivide = " << x /y;
}
};
class E :
public A
{
public:
void subtraction()
{
cout << "\nSubtraction = " << x - y;
}
};
int main()
{
B
obj1;
C
obj2;
D
obj3;
E
obj4;
obj1.getdata()
;
obj1.product()
;
obj2.getdata();
obj2.sum();
obj3.getdata();
obj3.divide();
obj4.getdata();
obj4.subtraction
(); return 0;
}
Q2. C++ program to read and print student’s information using two classes and simple
inheritance.
Ans – 2
Code-
#include
<iostream> using
namespace std;
class A
{
public:
float x, y;
void getdata()
{
cout << "\nEnter value of x and
y:\n"; cin >> x >> y;
}
};
class B :
public A
{
public:
void product()
{
cout << "\n your product = " << x * y;
}
void subtraction()
{
cout << "\nSubtraction = " << x - y;
}
void divide()
{
cout << "\nDivide = " << x / y;
}
void sum()
{
cout << "\nSum = " << x + y;
}
};
int main()
{
B obj1;
obj1.getdata();
obj1.product();
obj1.sum();
obj1.divide();
obj1.subtraction
(); return 0;
}
Q3. Write a Program to generate a Fibonacci series by overloading (a) Prefix
Operator (b) Postfix Operator.
Ans -
3
Code-
#include
<iostream> using
namespace std;
class fibonnaci
{
private :
unsigned long int
f0,f1,fib; public :
fibonnaci();
// fibonnaci operator();
//constructor fibonnaci
operator++(int );
void display();
};
fibonnaci :: fibonnaci ()
{
f0=-
1;
f1=1;
fib=f0+f1;
}
void fibonnaci:: display()
{
cout << fib << '\t';
}
fibonnaci fibonnaci :: operator++ (int x)
{
f0=f1;
f1=fib;
fib=f0+f1;
return *this;
}
int main()
{
fibonnaci
obj; int n;
cout << " how many fibonnaci numbers are
to be"; cout << "displayed? \n";
cin >> n;
for (int i = 0; i <= n-1; ++i)
{
obj.display(
); obj++;
}
}
Q4 Write a C++ program to overload unary operators that is increment and
decrement.
Ans –
4
Code-
#include<iostream
> using
namespace std;
class NUM
{
private:
int n;
public:
void getNum()
{
cout<<"Enter value
= "; cin>>n;
}
void dispNum(void)
{
cout << "value of n is: " << n;
}
void operator ++ (void)
{
n=++n;
}
void operator -- (void)
{
n=--n;
}
};
int main()
{
NUM num;
num.getNum(
);
++num;
cout << "After
increment - ";
num.dispNum();
cout << endl;
num.getNum();
--num;
cout << "After
decrement - ";
num.dispNum();
cout <<
endl;
return 0;
}
Q5. Write a C++ program to define a base class Item (item-no, name, price). Derive
a class Discounted-Item (discount-percent). A customer purchases 'n' item. Display the
item-wise bill and total amount using appropriate format.
Ans –
5
Code-
#include
<iostream> using
namespace std;
class Item
{
public:
int itemno;
float
price[10];
char
itnm[20];
void getdata()
{
cout << "Enter
name "; gets(itnm);
cout<<"Enter no. of items = ";
cin>>itemno;
cout<<"Enter price of each
items\n"; for(int i=0;i<itemno;i+
+)
{
cout<<"Item no
"<<i+1<<" = ";
cin>>price[i];
}
}
};
class DiscountedItem : public Item
{
public:
float total[10],discount[10];
void discounts()
{
cout<<"Enter discount percentage in each
items \n"; for(int i=0;i<itemno;i++)
{
cout<<"Item no
"<<i+1<<" = ";
cin>>discount[i];
total[i]=price[i]-(price[i])*(discount[i]/100);
}
}
void showdata()
{
cout <<"Name = "<<itnm<<endl;
cout<<"No. of items =
"<<itemno<<endl;
cout<<"Item no "<<" Price "<<" Discount "<<" Total
"<<endl; for(int i=0;i<itemno;i++)
{
cout<<i+1<<" \t "<<price[i]<<" \t "<<discount[i]<<" \t "<<total[i]<<endl;
}
}
};
int main()
{
DiscountedItem obj;
obj.getdata();
obj.discounts(
);
obj.showdata(
); return 0;
}
Q6. Write a C++ program to overload binary operator '+' to add two complex
numbers.
Ans – 6
#include
<iostream> using
namespace std;
class Complex
{
private:
float real;
float
imag;
public:
Complex(): real(0),
imag(0){ } void input()
{
cout << "Enter real and imaginary parts
respectively: "; cin >> real;
cin >> imag;
}
// Operator overloading
Complex operator -
(Complex c2)
{
Complex temp;
temp.real = real -
c2.real;
temp.imag = imag -
c2.imag; return temp;
}
void output()
{
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main()
{
Complex c1, c2, result;
cout<<"Enter first complex number:\n";
c1.input();
cout<<"Enter second complex
number:\n"; c2.input();
// In case of operator overloading of binary operators in C++ programming,
// the object on right hand side of operator is always assumed as argument by
compiler. result = c1 - c2;
result.output()
; return 0;
}
Q7. Write a Program to Bubble Sort Using template function.
Ans – 7
#include
<iostream> using
namespace std;
template <class l>
void bubbleSort(l a[], int
n) { for (int i = 0; i < n
- 1; i++)
for (int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
int main()
{
int n;
cout<<"Enter size of array you want
= "; cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
bubbleSort(a, n);
cout << "Sorted array usin bubble
sort : "; for (int i = 0; i < n; i++)
cout << a[i] <<
" "; cout << endl;
return 0;
}
Q8. Write a program to demonstrate the
destructor. Ans – 8
Code-
#include<iostream
> using
namespace std;
class FLOAT
{
public:
float
a;
FLOA
T()
{
a=10;
}
void take()
{
cout<<"Enter value
please = "; cin>>a;
}
void show()
{
cout<<"All arthematic operations
are \n "; cout<<"Addition =
"<<a+a<<"\n"; cout<<"Subtraction
= "<<a-a<<"\n";
cout<<"Multiplication =
"<<a*a<<"\n"; cout<<"Division =
"<<a/a<<"\n";
}
~FLOAT()
{
cout<<"Destructor called successfully!!";
}
};
int main()
{
FLOAT ob;
ob.take();
cout<<"By user
input \n"; ob.show();
cout<<"By user default
constructor \n"; ob.show();
}
Q9. Write a C++ program to calculate the percentage of a student using multi-level
inheritance. Accept the marks of three subjects in base class. A class will be derived
from the above-mentioned class which includes a function to find the total marks
obtained and another class derived from this class which calculates and displays the
percentage of student
Ans –
9
Code-
#include<iostream
> using
namespace std;
class A
{
public:
char nm[20];
float
marks[3];
void take()
{
cout<<"Enter your
name = "; gets(nm);
cout<<"Enter your marks of 3
subjects = \n"; for(int i=0;i<3;i++)
{
cin>>marks[i];
}
}
};
class B:public A
{
public:
float
total=0;
void
totals()
{
for(int i=0;i<3;i++)
{
total=total+marks[i];
}
}
};
class C:public B
{
public:
float per;
void pers()
{
per=total/3;
}
void show()
{
cout<<"Name = "<<nm<<endl;
cout<<"Total Marks =
"<<total<<endl;
cout<<"Percentage =
"<<per<<endl;
}
};
int main()
{
C ob;
ob.take();
ob.totals();
ob.pers();
ob.show();
return 0;
}
Q10. Create a C++ class for player object with the following attributes player no.,
name, number of matches and number of goals done in each match. The number of
matches varies for each player. Write parameterized constructor which initializes player
no., name, number of subjects and creates array for number of goals and number of
matches dynamically.
Ans –
10
Code-
#include<iostream
> using
namespace std;
class Player
{
int player_no,
no_of_matches; char
player_name[100];
int
*goals;
public:
Player()
{
int i;
cout<<"\n Enter Player
No. : "; cin>>player_no;
cout<<"\n Enter Player
Name : ";
cin>>player_name;
cout<<"\n Enter No. of
Matches : ";
cin>>no_of_matches;
goals=new int
[no_of_matches]; for(i=0;
i<=no_of_matches - 1; i++)
{
cout<<"\n Enter No. of Goals in Match
"<<i+1<<" : "; cin>>goals[i];
}
}
void display()
{
int i;
cout<<"\n ";
cout<<"\n Player No. :
"<<player_no; cout<<"\n Player
Name : "<<player_name;
cout<<"\n No. of Matches Played :
"<<no_of_matches; for(i=0; i<=no_of_matches - 1;
i++)
{
cout<<"\n Match "<<i+1<<" Goals :
"<<goals[i];
}
}
};
int main()
{
Player P;
P.displ
ay();
return
0;
}
Q 1 ) Copy data of one object into another using copy constructor.
#include<iostream>
using namespace std;
class Person
{
int age;
string name;
int standard;
public:
Person(int a,string n,int s) //para constructor
{
age = a;
name = n;
standard = s;
}
Person(const Person& p2) //copy constructor
{
age = p2.age;
name = p2.name;
standard = p2.standard;
}
void display()
{
cout<<"copied data members of object p1 into p3:\n"<<"age =
"<<age<<"\n"<<"name = "<<name<<"\n"
<<"standard = "<<standard<<endl;
}
};
int main()
{
Person p1(19,"Shivam",12);
Person p3 = p1;
p3.display();
}
Q 2). Checking if given year is an olympic year.
#include<iostream>
using namespace std;
class Olympic
{
int year;
public:
void get_year()
{
cout<<"enter year:"<<endl;
cin>>year;
}
bool check_olympic()
{
if(1896 <= year <= 1940 && year%4 == 0)
{
return true;
}
else if(1947 <= year <= 2020 && year%4 == 0 )
{
return true;
}
else
{
return false;
}
}
};
int main()
{
Olympic o1;
o1.get_year();
bool flag;
flag = o1.check_olympic();
if(flag)
{
cout<<"olympic year"<<endl;
}
else
{
cout<<"not an olympic year"<<endl;
}
}
Q 3) Overloading mathematical operators for floating point nos.
#include<iostream>
using namespace std;
class FLOAT
{
float a;
public:
FLOAT()
{
a = 0;
}
FLOAT( const FLOAT& obj)
{
a = obj.a;
}
void get_data()
{
cout<<"enter value:\n";
cin>>a;
}
void result()
{
cout<<a;
}
FLOAT operator + (const FLOAT& obj)
{
FLOAT res;
res.a = a + obj.a;
return res;
}
FLOAT operator -(const FLOAT& obj)
{
FLOAT res;
res.a = a - obj.a;
return res;
}
FLOAT operator *(const FLOAT& obj)
{
FLOAT res;
res.a = a * obj.a;
return res;
}
FLOAT operator /(const FLOAT& obj)
{
FLOAT res;
res.a = a / obj.a;
return res;
}
};
int main()
{
FLOAT obj1,obj2,obj3;
obj1.get_data();
obj2.get_data();
char c;
cout<<"enter operator:"<<endl;
cin>>c;
if(c=='+')
{
obj3 = obj1 + obj2;
}
else if(c=='-')
{
obj3 = obj1 - obj2;
}
else if(c=='*')
{
obj3 = obj1 * obj2;
}
else if(c=='/')
{
obj3 = obj1 / obj2;
}
obj3.result();
Q 4). Age Calculator
#include<iostream>
using namespace std;
class age
{
int YYYY,MM,DD;
public:
void get_year()
{
cout<<"Enter birth year(YYYY):\n";
cin>>YYYY;
}
void get_month()
{
cout<<"Enter birth month(MM):\n";
cin>>MM;
}
void get_date()
{
cout<<"Enter birth date(DD):\n";
cin>>DD;
}
void age_calculator()
{
int CD = 13;
int CM = 02;
int CY = 2020;
int month[] = { 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31 };
if (DD > CD) {
CD = CD + month[MM - 1];
CM = CM - 1;
}
if (MM > CM) {
CY = CY- 1;
CM = CM + 12;
}
int calculated_date = CD - DD;
int calculated_month = CM - MM;
int calculated_year = CY - YYYY;
cout<<calculated_year<<" years "<<calculated_month<<" months "<<calculated_date<<"
days "<<endl;
}
};
int main()
{
age p1;
p1.get_date();
p1.get_month();
p1.get_year();
p1.age_calculator();
}