Implementing an Integer class - C++ Class

C++ examples for Class:Class Creation

Description

Implementing an Integer class

Demo Code

                                            
#include <iostream>
                                            
class Integer{//  w  ww  .j  ava  2 s  .c  o m
private:
  int n;
                                            
public:
  Integer(int m);
  int getValue() {return n;}
  void setValue(int m){ n = m; }
  void show();
};
                                            
Integer::Integer(int m)
{
  n = m;
  std::cout << "Object created." << std::endl;
}
                                            
void Integer::show()
{
  std::cout << "Value is " << n << std::endl;
}
                                            
int main()
{
  std::cout << "Create i with the value 10." << std::endl;
  Integer i {10};
  i.show();
  std::cout << "Change value  of i to 15." << std::endl;
  i.setValue(15);
  i.show();
                                            
}

Result


Related Tutorials