Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

BCA-III C++ and Object Oriented Programming

CHAP-1 PRINCIPLES OF OBJECT ORIENTED PROGRAMMING

Procedure-Oriented Programming Vs Object-Oriented Programming

Sr.No. POP OOP

(1) In POP the primary or main focus is In OOP the primary or main focus is on data
on functions and procedures. instead of procedures.

(2) In POP large programs are divided In OOP programs are divided into an object
into smaller programs known as which is an instance of class.
functions.

(3) Data move openly around the system Data is hidden and cannot be accessed by
from function to function. external functions.

(4) Functions transform data from one Objects may communicate with each other
function to another. through functions.

(5) Mapping between data and function is Mapping between data and function is very
difficult and complicated. easy because it is accessed by using objects.

(6) Top-Down approach is used in Bottom-Up approach is used in program


program design. design.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Basic concepts of Object-Oriented Programming

Objects
Objects are the basic run-time entities in an object-oriented system.
It is an instance of class.
Object can be anything and used to represent a person, a place, a bank account, a table of
data or any item that the program has to handle.
Object reserve a space in memory and have an address like a structure in a C language.
Each object contains data and code to manipulate the data.
Below figure shows the example of representation of an object.

Classes
Class is a collection of objects of similar types.
The entire set of data and code of an object can be made a user define data type with the help
of a class.
For example mango, orange, apple and banana are members of the class fruit.
Classes are user-define data types and behave like the built-in types of a programming
language.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

The syntax used to create an object is as similar as create an integer object in c. If fruit has
been defined as a class, then the statement
fruit mango;

will create an object mango belonging to the class fruit.

Data Abstraction and Data Encapsulation

Data Encapsulation

Data encapsulation is a mechanism to binding up a data and functions into a single unit.
The data is not accessible to the outside world, only by functions which are bind in the class
can access.
Thus data encapsulation introduces a new OOP concept called data hiding or information
hiding.

Data Abstraction

Data abstraction is a mechanism of the separate interface and implementation means


providing only essential information to the outside world and hiding their background
details.
Since the classes used the concept of data abstraction, they are known as Abstract Data
Types (ADT).

Inheritance

Inheritance is a process by which object of one class can access the properties of another
class.
In OOP, the concept of inheritance providing the idea of reusability.
Inheritance is the process of creating new classes, which is called derived classes from the
existing classes. These existing classes are often called base classes.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Below example shows the derived classes which are creating from the base class.

Figure: Example of Inheritance

In above example Vehicle class has two attributes petrol and diesel, this attributes are
accessed by another two classes Two Wheeler and Four Wheeler which is derived from the
Vehicle class.
Same way Two Wheeler class further divided into Scooter and Motor and Four Wheeler
class is divided into Car and Truck class and access same attributes (properties) of Vehicle
class.

Polymorphism
In OOP polymorphism means that same code of operations or objects behave differently in
different instances.
For example (+) operator in C++:
4+5 Integer Addition
3.14 + 2.0 Floating Point Addition
S1 + abc String Concatenation

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

In C++, that type of polymorphism is called overloading means thing used for different
purpose.
If we achieve polymorphism through function, then it is called function overloading. If we
use operator for achieving polymorphism, then it is called operator overloading.
A polymorphism are two types:
(i) Compile time polymorphism
(ii) Run-Time polymorphism
Below figure shows the polymorphism using function.

Figure: Example of Polymorphism using Function

Dynamic Binding

In OOP dynamic binding refers to the linking a procedure call to the code that will be
executed only at run time.
Dynamic binding also known as late binding. It is one type of run time polymorphism.
In run time polymorphism, the selection of appropriate functions is called at run time not at
compile time.
The dynamic binding is achieved with virtual function.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Message Passing

An OOP consists of a set of object that communicates with each other through member
functions.
The objects communicate with each other by sending and receiving information same way
as people pass the message to one another.
Message passing invokes specifying the name of the object, the name of the function and the
information to be sent as shown in below example.

Emp.Salary (name);

Object Information
Message

In above example Emp is a object of class, Salary is the message of function to do and the
name is the information required.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Benefits (Advantages) of OOP

Data Hiding: The principle of data hiding helps the programmer to build the secure
programs that cannot be accessed by the code in other parts of the program.
Inheritance: Through inheritance, we can eliminate redundant code and extend the use of
existing classes. So the idea of reusability can be provided.
Polymorphism: Using polymorphism we can achieve function overloading and operator
overloading which helps to do same thing using different instances.
Dynamic Binding: Using dynamic binding, we can select appropriate function at run time.
Message Passing: Technique of message passing for communication between objects makes
the system much simpler.
OOP can easily upgrade from small to large systems.
It is easy to partition the work based on objects, means different objects have different data
stored in different partitioned in memory, so problem of data loss can be solved.
Using OOP concepts software complexity can be easily managed.

Applications of OOP

OOP used in complex system and make it easier and simple to manage. The application of
OOP include:

Real-time systems

Object-Oriented Databases

Hypertext and Hyper Media

Office Automation Systems

CAD / CAM Systems

AI and Neural Networks

Networking and Graphics

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

What is C++

C++ is an Object-Oriented Programming language which is extension of C language.


It was originally developed by Bjarne Stroustup at AT & T Bell Laboratory in USA, in the
early 1980s (Ending of 1979).
Why C++ is a favorite programming language and very popular now a days? Because it
provides rich and robust programming language and having a major addition of the Class
Structure.
The idea of C++ named came from the C increment operator ++, thereby in 1983 the name
was changed to C++.
C++ is a superset of C. Most of all the functionality available in C applies to C++ also.
The most important facilities that C++ adds are Classes, Inheritance, Function Overloading
and Operator Overloading. These features making C++ a truly Object-Oriented language.

Application of C++

C++ is a versatile language for handling very large programs.


Area of C++ Application:
Software Engineering
Graphics
System Programming such ate compilers, editors, databases, etc.
Real world application systems

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Structure of C++ Programs

Below figure shows the typical structure of C++ program that contain four sections.

Figure: Structure of a C++ Program

It is a common structure of C++ program. The class declarations are placed in a header file
and the definitions of member functions go into another file.
This approach enables the programmers to separate the abstract specification of the interface
(Class Definition) from the implementation details (Member Functions Definition).
Finally, the main program that uses as a third file which includes the previous two files as
well as any other files.

Input / Output Operators

In C++ for the input and output there are some predefine operator and some predefine
stream objects are available.

INPUT OPERATOR

Operator >> is known as extraction or get from operator.


It is used to get the value from the keyboard and assign it to the variable on its right.
The operator >> is used with Cin object.

Example: Cin >> number1;

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

OUTPUT OPERATOR

Operator << is known as insertion or put to operator.


It is used to display the output on the console (Monitor).
The operator << is used with Cout object.

Example: Cout << Hello ;

Cascading Input / Output Operators

The multiple use of << and >> operator in one statement is called cascading.

Example 1: Cout << Total= << Total << \ n ;


In above example first sends the string Total to Cout and then it sends the value of Total.
Finally, it sends the newline character so that next output will be in the new line.
We can also cascading input operator >> as shown in below:
Example 2: Cin >> number1 >> number2 ;
In above example the values are assigned from left to right. For example if we entered two
value 10 and 20, then 10 will be assigned to number1 and 20 to number2

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Writing a simple C++ Program

Lets start with a simple C++ Program that prints a string on the screen.

#include <iostream.h> // Line1


int main () // Line2
{
cout << "Hello World !" ; // Line3
return 0; // Line4
}

Line1 of program indicate that we include iostream file into our program.
IO stands for Input / Output and h stands for header.
The file contains the declaration for the predefine iostream objects like cin and cout for input
/ output operations.
Line2 of program contain main () function. In C++, main () returns an integer type value to
the operating system. Therefore, every main () in C++ should end with a return (0)
statement; otherwise a warning or an error might occur.
Line3 prints the string Hello World ! on the screen using output operator << .
Line4 indicate return type of main () function which return 0 value.

Features of C++ Program

Like C, the C++ program is a collection of functions. In above example only one function
main () is used.
As usual, execution begins at main ().
Every C++ programs must have a main ().
Like C, C++ statements terminate with semicolons.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Comments in C++

SINGLE LINE COMMENTS

It starts with two back slashes // and followed by your descriptions.


Example: // This is C++ Program.

MULTIPLE LINE COMMENTS

It starts with a slash followed by asterisk sign /* and end with asterisk and slash */
Example: /* C++ is an Object-Oriented Programming Language and Superset of C

Language */

The Complier and Editors for C++

The compiling and linking of C++ programs depends upon the operating system. A few
popular editors for C++ programs are:

Turbo C++

Borland C++

Visual C++

Dev C++

Introduction of namespace

Namespace is a new concept introduce by the ANSI C++ standards committee.


This defines a scope for the identifiers that are used in a program. For using the identifiers
defined in the namespace scope we must include the using directive, like:
using namespace std;
Here, std is the namespace where ANSI C++ standard class libraries are defined. This will
bring all the identifiers defined in std to the current global scope. using and namespace are
the new keywords of C++.

Prepared by | Hemang R. Chath

You might also like