C++ Online Compiler
Example: Sum of Two Numbers using Class with User Input in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Sum of Two Numbers using Class with User Input #include <iostream> using namespace std; // Define a class named 'InteractiveAdder' class InteractiveAdder { private: int num1; int num2; public: // Method to get numbers from the user void getNumbersFromUser() { cout << "Enter first number: "; cin >> num1; cout << "Enter second number: "; cin >> num2; } // Method to calculate the sum int calculateSum() { return num1 + num2; } // Method to display the sum void displaySum() { cout << "The sum of " << num1 << " and " << num2 << " is: " << calculateSum() << endl; } }; int main() { // Step 1: Create an object of the 'InteractiveAdder' class InteractiveAdder userAdder; // Step 2: Call the 'getNumbersFromUser' method to prompt for input userAdder.getNumbersFromUser(); // Step 3: Call the 'displaySum' method to show the result userAdder.displaySum(); return 0; }
Output
Clear
ADVERTISEMENTS