Knowee
Questions
Features
Study Tools

Create a class Calculator with overloaded methods for addition:A method add(int a, int b) to add two integers.A method add(float a, float b) to add two floating-point numbers.A method add(const std::string& a, const std::string& b) to add two strings representing numbers.A method add(int a, int b, int c) to add three integers.Implement a main program that:Creates an instance of Calculator.Calls the overloaded add methods with different argument types and counts.Displays the results for each addition operation.

Question

Create a class Calculator with overloaded methods for addition:A method add(int a, int b) to add two integers.A method add(float a, float b) to add two floating-point numbers.A method add(const std::string& a, const std::string& b) to add two strings representing numbers.A method add(int a, int b, int c) to add three integers.Implement a main program that:Creates an instance of Calculator.Calls the overloaded add methods with different argument types and counts.Displays the results for each addition operation.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the implementation in C++:

#include <iostream>
#include <string>

class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }

    float add(float a, float b) {
        return a + b;
    }

    std::string add(const std::string& a, const std::string& b) {
        return std::to_string(std::stod(a) + std::stod(b));
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }
};

int main() {
    Calculator calc;

    std::cout << "Addition of two integers: " << calc.add(2, 3) << std::endl;
    std::cout << "Addition of two floats: " << calc.add(2.5f, 3.5f) << std::endl;
    std::cout << "Addition of two strings: " << calc.add("2.5", "3.5") << std::endl;
    std::cout << "Addition of three integers: " << calc.add(2, 3, 4) << std::endl;

    return 0;
}

This program first defines a Calculator class with four overloaded add methods for different types and counts of arguments. Then in the main function, it creates an instance of Calculator and calls each of the add methods with appropriate arguments. The results of each addition operation are printed to the console.

This problem has been solved

Similar Questions

Write a program overloading arithmetic operators to add two complex numbers using oops c++

Write a function that adds 2 integers.Prototype: def add_integer(a, b=98):a and b must be integers or floats, otherwise raise a TypeError exception with the message a must be an integer or b must be an integera and b must be first casted to integers if they are floatReturns an integer: the addition of a and bYou are not allowed to import any module

Write a program to add two numbers using pointers

Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’,evaluates the following expressions and displays results on screen.num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3))For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators(both prefixing and postfixing are allowed). You must remember that using increment/decrement operatorschanges the original value of a number. Indent your code and include comments for improving the readabilityof your code. Your program should have the following interface.Enter a number : 2020 + 5 = 2520 – 3 = 17(20 + 3)- 2 = 21((20 + 5)*2 / (20 + 3)) = 2Enter a number : 2020 + 5 = 2520 – 3 = 17(20 + 3)- 2 = 21((20 + 5)*2 / (20 + 3)) = 2

Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.