C++ Program to Add two Numbers using Class and Object
Here you will get and learn the C++ program to add two numbers using class and object in C++ programming.
Example of C++ Program to add two numbers using class and object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | //add two numbers using class and object in c++ #include <iostream> #include<conio.h> using namespace std; class ClassAdd { private: int n1, n2; public: int sum; void readData() { cout << "\nEnter First Number :"; cin >> n1; cout << "\nEnter Second Number :"; cin >> n2; } void sumData() { sum = n1 + n2; } void printResult() { cout << "\nResult :" << n1 << " + " << n2 << " = " << sum; } }; int main() { ClassAdd obj1, obj2; cout << "Addition of two numbers using class and object in c++\n"; cout << "\n\n********* Result using obj1***********\n"; obj1.readData(); obj1.sumData(); obj1.printResult(); cout << "\n\n********* Result using obj2***********\n"; obj2.readData(); obj2.sumData(); obj2.printResult(); getch(); return 0; } |
Output
Addition of two numbers using class and object in c++
********* Result using obj1***********
Enter First Number :4
Enter Second Number :5
Result :4 + 5 = 9
********* Result using obj2***********
Enter First Number :7
Enter Second Number :8
Result :7 + 8 = 15
Check out our other C++ programming Examples