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

Object Oriented Programming Assignment 1

The document contains programming assignments for a course in Object Oriented Programming, detailing three questions. Question 1(A) requires a C++ program to calculate the area and volume of rectangular objects using function overloading. Questions 1(B) and 1(C) involve creating classes for calculating volumes of cones and cylinders using polymorphism, and calculating interest on a principal amount using a constructor, respectively.

Uploaded by

neviladnuno
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)
2 views

Object Oriented Programming Assignment 1

The document contains programming assignments for a course in Object Oriented Programming, detailing three questions. Question 1(A) requires a C++ program to calculate the area and volume of rectangular objects using function overloading. Questions 1(B) and 1(C) involve creating classes for calculating volumes of cones and cylinders using polymorphism, and calculating interest on a principal amount using a constructor, respectively.

Uploaded by

neviladnuno
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/ 4

SCHOOL OF COMPUTING AND MATHEMATICS

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY

UNITNAME: OBJECT ORIENTED PROGRAMMING

UNIT CODE:BCIT 2122

LECTURER:DR SILAS MAIYO

Group members

Jabez kanisa- C026/401159/2023

Delmas Owino-C026/401140/2023

Emelda valentine-C026/401041/2023

Mercy kamau--BBITC01/0324/2020

Yvone Muturi --BBITC01/1235/2022


Question 1(A)

a) Write a C++ program that uses an overloaded function name calculate to determine the area
or volume of a rectangular object based on the number of parameters provided. The program
should determine and output the area or volume appropriately for the Object1(12, 8) and
Object2(6, 5, 4). Us the function prototypes

#include <iostream>
using namespace std;

// Function prototypes
double calculate(double length, double width); // Area for rectangle
double calculate(double length, double width, double height); // Volume for rectangular prism

int main() {
// Calculate area for Object1
double area = calculate(12, 8);
cout << "Area of Object1 (12, 8): " << area << endl;

// Calculate volume for Object2


double volume = calculate(6, 5, 4);
cout << "Volume of Object2 (6, 5, 4): " << volume << endl;

return 0;
}

// Function definitions
double calculate(double length, double width) {
return length * width; // Area
}

double calculate(double length, double width, double height) {


return length * width * height; // Volume
}
Question 1(B)
b) Tom would like to use a polymorphism class to determine the volume of cones and cylinders. Write a
C++ program that he would use to:
Define a class named threeD that has data members named radius and height, a parametric member
function named set which is used to initialize the value of
radius and height and a pure virtual function named volume;
Implement two derived classes from threeD named cone and cylinder whose
radius and height are 14m and 8m respectively.
The program should then output the volume for the cone and cylinder. Use pie as 3.142 and pointers
appropriately

#include <iostream>
#include <cmath>
using namespace std;

class threeD {
protected:
double radius;
double height;
public:
virtual void set(double r, double h) {
radius = r;
height = h;
}
virtual double volume() = 0; // Pure virtual function
};
class cone : public threeD { public:
double volume() override {
return (1.0 / 3.0) * 3.142 * radius * radius * height; // Volume of cone
};
class cylinder : public threeD {
public:
double volume() override {
return 3.142 * radius * radius * height; // Volume of cylinder
}
};
Int main() {

threeD* shape1 = new cone();


shape1->set(14, 8); // Setting radius and height for cone cout
<< "Volume of cone: " << shape1->volume() << endl;
threeD* shape2 = new cylinder();
shape2->set(14, 8); // Setting radius and height for cylinder
cout << "Volume of cylinder: " << shape2->volume() << endl;
// Clean up
delete shape1;
delete shape2;

return 0;
}

Question 1(C)
c) Write a C++ program that implements a class with the following properties:

Data members as a, b, and c i.e principal amount, interest rate p.a and number of years respectively that
are initialized as 100,000, 0.2, and 5;Member function for calculating and outputting the interest on the
principal amount

Use constructor.
#include <iostream>
using namespace std;
class InterestCalculator {
private:
double a; // Principal amount
double b; // Interest rate (p.a)
int c; // Number of years
Public:
// Constructor
InterestCalculator(double principal, double rate, int years) : a(principal), b(rate), c(years) {}

void calculateInterest() {
double interest = a * b * c; // Interest calculation
cout << "Interest on Principal Amount: " << interest << endl;
}
};
Int main() {

InterestCalculator interest(100000.0, 0.2, 5); // Initializing the class with given values
interest.calculateInterest(); // Calculating and outputting interest
return 0;
}

You might also like