C++ OOP:

C++ is a OOP Version of C…
A class is a blueprint of an object…..Basically a object is created from class

c++ class define:

 

 

c++ object define:

Here we are declaring two objects of a class Box

Box box1; // declare box1 of type Box
Box box2; //declare box2 of type Box

Basic example of OOP here :

C++ Class Member Functions:

2015-09-26 01_01_14-_D__desktop_desktop september_oop_access.exe_

We use this process to avoid direct access to the members of the class

Another technique, we have used scope resolution operator :: to define the same function outside the class that means if we want to define class in outside from the class then we have to use scope resolution operator.

 

inside the class we define this like that

and the member function is called just like this in a object from a class

C++ Access Modifiers:

Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type .The access restriction to the class members is specified by the labeled public,private and protected sections with class body.The keywords public ,private and protected are called access specifiers.

 

A class can have multiple public,protected or private labeled sections.Each section remains in effect until either another section label or the closing right brace of the class body is seen.The default access for members and classes is private.

The public members:

A public meber is accessible from anywhere outside the class but within  a program.You can set and get the value of public variables without any member function as shown in the following example:

2015-09-26 02_34_55-access.cpp - Code__Blocks 13.12

 

The private members:

A private member variable or function cannot be accessed or even viewed from outside the class.Only the class and friend functions can access private members.

By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member.

Practically we define data in private section and related functions in public section so that they can be called from outside of the class as shown in the following program.

The protected members:

A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.

We will discuss derived classes and inheritance in next .But here I have derived one child class names SmallBox from a parent class Box

Following example is similar to above example and here width member will be accessible by any member function of its derived class SmallBox