FACULTY OF ENGINEERING
P. O. BOX 1, KYAMBOGO – P. O. BOX 7181 KAMPALA, UGANDA
Website: [Link] Email: civil@[Link] Tel: +256-41-4287340, FAX: +256-41-4289056/4222643
Department of Civil and Environmental Engineering
/* **************************************************************************
* TCBE 2202: COMPUTING FOR CIVIL ENGINEERING
* INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP)
* Project: Basic Shape Area Calculator for Engineering Students
* ************************************************************************** */
#include <iostream> // Header for Input/Output (cin/cout)
#include <cmath> // Header for Math functions (M_PI, pow)
using namespace std; // Standard namespace to simplify code syntax
// --- OBJECT-ORIENTED PROGRAMMING (OOP) SECTION ---
// A 'class' acts as a container for related data and functions.
class AreaCalculator {
public: // Access specifier: allows these functions to be used in main()
// Function for Circle Area: π * r^2
// 'double' is the standard data type for precise engineering decimals
double circle(double r) {
return M_PI * pow(r, 2); // pow(base, exponent) is a cmath function
}
// Function for Square Area: side * side
double square(double s) {
return s * s;
}
// Function for Triangle Area: 0.5 * base * height
double triangle(double b, double h) {
return 0.5 * b * h;
}
};
int main() {
// --- INITIALIZATION ---
AreaCalculator calc; // Creating an 'Object' named 'calc' from our class
int choice = 0; // Variable to store the user's menu selection
double val1, val2; // Variables for dimensions (radius, base, etc.)
cout << "=== Welcome to the C++ Engineering Calculator ===" << endl;
// --- ITERATION / LOOPING (WHILE LOOP) ---
// This loop repeats the menu until the user selects option 4 (Exit).
while (choice != 4) {
cout << "\nSelect a shape to calculate area:" << endl;
cout << "1. Circle\n2. Square\n3. Triangle\n4. Exit" << endl;
cout << "Enter choice (1-4): ";
// --- INPUT SECTION ---
cin >> choice;
// --- CONTROL OF FLOW (IF / ELSE IF / ELSE) ---
// These 'Selection' structures decide which math function to run.
if (choice == 1) { // Logic for Circle
cout << "Enter radius: ";
cin >> val1;
// Calling the object's function and printing the output
cout << "Result: Area of Circle is " << [Link](val1) << endl;
}
else if (choice == 2) { // Logic for Square
cout << "Enter side length: ";
cin >> val1;
cout << "Result: Area of Square is " << [Link](val1) << endl;
}
else if (choice == 3) { // Logic for Triangle
cout << "Enter base and height: ";
cin >> val1 >> val2; // Multiple inputs separated by space
cout << "Result: Area of Triangle is " << [Link](val1, val2) <<
endl;
}
else if (choice == 4) { // Logic to Close
cout << "Exiting... System shutting down." << endl;
}
else { // Logic for error handling (Semantics)
cout << "Invalid input. Please choose a number between 1 and 4." << endl;
}
}
return 0; // Standard successful program termination
}