You are here

C++ class example program

C++ class program example: In our program, we create a class named programming with one variable and two functions. In the main function, we create an object of this class and call these functions.

C++ programming code

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class programming
  6. {
  7.    private:
  8.       int variable;
  9.    
  10.    public:
  11.      
  12.       void input_value()
  13.       {
  14.          cout << "In function input_value, Enter an integer\n";
  15.          cin >> variable;
  16.       }
  17.      
  18.       void output_value()
  19.       {
  20.          cout << "Variable entered is ";
  21.          cout << variable << "\n";
  22.       }
  23. };
  24.  
  25. main()
  26. {
  27.    programming object;
  28.    
  29.    object.input_value();
  30.    object.output_value();
  31.    
  32.    //object.variable;  Will produce an error because variable is private
  33.    
  34.    return 0;
  35. }