Thursday, November 13, 2014

CLASS OF C++ PROGRAME



C++ hello world class program

#include
#include
          class message
{
  public:

    void display()
 {
      cout << "hello world\n";
    }
};

int main()
{
   message c;   
   c.display(); 
               return 0;
}

C++ addition program using class


#include 
#include 
class mathematics 
{
  int x, y;
 
public:
  void input()
 {
    cout << "input two inetegers\n";
    cin >> x >> y;
  }
 
  void add()
{
    cout << "result = " << x + y;
  }
 
};
 
int main()
{
   mathematics m;  
   m.input();
   m.add();
 
   return 0;
}
 

C++ program to reverse a number


#include 
          #include 
class operations
{
  long c;
 
public:
  void inputnumber()
  {
    cout << "input a number\n";
    cin >> c;
  }
 
  long reversenumber()
  {
    long invert = 0;
 
    while (c != 0)
    {
      invert = invert * 10;
      invert = invert + c%10;
      c = c/10;
   }
 
    return invert;
  }
 
};
 
int main()
{
  long result;
 
  operations t;
  t.inputnumber();
  result = t.reversenumber();
 
  cout << "number obtained on reversal = " << result;
 
  return 0;
}

No comments:

Post a Comment