c++ to calculate the sum of 2 interger no. by using constructor concept

//wap to calculate the sum of 2 interger no. by using constructor concept
#include<iostream>
using namespace std;
class sum
{
int a,b,s;
public:
sum(int ,int );
void display();
};
sum::sum(int x ,int y)
{
a=x;
b=y;
s=a+b;
}
void sum::display()
{
cout<<"The sum is : "<<s<<"\n";
}

int main()
{
sum ad=sum(10,20);
ad.display();
return (0);
}

OUT- The sum is : 30


//Add two number using constructor without display function.

#include<iostream>
using namespace std;

//class containing methods and variables.
class add
{
    int num1,num2,sum;
    public :
    add(int x,int y);
    
  
};

//defining methods using scope resolution opertor.

add::add(int x,int y)
{
num1=x;
num2=y;
    sum=x+y;
    cout<<"The sum is : "<<sum<<"\n";
}


int main()
{
    add b=add(10,20);
    
}

    OUT- The sum is : 30


Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation