A tutorial on C++ class & objects

How to create a class in C++ and how to create objects and call the properties / methods of a C++ class from the main() function body.

C++ tutorials >> C++ Classes

In this program I have created a class distance and declared one private variable called miles of integer type and also created one constructor function Distance(). Constructor is a function which has same name as the class.
I have initialized the value of the private data member miles of the class. This private property can be accessed only using the methods of the same class methods our distance a distance show miles and set distance these all are user-defined functions from which you can access manipulate modify the data members the data members can be private or you can write here public also if you

#include<iostream>
using namespace std;
class Distance {
         private:
                 int miles;
         public:
                 Distance(){
                              miles = 0;
                           }

                  void getDistance() 
                           {
                              cout << "\r\n In getDistance() method - Enter Distance in Miles";
                              cin >> miles;
                           }

                  void showDistance() {
                              cout << "\r\n In showDistance() method - Miles = " << miles;
                  }

                  void setDistance(int m) {
                              miles = m;
                   }

};

int main()
{
Distance d;
d.showDistance();
d.getDistance();
d.showDistance();
d.setDistance(50);
d.showDistance();
}