Categories
C++ Free Tutorials

Classes and objects in C++, a beginners tutorial

Learn about classes and objects in C++. Whats are data members and function members or methods in C++. How to define or how to create a class in C++. How to define functions as members in a C++ class. How to create objects and assign data members of a C++ class. Access the properties / methods of a C++ class from the main() function body.

C++ Class Structure

Class MyClass {
        private:
               // Data members
               int i;
               char c;
               // More Data or function members can be here
        public:
               void showData();
               int getI();
        
        access_specifiers: 
               // Data or function members
        access_specifiers: 
               // More Data or function members can be here
}

Access specifier can be private , public or protected.
In C++ default specifier is private.

A) How To Define A Class ( Source Code Example )

class MyClass {
                 private:
                      int datamember;
                 public:
                      void setDataMember( int i )
                      {
                                 datamember = i;
                      }

                      int getDataMember()
                      {
                                 return datamember;
              }
};
// Class ends with a semicolon after braces enclosing class body

B) Creating Objects from the above class

int main()
{
               MyClass obj;
               obj.setDataMember(8);
               int a = obj.getDataMember();
               cout << “The value of data member = ” << a;
}

In the above A) and B) makes the complete program

Data hiding

Hiding the data members from the parts of the program, that should not get access of it. Easiest mechanism to hide a data member variable or object is to declare it as private. Private data or functions can be accessed from within the class by using the public functions.

Data members

Data items inside a class, which are not functions. Private data members can be accessed by public methods or functions. For the data member of a class there should be methods or functions inside the class to set data and get data. There should be ways to set / assign values to the data members by some way in your program source code.
Memory Management: Each objects data members are stored in separate memory location.

Function Members

Function members can be private or public. Few functions may be private , few may be public. Normally member functions are called on the objects of a class as shown in A and B
The general form of calling the member functions are

objectName.memberFunctionName();

Memory Management : For all objects of a class only one set of member functions are stored in memory. All objects of a class share the same function stored in memory.
setData() and getData() type functions must be defined for a class to set values to data members and get “data member values”. Also it is recommended to assign . There is no setData() function in C++, but you should define such a function.

Member functions defined inside a C++ class body

A member function can be defined inside a class, then they are inline member functions. Though you donot need to use the keyword inline .

 

C++ Free tutorials >>  C++ courses in Kolkata >> Java Courses

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();
}