Sample C++ Code to Understand Public, Private and Protected Access Modifiers in C++

Effective usage of C++ class requires you to have understanding of Public, Private and Protected Access Modifier. Knowledge of C++ Access Modifiers is required so that you can protect your C++ classes and simultaneously understand how to use c++ classes provided by other developers without providing the source code. This sample C++ code uses an example of Car class and the Car Owner class inherits Car Class to demonstrate the usage of Access Modifiers. Remember that this C++ example is a very simple class and in practical usage, the Car Class would be much more sophisticated and so would be the Car Owner Class.

Public, Private and Protected Member Varialbles in a C++ Class

Public, Private and Protected Member Variables in a C++ Class

The above C++ class depicting a Car of any make or model has private, protected and public member variables. The Access Modifier works the same for Member Functions as they apply to Member Variables and hence to keep this Sample C++ code simple and fast to learn , only member variables have been added to the class.

The int member variable depicting Number of Bolts in the car is kept private assuming that normally a Car Owner would not have this knowledge and would not need it either. Another int member variable depicting current car speed is kept private in this C++ class as with speed camera anybody can determine the speed of the car while it is running and car owner would have this information from the car speedometer. Finally another member variable depicting current gear of the car has been kept as protected assuming that only the car owner would have this information. This class with member variables has been created so that any other class like Car Owner class can inherit it’s member variables using C++ Inheritance. The given below car owner class inherits the Car class and uses the member variables of the Car Class.

Car Owner Class in C++ to Illustrate Usage of Public, Private and Protected Member Variables

Car Owner Class in C++ to Illustrate Usage of Public, Private and Protected Member Variables

Yes in a practical usages , the car class and the owner class would be much more sophisticated. This sample c++ project has been presented for beginner to understand public, private and protected access modifiers used in c++ inheritance. In case you are wondering why to restrict access of member variables or member functions to other classes, you must understand that when you would be actually developing software applications, you would not be having source code of libraries you are using. Software Developers create dll files and other libraries to share functionality developed by them to other developers without providing them the full source code. Yes this website will provide enough examples and samples for you to fully understand how to harness the power of C++ Programming Language to the fullest. Do have a look at the sample source code of this sample project.

// PublicPrivateProtected.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class CCar{private:int m_iNumberOfBolts;public:int m_iCarCurrentSpeed;CCar(){m_iCarCurrentSpeed=0;m_iCurrentCarGear=0;m_iNumberOfBolts=78569;}protected:int m_iCurrentCarGear;};class CCarOwner : public CCar{public:void DisplayCurrentCarSpeed(){cout << "Current Speed of Car is " << m_iCarCurrentSpeed << " Miles Per Hour.n";}void DisplayCurrentGearOfTheCar(){cout << "Currently Car is in " << m_iCurrentCarGear << " Gear.n";}};int _tmain(int argc, _TCHAR* argv[]){return 0;}