C++ Examples

  1. Write a C++ Program to print a welcome text in a separate line.

Add two numbers in C++ using class.

#include <iostream>
using namespace std;

class AdditionUsingClass{
    
    private:
    
    int number1,number2,sum;
    
    public:
    
    int sumOfTwoNumbers(){
        cout<<"Enter two numbers: "<<endl;
       
        cin>>number1>>number2;
       
        sum = number1 + number2;
        
        cout<<"The sum of two numbers is ";
        
        return sum;
    }
};

int main()
{
    AdditionUsingClass add;
   
    cout<<add.sumOfTwoNumbers();

    return 0;
}

Output

Enter two numbers: 
5
4
The sum of two numbers is 9
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments