C++ Object-Oriented Concepts

Object Oriented programming Concepts in C++
Object Oriented programming is a programming style that is associated with the concept of OBJECTS, having datafields and related member functions.
Objects are instances of classes and are used to interact amongst each other to create applications.Instance means, the object of class on which we are currently working. C++ can be said to be as C language with classes. In C++ everything revolves around object of class, which have their methods & data members.
C++ can be said to be as C language with classes. In C++ everything revolves around object of class, which have their methods & data members.

For Example :- We consider human body as a class, we do have multiple objects of this class, with variable as color, hair etc. and methods as walking, speaking etc.
Now, let us discuss some of the main features of object oriented programming which you will be using in C++.

  1. Objects
  2. Classes
  3. Abstraction
  4. Encapsulation
  5. Inheritance
  6. Overloading
  7. Exception Handling
Advantages And Features Of Object Oriented Programming - C++ Programming
Features Of Object Oriented Programming :
Data Hiding
Data hiding is a software development technique specifically used in object-oriented programming (OOP) to hide internal object details (data members). Data hiding ensures exclusive data access to class members and protects object integrity by preventing unintended or intended changes.
Data hiding also reduces system complexity for increased robustness by limiting interdependencies between software components.
* Data Hiding is also known as Encapsulation.
* Encapsulation is the process of combining data and function into a single unit called class.
* Data Hiding is the mechanism where the details of the class are hidden from the user.
* The user can perform only a restricted set of operations in the hidden member of the class.
* Encapsulation is a powerful feature that leads to information hiding,abstract data type and friend function.
* They encapsulate all the essential properties of the object that are to be created.
* Using the method of encapsulation the programmer cannot directly access the class.
Abstraction
The process of picking out (abstracting) common features of objects and procedures. A programmer would use abstraction, for example, to note that two functions perform almost the same task and can be combined into a single function. Abstraction is one of the most important techniques in software engineering and is closely related to two other important techniques -- encapsulation and information hiding. All three techniques are used to reduce complexity.
Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.
Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.
Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.
Abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures.
* Control abstraction involves the use of subprograms and related concepts control flows
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype.
Access Labels Enforce Abstraction:
In C++, we use access labels to define the abstract interface to the class. A class may contain zero or more access labels:
* Members defined with a public label are accessible to all parts of the program. The data-abstraction view of a type is defined by its public members.
* Members defined with a private label are not accessible to code that uses the class. The private sections hide the implementation from code that uses the type.
There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen.
Benefits of Data Abstraction:
Data abstraction provides two important advantages:
* Class internals are protected from inadvertent user-level errors, which might corrupt the state of the object.
* The class implementation may evolve over time in response to changing requirements or bug reports without requiring change in user-level code.
Example: -
#include < iostream.h >
using namespace std;
class Adder{
public:
Adder(int i = 0)
{
total = i;
}
void addNum(int number)
{
total += number;
}
int getTotal()
{
return total;
};
private:
int total;
};
int main( )
{
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() < return 0;
}
Output: -
Total 60
Encapsulation
Data encapsulation, sometimes referred to as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called methods. The actions performed by the methods are determined by the designer of the class, who must be careful not to make the methods either overly flexible or too restrictive. This idea of hiding the details away from the user and providing a restricted, clearly defined interface is the underlying theme behind the concept of an abstract data type.
The advantage of using data encapsulation comes when the implementation of the class changes but the interface remains the same. For example, to create a stack class which can contain integers, the designer may choose to implement it with an array, which is hidden from the user of the class. The designer then writes the push() and pop() methods which puts integers into the array and removes them from the array respectively. These methods are made accessible to the user. Should an attempt be made by the user to access the array directly, a compile time error will result. Now, should the designer decide to change the stack's implementation to a linked list, the array can simply be replaced with a linked list and the push() and pop() methods rewritten so that they manipulate the linked list instead of the array. The code which the user has written to manipulate the stack is still valid because it was not given direct access to the array to begin with.
The concept of data encapsulation is supported in C++ through the use of the public, protected and private keywords which are placed in the declaration of the class. Anything in the class placed after the public keyword is accessible to all the users of the class; elements placed after the protected keyword are accessible only to the methods of the class or classes derived from that class; elements placed after the private keyword are accessible only to the methods of the class.
As a convention, calling a method of an object instantiated from a class is commonly referred to as sending a message to that object.
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. A class can contain private, protected and public members. By default, all items defined in a class are private. For example:
#include < iostream.h >
using namespace std;
class Adder{
public:
Adder(int i = 0)
{
total = i;
}
void addNum(int number)
{
total += number;
}
int getTotal()
{
return total;
};
private:
int total;
};
int main( )
{
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() < return 0;
}
Output: -
Total 60
Encapsulation and Abstraction
*Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.
*OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.


Free Web Hosting