C++ Program to add two numbers using constructor overloading
In this program, You will learn how to add two numbers using constructor overloading in C++.
class Test{
//statement
};
int main(){
//statement
}
Example: How to add two numbers using constructor overloading in C++.
#include<iostream>
using namespace std;
class Test {
int c;
public:
Test() {
cout << "Sum is:";
}
Test(int a, int b) {
c = a + b;
}
void display() {
cout << c;
}
};
int main() {
int a, b;
cout << "Enter two numbers:";
cin >> a>>b;
Test t;
Test tt(a, b);
tt.display();
return 0;
}
Output:
Enter two numbers:10 30
Sum is:40