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.
SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////class Int
{
private:
int i;
public:
Int( ) :i(0)
{ /*Empty Body*/ }
Int(int n) :i(n)
{ /*Empty Body*/ }
void add(Int n2,Int n3)
{
i=n2.i+n3.i;
}
void display( ) const
{
cout << "Integer is = "
<< i << endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////void main( )
{
Int first, second(250), third(122);
first.add(second, third);
first.display( );
system("pause");
}
OUTPUT:
0 comments: