Using a class to model an integer data type - C++ Class

C++ examples for Class:Class Creation

Description

Using a class to model an integer data type

Demo Code

#include <iostream>
using namespace std;
class Int/*  w  ww .  j a v  a 2  s  .  co m*/
{
   private:
   int i;
   public:
   Int()
   { i = 0; }
   Int(int ii)
   { i = ii; }
   void add(Int i2, Int i3)
   { i = i2.i + i3.i; }
   void display()
   { cout << i; }
};
int main(){
   Int Int1(7);                 //create and initialize an Int
   Int Int2(11);                //create and initialize an Int
   Int Int3;                    //create an Int
   Int3.add(Int1, Int2);                 //add two Ints
   cout << "\nInt3 = "; Int3.display();  //display result
   cout << endl;
   return 0;
}

Result


Related Tutorials