Object Oriented Programming Assignment 1
Object Oriented Programming Assignment 1
Group members
Delmas Owino-C026/401140/2023
Emelda valentine-C026/401041/2023
Mercy kamau--BBITC01/0324/2020
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;
return 0;
}
// Function definitions
double calculate(double length, double width) {
return length * width; // Area
}
#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() {
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;
}