Ads block

Banner 728x90px

Object Oriented Programming using C++


Introduction

C++ is an object - Oriented Programming language. It was developed by ' Bjarne Stroustrup ' at AT & T  Bell Laboratories in Murray Hill, New Jersey, USA  in the early 1980's.


Stroustrup,  an admirer of Simula67 and a strong supporter of C, wanted to combine the best of both the language and create a more powerful language that could support Object - Oriented Programming feature and still retain the power and elegance of C. The result was C++. 

We can say that C++ is an extension of C with a major addition of class construct feature of Simula67.                                                                     Stroustrup initially called the new language       'C with classes'. However, later in 1983, the name was changed to C++.


The most important facilities that C++ adds on to  classes,  Inheritance,  function overloading and  operator overloading.  These features enable creating of abstract data types, inherit properties from existing data types and support  Polymorphism,  thereby making C++ a truly Object - Oriented language.


Note:  C++ is not purely Object - Oriented language because  the word  Object - Oriented means  whole program written with the help of Class and Objects, but in C++ program can be  write without use of Class & Objects.


Object-Oriented Programming Paradigm

The core of Object- Oriented Programming is to creating Objects which has certain properties and methods. Object- Oriented Programming (OOP) was invented to overcome the drawbacks of the POP. 

OOP treats data as a critical element in the program development and does not allow it to flow freely around the system.  It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions.

OOP allows decomposition of a problem into a number of entities called Objects  and then builds data and functions around these Objects.




Emphasis is on data rather than procedure.
Programs are divided into what are known as Objects.

Data structures are designed such that they characterize the objects.

Functions that operate on the data of an object are tied together in the data structure.

Data is hidden and cannot be accessed by external functions.

Ojects may communicate with each other through functions.
New data and functions can be easily added whenever necessary.

Basic Concepts of Object-Oriented Programming


Some basic concepts are necessary to understand in Object-Oriented programming. These include:


Objects 

Objects are the basic run-time entities in an Object-Oriented system. Objects contain set of data and code to manipulate that data.


For example, " STUDENT" is an object of a program. It contains some data relating to student like  Name, Date of birth, and so on and   Functions such as Total, Average and so on.

Object: STUDENT
DATA
Name
Date of birth
.......
FUNCTIONS
Total
Average
.......


Classes


A class is a container that contain objects, and these objects are known as variables or members of this class. 

Once a class has been defined, we can create any number of objects belonging to that class.

A class is a collection of objects of similar type.
For example,  mango, apple and banana  are members of class fruit.
The syntax  used to create an object is given below:


fruit mango;




Encapsulation

The wrapping up or binding of data and functions into a single unit (called class) is known as encapsulation.   


Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions which are wrapped in the class can be access it.



Data Abstraction


Abstraction refers to the act of representing essential features without including the background details or explanations.
Classes uses the concept of data abstraction, they are known as Abstract Data Types (ADT).



Inheritance


Inheritance is the process by which of one class acquire the properties of another class. It supports the concept of hierarchical classification.

In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one.



Polymorphism


In this process, an operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in operation.


The process of making an operator to exhibit different behaviors in different instance is known as operator overloading.



Function overloading


it is important feature of C++. function overloading is more than one function with the same name but different parameters list.
function overloading provides facility to save memory, consistency and readable program.

Function overloading means single function name can be used to handle different number and different types of arguments. Using a single function name to perform different types of task is known as a function overloading.



Output Operator Cout


The identifier "Cout" is a predefined object that represents the standard output stream in C++.

The statement

cout << "C++ is better than C";

causes the string in quotation marks to be displayed on the screen.

Here, it introduces two new C++features, cout and <<. Cout means standard output stream in C++ and operator << (called insertion) inserts the contents of the variable on its object. This corresponds to the familiar printf() operation of C language.

This concept is known as operator overloading, an important aspect of Polymorphism.


Input Operator Cin


The identifier "Cin" is a predefined object that represents standard input stream.
The statement


cin >> number1;

is an input statement and causes the program to wait for the user to type in a number.

In simple words, cin is a object to the standard input stream and operator >> (called extraction) takes the value from the keyboard and assigns it to variable.




Operators in C++


C++ has a rich set of operators. All C operators are valid in C++ also. In addition, C++ introduce some new operators. Other new operators are:


In C, the global version of a variable cannot be accessed from the inner block. C++ resolves this problem by introducing a new operator : : called Scope resolution operator.

: : variable-name;

This operator allows access to the global version of variable.


The operator : :* is used to declare a pointer to a member of a class.


The operator ->* is used to access a member using a pointer to the object and a pointer to that member.


The operator .* is used to access a member using object name and a pointer to the member.


An object can be destroyed by using delete operation.

delete pointer-variable;

The endl manipulator, when used in an output statement, causes a linefeed to be inserted. It has the same effect as using the newline of character "\n" in C.


The new operator can be used to create objects of any type.

pointer_variable = new data-type;

The new operator allocate sufficient memory to hold a data object of type.


memory card to the name on internet because it is the same and let me to do anything 😭😭.