initialize with class name,adding the two initialized values and placing the response in the uninitialized value, and then displaying this result.
Create a class that imitates part of the functionality of the basic data type int . Call the class
Int (note different capitalization). The only data in this class is an int variable. Include
member functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks
just like an int ), and to add two Int values. Write a program that exercises this class by
creating one uninitialized and two initialized Int values, adding the two initialized values and
placing the response in the uninitialized value, and then displaying this result.
Understanding Things:
coding:
#include<iostream>
using namespace std;
class Int
{
int num,num1;
public:
Int()
{
}
Int(int n)
{
num=n;
}
int add(Int n1,Int n2)
{
num1=n1.num+n2.num;
return num1;
}
};
int main()
{
int sum;
Int n1(2),n2(3),n3;
sum=n3.add(n1,n2);
cout<<"sum of two number is:"<<sum;
return 0;
}
output:
0 comments: