Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Sri Krishna College of Engineering and Technology

Name :Dinesh Karthikeyan G M Email :23ee026@skcet.ac.in


Roll no:23ee026 Phone :9942227000
Branch :SKCET Department :EEE-A
Batch :2023_27 Degree :BE-EEE

2023_27_I_PS using CPP_IRC

CPP_Inheritance_PAH

Attempt : 1
Total Mark : 80
Marks Obtained : 77.5

Section 1 : Coding

1. Hierarchical Inheritance
Create a class 'Parent'. 'Parent' class should have a method 'add' which
prints the addition of 2 integers. Create a class 'child1' which should be a
'child' class of 'parent' class.it has a method 'sub' which prints subtraction
of 2 integers. Create a class 'child2' which should be a child class of
'Parent' class. 'Child2' class should have a method 'mul' which prints
multiplication of 2 integers.
Create an object for a 'Child1' class. Call the 2 methods to add and sub
from the child1 class object and display the result. Create an object for a
'Child2' class. Call the method mul from the child2 class object and display
the result.
Example
Input
12
34
Output
46
-22
408
Explanation
Perform addition, subtraction and multiplication for the given input and
print it.

Answer
//you are using GCC
#include <iostream>
class Parent {
public:
void add(int a, int b) {
std::cout << a + b << std::endl;
}
};
class Child1 : public Parent {
public:
void sub(int a, int b) {
std::cout << a - b << std::endl;
}
};
class Child2 : public Parent {
public:
void mul(int a, int b) {
std::cout << a * b << std::endl;
}
};
int main() {
int integer1, integer2;
std::cin >> integer1 >> integer2;
Child1 child1;
Child2 child2;
child1.add(integer1, integer2);
child1.sub(integer1, integer2);
child2.mul(integer1, integer2);
return 0;
}
Status : Correct Marks : 10/10

2. Multiple Inheritance
Create a class 'Parent',it should have a method 'add' which prints the
addition of 2 integers. Create a class 'parent2' which is another 'parent'
class that should have a method 'sub' which prints subtraction of 2
integers. Create a class 'Child' which should be a child class of 'Parent'
class and 'parent2' class. 'Child' class should have a method 'mul' which
prints multiplication of 2 integers. Create an object for a 'Child' class. Call
the 3 methods to add, sub and mul from child class object and display the
result.
Example
Input
34
56
Output
90
-22
1904
Explanation
Perform addition, subtraction and multiplication for the given input and
print it.

Answer
// You are using GCC
#include <iostream>
class Parent {
public:
void add(int a, int b) {
std::cout << a + b << std::endl;
}
};
class Parent2 {
public:
void sub(int a, int b) {
std::cout << a - b << std::endl;
}
};
class Child : public Parent, public Parent2 {
public:
void mul(int a, int b) {
std::cout << a * b << std::endl;
}
};
int main() {
int integer1, integer2;
std::cin >> integer1 >> integer2;
Child child;
child.add(integer1, integer2);
child.sub(integer1, integer2);
child.mul(integer1, integer2);
return 0;
}

Status : Correct Marks : 10/10

3. Person Detail
Create a class 'Person'. This is a parent class that contains two attributes
that is person name and age. Create a method to display name and age.
Create another class 'Student'. This is also a parent class that contains one
attribute that is student id. Create a method to display student id. Create a
child class 'Resident' which inherits a two-parent class that is Person and
Student class.
Create an object for Resident class and call all the methods through child
object and display the details. Refer sample input and output for
formatting specifications. Note: Use the same class names as mentioned
above in the code.
Example
Input
ANANDHI
20
16001
Output
ANANDHI
20
16001
Explanation
Using inheritance concept print the properties of parent class by creating
object for child class.

Answer
// You are using GCC
// You are using GCC
#include <iostream>
#include <string>
class Person {
protected:
std::string name;
int age;
public:
Person(const std::string& n, int a) : name(n), age(a) {}
void display() {
std::cout << name << std::endl;
std::cout << age << std::endl;
}
};
class Student {
protected:
int studentID;
public:
Student(int id) : studentID(id) {}
void display() {
std::cout << studentID << std::endl;
}
};
class Resident : public Person, public Student {
public:
Resident(const std::string& n, int a, int id) : Person(n, a), Student(id) {}
void displayAll() {
Person::display();
Student::display();
}
};
int main() {
std::string name;
int age, studentID;

std::cin >> name >> age >> studentID;

Resident resident(name, age, studentID);


resident.displayAll();

return 0;
}

Status : Partially correct Marks : 7.5/10

4. Salary Calculator
Create a parent class as Employee with empId (int ) attribute and create a
child class as salary with attribute working_hours (int).
Calculate the total salary inside the child class.
Note:
Total salary = working_hours*50.
Example
Input
2
500
Output
Employee Id:2
No. of Hours Worked = 500
Total Salary = 25000
Explanation
Evaluate total salary by finding the product of hours worked by 50.

Answer
// You are using GCC
#include <iostream>
class Employee {
protected:
int empId;
public:
Employee(int id) : empId(id) {}
void displayEmployeeId() {
std::cout << "Employee Id:" << empId << std::endl;
}
};
class Salary : public Employee {
private:
int workingHours;
public:
Salary(int id, int hours) : Employee(id), workingHours(hours) {}
void calculateSalary() {
int totalSalary = workingHours * 50;
std::cout << "No. of Hours Worked = " << workingHours << std::endl;
std::cout << "Total Salary = " << totalSalary << std::endl;
}
};
int main() {
int empId, workingHours;
std::cin >> empId >> workingHours;
Salary salary(empId, workingHours);
salary.displayEmployeeId();
salary.calculateSalary();
return 0;
}

Status : Correct Marks : 10/10


5. Digit Sum
Write a program to implement the following logic using inheritance. Create
a parent class and implement the fun method. In the method, get the
individual digits of the entered number, store it in an array, and find their
sum. Create the main class that inherits the parent class and call the fun
method inside the parent function.
Example
Input
1234
Output
30
Explanation
For 1234, the individual digits are 4,3,2,1 and the final sum
(4+3)+(4+2)+(4+1)+(3+2)+(3+1)+(2+1) = 30.

Answer
// You are using GCC
#include <iostream>
#include <vector>
class Parent {
public:
void fun(int number) {
std::vector<int> digits;
int temp = number;
while (temp > 0) {
digits.push_back(temp % 10);
temp /= 10;
}
int sum = 0;
for (int i = 0; i < digits.size(); i++) {
for (int j = i + 1; j < digits.size(); j++) {
sum += (digits[i] + digits[j]);
}
}
std::cout << sum << std::endl;
}
};
int main() {
int number;
std::cin >> number;
Parent parent;
parent.fun(number);
return 0;
}

Status : Correct Marks : 10/10

6. Problem Statement:
Write a program with two classes Vehicle as the base class and Car as the
derived class, which inherits the properties from the Vehicle class for
calculating the speed. The base class fetches the input as a float value
whereas the derived class calculates and prints the output as a float value.
Note: Use public inheritance.

Answer
// You are using GCC
#include <iostream>
class Vehicle {
public:
float distance;
float time;
void getInput() {
std::cin >> distance >> time;
}
};
class Car : public Vehicle {
public:
void calculateSpeed() {
float speed = distance / time;
std::cout << "Speed of car: " << speed << " km/hr" << std::endl;
}
};
int main() {
Car car;
car.getInput();
car.calculateSpeed();
return 0;
}

Status : Correct Marks : 10/10

7. Problem Statement:
You are given three classes A, B and C. All three classes implement their
own version of func.
In class A, func multiplies the value passed as a parameter by 2
In class B, func multiplies the value passed as a parameter by 3
In class C, func multiplies the value passed as a parameter by 5
Implement class D:

You need to modify the class D and implement the function update_val
which sets D's val to new_val by manipulating the value by only calling the
func defined in classes A, B and C.

It is guaranteed that new_val has only 2,3 and 5 as its prime factors.

Answer
// You are using GCC
#include<iostream>
using namespace std;
int main(){
int a,A=0,B=0,C=0;
cin>>a;
int n = a; while(n!=1){
if(n%2==0){ n = n/2;
A++; }if(n%3==0){
n = n/3;
B++; }if(n%5==0){
n = n/5;
C++; }
}
cout<<"Value = "<<a<<endl;
cout<<"A's func called "<<A<<" times "<<endl;
cout<<"B's func called "<<B<<" times"<<endl;
cout<<"C's func called "<<C<<" times"<<endl;
}

Status : Correct Marks : 10/10

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 card and HP VISA card. The reward points for VISA card is
1% of the spending for all kinds of purchases. For HP Visa card, 10
additional points are given for fuel purchases. Also, include method
Overriding for the method computeRewardPoints() which computes the
reward points for both types. write a program using the above specification
for computing the reward points.
Create a class named VISACard with the following private attributes

the VISACard class has the following methods

Create a class named HPVISACard that extends VISACard with the


following methods

Create a driver class called Main. In the Main method, obtain inputs from
the user and compute the reward points by calling appropriate methods. If
choice other than specified is chosen, print "Invalid choice"
Note: Display two digit after the decimal point for Double values.
[Strictly adhere to the Object-Oriented Specifications given in the problem
statement.
All class names, attribute names and method names should be the same
as specified in the problem statement.]

Answer
// You are using GCC
#include <iostream>
#include <string>
#include <iomanip>
class VISACard {
protected:
std::string accountHolderName;
std::string cardNumber;
double amount;
public:
VISACard(const std::string& name, const std::string& number, double amt)
: accountHolderName(name), cardNumber(number), amount(amt) {}
virtual void computeRewardPoints() {
double rewardPoints = amount * 0.01;
std::cout << std::fixed << std::setprecision(2) << rewardPoints << std::endl;
}
};
class HPVISACard : public VISACard {
private:
std::string purchaseType;
public:
HPVISACard(const std::string& name, const std::string& number, double amt,
const std::string& purchase): VISACard(name, number, amt),
purchaseType(purchase) {}
void computeRewardPoints() override {
double rewardPoints = amount * 0.01;
if (purchaseType == "fuel") {
rewardPoints += 10.0;
}
std::cout << std::fixed << std::setprecision(2) << rewardPoints << std::endl;
}
};
int main() {
std::string name, cardNumber, purchaseType;
double amount;
int choice;
std::cin >> name >> cardNumber >> amount >> purchaseType >> choice;
if (choice == 1) {
VISACard visaCard(name, cardNumber, amount);
visaCard.computeRewardPoints();
} else if (choice == 2) {
HPVISACard hpVisaCard(name, cardNumber, amount, purchaseType);
hpVisaCard.computeRewardPoints();
} else {
std::cout << "Invalid Choice" << std::endl;
}
return 0;
}

Status : Correct Marks : 10/10

You might also like