0% found this document useful (0 votes)
10 views11 pages

OOP Console Calculator in C++

Assignment

Uploaded by

zjjscd6hvv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

OOP Console Calculator in C++

Assignment

Uploaded by

zjjscd6hvv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROJECCT #01

BSSE 2A
Instructor: Sir Sohail Ahmed
Group Members:
Saqlain Naeem (2480419)
Touseef Rashid Mughal (2480369)
Zaidan Ali Gul (2480295)
Final Report:

1. Abstract
This project is a simple yet powerful console-based calculator application
developed using Object-Oriented Programming (OOP) concepts in C++. It performs
a variety of mathematical operations including addition, subtraction, multiplication,
division, modulus, factorial, power, and square root. The purpose of this project is
to demonstrate the core principles of OOP such as encapsulation, abstraction,
function overloading, and data hiding.

2. Introduction
A calculator is a basic utility program that is widely used in academic, professional,
and daily life scenarios. In this project, a calculator system is implemented using
object-oriented principles. The user can choose from multiple operations using a
menu-based interface. Each operation is encapsulated within a class to ensure
modularity and reusability.

3. Objectives
 To implement a console-based calculator using C++ OOP concepts.
 To understand and apply principles like classes, objects, encapsulation, and
abstraction.
 To create a user-friendly menu system for performing different operations.
 To handle mathematical errors such as division by zero and invalid inputs.

4. Tools and Technologies Used


Component Description
Language C++
Dev-C++, Code::Blocks, Turbo C++, Visual Studio or any C++
Compiler
compiler
Paradigm Object-Oriented Programming
Platform Console Application

5. Literature Review
Calculator programs have been widely used for learning programming concepts.
Traditional calculators are designed using procedural programming, whereas
modern applications focus on OOP-based modular systems. This project uses OOP
to enhance the scalability and readability of the code. The use of functions inside
class structures ensures secure and efficient code management.

6. Methodology
This project is developed using the following steps:
Step 1: Problem Identification
We identified that a calculator with multiple features is a suitable project to
demonstrate OOP concepts.
Step 2: System Design
A class named Calculator was created with private data members and public
functions to handle mathematical operations.
Step 3: Implementation
Operations such as addition, subtraction, multiplication, division, modulus,
factorial, power, and square root were implemented as separate methods in the
class.
Step 4: Testing
The program was compiled and tested with different inputs, including invalid cases
such as division by zero.

7. System Design
7.1 Class Diagram (Conceptual)
+----------------------+
| Calculator |
+----------------------+
| - num1: double |
| - num2: double |
+----------------------+
| + getTwoInputs() |
| + getSingleInput() |
| + add(): double |
| + subtract(): double |
| + multiply(): double |
| + divide(): double |
| + modulus(): int |
| + factorial(): long |
| + power(): double |
| + squareRoot(): double |
+----------------------+

8. Conclusion
This project demonstrates how Object-Oriented Programming can be applied to
build a modular and efficient application. The calculator is flexible, easy to modify,
and scalable. It serves as a foundational OOP project for beginners in C++.

9. Code
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;

class Calculator {
double num1;
double num2;

public:
// default constructor
Calculator() : num1(0.0), num2(0.0) {}

// set numbers (used when user inputs new values)


void setNumbers(double a, double b) {
num1 = a;
num2 = b;
}

// read numbers from stdin with basic validation


bool inputNumbers() {
cout << "Enter first number: ";
if (!(cin >> num1)) { clearInput(); cout << "Invalid input.\n"; return false; }
cout << "Enter second number: ";
if (!(cin >> num2)) { clearInput(); cout << "Invalid input.\n"; return false; }
return true;
}

// operations
double add() const { return num1 + num2; }
double subtract() const { return num1 - num2; }
double multiply() const { return num1 * num2; }
// returns pair<bool,double>: bool indicates success (false if divide by zero)
bool divide(double &result) const {
if (num2 == 0.0) return false;
result = num1 / num2;
return true;
}
double power() const { return pow(num1, num2); } // num1^num2
bool modulus(double &result) const { // works for integers
but with fmod for doubles
if (num2 == 0.0) return false;
result = fmod(num1, num2);
return true;
}
void showNumbers() const {
cout << "Current numbers -> num1: " << num1 << " , num2: " << num2 << "\
n";
}

private:
// helper to clear bad input
static void clearInput() {
[Link]();
[Link](numeric_limits<streamsize>::max(), '\n');
}
};

int main() {
Calculator calc;
int choice;
double result;

cout << "=== OOP Calculator ===\n";


do {
cout << "\nMenu:\n";
cout << "1. Enter numbers\n";
cout << "2. Show current numbers\n";
cout << "3. Add (num1 + num2)\n";
cout << "4. Subtract (num1 - num2)\n";
cout << "5. Multiply (num1 * num2)\n";
cout << "6. Divide (num1 / num2)\n";
cout << "7. Power (num1 ^ num2)\n";
cout << "8. Modulus (fmod)\n";
cout << "9. Exit\n";
cout << "Choose an option: ";

if (!(cin >> choice)) {


[Link]();
[Link](numeric_limits<streamsize>::max(), '\n');
cout << "Invalid choice. Please enter a number 1-9.\n";
continue;
}

switch (choice) {
case 1:
if (![Link]()) {
cout << "Failed to read numbers. Try again.\n";
} else {
cout << "Numbers stored.\n";
}
break;
case 2:
[Link]();
break;
case 3:
cout << "Result: " << [Link]() << "\n";
break;
case 4:
cout << "Result: " << [Link]() << "\n";
break;
case 5:
cout << "Result: " << [Link]() << "\n";
break;
case 6:
if ([Link](result)) cout << "Result: " << result << "\n";
else cout << "Error: Division by zero.\n";
break;
case 7:
cout << "Result: " << [Link]() << "\n";
break;
case 8:
if ([Link](result)) cout << "Result: " << result << "\n";
else cout << "Error: Modulus by zero.\n";
break;
case 9:
cout << "Exiting. Goodbye!\n";
break;
default:
cout << "Please choose a valid option (1-9).\n";
break;
}
} while (choice != 9);

return 0;
}

Output:

You might also like