Day 14 P@H
Day 14 P@H
CPP_Polymorphism_PH
Attempt : 1
Total Mark : 80
Marks Obtained : 80
Section 1 : Coding
Answer
// You are using GCC
#include <iostream>
#include <iomanip>
using namespace std;
class Types
{
public:
void print(int i)
{
cout<<i<<" ";
}
void print(string s)
{
cout<<s;
}
void print(float f)
{
cout<<fixed<<setprecision(2)<<f<<" ";
}
};
int main ()
{
Types t;
int i;
float f;
string s;
cin>>i>>f>>s;
t.print(i);
t.print(f);
t.print(s);
}
2. Overloading
Create two methods that add two numbers of different type like integer
and floats which has same function name add. Approach the problem
using overloading concept.
Example
Input
12
1.3 6.2
Output
Integer:3
Float:7.50
Explanation
Two integer values are added and printed 3. Then two float values are
added and printed as 7.50.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
class calc
{
public:
void add (int a,int b)
{
cout<<"Integer:"<<a+b;
}
void add (float x,float y)
{
cout<<fixed<<setprecision(2)<<"\nFloat:"<<x+y;
}
};
int main ()
{
int a,b;
float x,y;
cin>>a>>b;
cin>>x>>y;
calc c;
c.add(a,b);
c.add(x,y);
}
3. Mathematical Operations
Write a program defining 3 functions with the same name which performs
the mathematical operations of Square, Modulus, and Division.
Function for Square takes in one integer argument and returns an integer
valueFunction for Modulus takes 2 integer arguments and returns an
integer valueFunction for Division takes 2 floating-point arguments and
returns and float valueExample
Input
2 5 6.3 5.2
Output
Square of 2: 4
Modulus of 2, 5: 2
Quotient of 6.3, 5.2: 1.21
Explanation
Apply mathematical operations like square, modulus and division for the
given input.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int calc(int a)
{
cout<<"Square of "<<a<<": "<<a*a<<endl;
}
int calc(int a,int b)
{
cout<<"Modulus of "<<a<<", "<<b<<": "<<a%b<<endl;
}
int calc(float x,float y)
{
cout<<"Quotient of "<<x<<", "<<y<<": "<<fixed<<setprecision(2)<<x/y;
}
int main()
{
int a,b;
cin>>a;
cin>>b;
calc(a);
calc(a,b);
float x,y;
cin>>x>>y;
calc(x,y);
return 0;
4. Function Overloading
An ice-cream vendor sells his ice-creams in a cone(radius r and height h)
and square(side r) shaped containers. However, he is unsure of the
quantity that can be filled in the two containers. You are required to write a
program that prints the volume of the containers given their respective
dimensions as input. Your class must be named ‘Icecream’ which has two
methods with the same name ‘Quantity’ each having the respective
dimensions of the containers as the parameters.
Function Header:
void Quantity(int r, int h)
void Quantity(int r)
Formulas:
Volume of a cone = 0.33*pi*r*r*h.
Volume of a square = r*r*r.
Example
Input
1
4
Output
64.00
Explanation
Choice 1: So Volume of Square for 4 is 64.
Answer
// You are using GCC
#include <iostream>
#include<iomanip>
using namespace std;
class Icecream
{
public:
void Quantity(float r,float h)
{
cout<<fixed<<setprecision(2)<<0.33*(3.14159)*r*r*h;
}
void Quantity(float r)
{
cout<<fixed<<setprecision(2)<<r*r*r;
}
};
int main()
{
Icecream i;
int n;
cin>>n;
if(n==1)
{
float r;
cin>>r;
i.Quantity(r);
}
else if(n==2)
{
float r,h;
cin>>r>>h;
i.Quantity(r,h);
}
return 0;
}
5. Function Overloading
Ram is given two or three inputs as an integer, if he has two integers then
add the two numbers. If he has three inputs, then multiply the three
numbers. Write a program to implement function overloading.
Function Header:
void fun1(int a,int b,int c)
void fun1(int a,int b)
Example1
Input
3123
Output
6
Explanation
Totally 3 input read so 1*2*3=6.
Example2
Input
2 14 56
Output
70
Explanation
Totally 2 input read so 14+56=70.
Example3
Input
4 67 89 43 21
Output
Invalid Input
Explanation
Totally 4 input read so Invalid input. Only 2 or 3 input values allowed.
Answer
// You are using GCC
#include<iostream>
using namespace std;
void fun1(int a,int b,int c)
{
cout<<a*b*c;
}
void fun1(int a,int b)
{
cout<<a+b;
}
int main()
{
int n,a,b;
cin>>n;
if(n>0&&n<4)
{
if(n==3)
{
int a,b,c;
cin>>a>>b>>c;
fun1(a,b,c);
}
if(n==2)
{
int a,b;
cin>>a>>b;
fun1(a,b);
}
}
else
{
cout<<"Invalid Input";
}
return 0;
}
6. Loan Calculator
For the clients, Bank XYZ need to calculate an education loan and a gold
loan. As a result, they built a loan class with a loadCalc(). The amount,
year, and interest percent are required inputs for calculating an education
loan. The amount and interest % are necessary inputs for calculating a
gold loan. Apply method overloading to calculate total amount need to be
paid for interest x. Print the monthly amount need to be paid by the client.
Note:
Education loan doesn't have interest upto 4 years. So apply interest if the
year is greater than 4, otherwise interest is 0%.
Example1
Input
1
1000
5
2
Output
Amount with interest 1020.0
Explanation
Year greater than 5 , So interest applied .2% of 1000 is 20. So total amount
to be paid is 1000+20 =1020.
Example2
Input
1
1000
4
2
Output
Amount with interest 1000.0
Explanation
Year less than 5, so interest is not applied. Therefore, total amount to be
paid is 1000.
Example3
Input
2
1000
2
Output
Amount with interest 1020.0
Explanation
2% of 1000 is 20. So total amount to be paid is 1000+20 =1020.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
class loan
{
public:
void loadCalc(float p ,float n ,float r)
{
if(n>4)
{
cout<<fixed<<setprecision(1)<<"Amount with interest "<<p+(p*r/100);
}
else
{
cout<<fixed<<setprecision(1)<<"Amount with interest "<<p;
}
}
void loadCalc(float p ,float i)
{
cout<<fixed<<setprecision(1)<<"Amount with interest "<<p+(p*i/100);
}
};
int main ()
{
loan l;
int c;
cin>>c;
if(c==1)
{
float p,n,r;
cin>>p;
cin>>n;
cin>>r;
l.loadCalc(p,n,r);
}
else if (c==2)
{
float p,i;
cin>>p;
cin>>i;
l.loadCalc(p,i);
}
}
8. REWARD CALCULATION
A new announcement has been made by the Mayor, the Fair will be on for
more than a month. For rewarding customers who actively purchase in the
fair, the developers are asked to compute reward points for credit card
purchasing. For a small demo implementation, we now compute reward
points for VISA cards and HP VISA cards.
Reward Points:
The reward points for the VISA card are 1% of the spending for all kinds of
purchases.
For HP Visa cards, like VISA cards, 1% of the spending for all kinds of
purchases and also 10 additional points are given for Fuel purchases.
Create a class named VISACard as Parent class with the following
attributes and methods.
Attributes: holder_name, card_number
Method Name:
display_details
This method displays the cardholder name and card number
compute_reward_points
This method accepts the purchase_type and amount as inputs and
displays the reward points which is 1% of the purchase amount.
Create a child class HPVISACard of parent class VISACard.
Method Name: compute_reward_points
This method accepts the purchase_type and amount as inputs and
displays the reward points which is 1% of the purchase amount. If the
purchase_type is 'Fuel' (case sensitive) 10 more points are given.
Strictly adhere to the Object-Oriented specifications given in the problem
statement
Answer
// You are using GCC
#include <iostream>
#include <iomanip>
using namespace std;
class VISAcard
{
private:
string holder_name,card_number;
public:
void display_details()
{
cin>>holder_name;
cin>>card_number;
cout<<holder_name<<endl<<card_number<<endl;
}
void compute_reward_points(string s,string f,float amt)
{
if(s=="VISA")
{
cout<<fixed<<setprecision(2)<<amt*0.01;
}
else if(s=="HPVISA")
{
if(f=="Fuel")
cout<<fixed<<setprecision(1)<<(amt*0.01)+10;
else
cout<<fixed<<setprecision(1)<<amt*0.01;
}
}
};
int main()
{
VISAcard v;
string s,f;
cin>>s;
if(s =="VISA" || s == "HPVISA")
{
v.display_details();
float amt;
cin>>amt;
cin>>f;
v.compute_reward_points(s,f,amt);
}
else
{
cout<<"Invalid Choice";
}
}