You are on page 1of 25

C++ Introduction

Unit-1
Presented by
Er.
Assistant Professor
Applied Science(CSE)
Chandigarh University
Gharuan (Mohali).
1
C vs C++
• C- Structured programming became very popular and
was the main technique of the1980.
• Structured programming enable programmers to write
moderately complex programs fairly easily. However as
the programs grew larger, even the structured approach
failed to show the desired results in terms of bug-free,
easy-to-maintain and reusable programs.
C vs C++
• -Object-Oriented Programming (OOP) is an approach
to program organization and development that
attempts to eliminate some of the pitfalls of
conventional programming methods by incorporating
the best of structured programming features with
several powerful new concepts.
Procedure-Oriented Programming
• Conventional programming using high level language
such as COBOL, FORTRAN and C is commonly known
as Procedure oriented programming.
• In the Procedure oriented approach the problem is
viewed as a sequence of things to bed one such as
reading, calculating and printing.
• The primary focus is on functions.
Structure of Procedure-oriented
Procedure-Oriented Programming
• Procedure-Oriented Programming
• Procedure oriented programming basically consists
of writing a list of instructions for the computer to
follow and organizing these instructions into groups
known as functions.
• We normally use a flow chart to organize these
actions and represent the flow of control from one
action to another.
• More importance to functions very little attention to
data that are being used by the functions.
Structure of Procedure-oriented
Procedure-Oriented Programming
• In multi-function program many important data
items are placed as global so that they may be
accessed by all the functions. Each function may
have its own local data.
• In large program it is very difficult to identify what
data is used by which function. In case we need to
revise an external data structure we also need to
revise all function that access it.
Object-Oriented Programming
• 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 entites called objects
Organization of data & function OOP
Striking features of OOP
• Emphasis is on data rather than procedure.
• Programs are divided in to what are known as
objects
• Functions that operate on the data of an object are
tied together in the data structure
• Data is hidden and can not be accessed by external
functions
• Objects may communicate with each other through
functions
Difference between Procedure oriented Programming and
Object Oriented Programming

Procedure Oriented Object Oriented


Programming Programming

Divided Into In POP, program is divided into


In OOP, program is divided
small parts calledfunctions.
into parts called objects.
Importance In POP,Importance is not given
In OOP, Importance is given
to data but to functions as well
to the data rather than
as sequence of actions to be
procedures or functions
done. because it works as a real
world.
Approach POP follows Top Down OOP follows Bottom Up
approach. approach.
Access POP does not have any OOP has access specifiers
Specifiers access specifier. named Public, Private,
Protected, etc.
Data Moving In POP, Data can move In OOP, objects can move
freely from function to and communicate with
function in the system. each other through member
functions.

Expansion To add new data and OOP provides an easy way


function in POP is not so to add new data and
easy. function.

Data Access In POP, Most function In OOP, data can not move
uses Global data for easily from function to
sharing that can be function,it can be kept
accessed freely from public or private so we can
function to function in the control the access of data.
system.
Data Hiding POP does not have any OOP provides Data
proper way for hiding Hiding so provides more
data so it is less secure. security.

Overloading In POP, Overloading is In OOP, overloading is


not possible. possible in the form of
Function Overloading
and Operator
Overloading.

Examples Example of POP are : C, Example of OOP are :


VB, FORTRAN, Pascal. C++, JAVA, VB.NET,
C#.NET.
Basic Object oriented concepts:
• Class –The entire set of data and code of an object
can be made a user-defined data type with the help
of a class.
• Objects are variable of type class.
• Once a class has been defined we can create any
number of objects belonging to that class.
• A class is thus a collection of objects of similar type.
• Classes are user-defined data types and behave like
the built-in types of a programming language.
Object
• Objects are the basic run-time entities in an object-oriented
system. Object- is an instance(Properties) of a class. Each
object has a class which defines its data and behaviour.
• They may represent a person, a place, a bank account , a table
of data or any item that the program must handle.
• Program objects should be chosen such that they match
closely with the real-world objects.
• Objects take up space in the memory and have an associated
address like structure in C.
• When a program is executed the objects interact by sending
messages to one another.
Structure of a Class in C++

class name {
attributes and
declarations symbolic constants

how to create and


constructor definition(s) initialize objects

method definitions how to manipulate


the state of objects
} These parts of a class can
actually be in any order

17
Sample class

#include<iostream.h>
class Pencil
{
public String color = “red”;
public int length;
public float diameter;
setcolor(string);

public void setColor (String newColor)


{
color = yellow;
}
}
• Data Abstraction-Data Abstraction refers to the act of
re- presenting essential features without including
the back-ground details. It is concerned with
separating the behaviour of a data object from its re-
presentation.
• E.g. Executable file of a program.
Encapsulation- The process of binding data members
and functions in a class is known as, encapsulation.
Encapsulation is the powerful feature (concept) of
object-oriented programming. With the help of this
concept, data is not accessible to the outside world
and only those functions which are declared in the
class, can access it.
• information hiding/Data Hiding- Data Hiding is
similar to encapsulation. Basically, encapsulating data
members and functions in a class promotes data
hiding. This concept will help us to provide the
essential features to the users and hide the details. In
short, encapsulating through private access modifier
(label) is known as data hiding.
• Inheritance- Inheritance is a process by which
objects of new class acquire the properties of objects
of existing (base) class. It is in hierarchical order. The
concept of inheritance provides the idea of
reusability. This means that we can add additional
features to an existing class without modifying it.
• Polymorphism
Polymorphism is an important object-oriented
programming concept. This is a greek term, means
the ability to take more than one form. The process
of making an operator to show different behaviours
in different instances is known as operator-
overloading. Using a single function name to
perform different types of tasks is known as
function-overloading.
Benefits of OOP
• Through inheritance we can elimate the redundant
code and extend the use of existing code
• Data hiding helps the programmer to build secure
programs that can not be invaded by code in other
parts of the program
• It is possible to have multiple instances of an objects to
co-exist with out any interference.
• It is possible to map objects in the problem domain to
those objects in the program
• Easy to partition the work in a project based on objects
Data-centred design approach enables us to
capture more details of a model in
implementable form
• Object-oriented systems can be easily
upgraded from small to large systems
• Software complexity can be easily managed.

You might also like