Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views28 pages

Unit I - Basic Concepts of OOP

C++ is an object-oriented programming language developed in the early 1980s, serving as a versatile tool for various programming tasks. It differs from C by supporting data hiding, object-oriented principles, and allowing local variable declarations anywhere in the program. Key concepts of C++ include classes, objects, inheritance, polymorphism, and encapsulation, which facilitate modular programming and code reusability.

Uploaded by

Poorani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views28 pages

Unit I - Basic Concepts of OOP

C++ is an object-oriented programming language developed in the early 1980s, serving as a versatile tool for various programming tasks. It differs from C by supporting data hiding, object-oriented principles, and allowing local variable declarations anywhere in the program. Key concepts of C++ include classes, objects, inheritance, polymorphism, and encapsulation, which facilitate modular programming and code reusability.

Uploaded by

Poorani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction to C++

• 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.
• C+ + is a superset of C.
• C++ is a versatile language for handling very large
programs; it is suitable for virtually any
programming task including development of
editors, compilers, databases, communication
systems and any complex real life applications
systems.
Difference b/w c and c++
C Language C++ Language
Procedural Programming Object Oriented
Language Programming Language
Headerfile : #include<stdio.h> #include<iostream.h>

“\n” is used to go to the next we can use endl statement


line
Local variables declared only It can be declared
the start of a C program anywhere in a program,
before they are used
Return type for a function is Return type must be
optional specified
Do not permit data Hiding They permit data hiding
Bydefault structure members class members are private
are public
We can call a main() function This is not allowed
within a program 2
What is Programming?
Like any human language, a programming
language provides a way to express concepts.
Program development involves creating models
of real world situations and building computer
programs based on these models.
Computer programs describe the method of
implementing the model.
Computer programs may contain computer
world representations of the things that
constitute the solutions of real world problems.
• Programmers write instructions in various programming
languages to perform their computation tasks such as:
(i) Machine level Language - Machine code or machine language is
a set of instructions executed directly by a computer's central
processing unit (CPU).

Strings of numbers giving machine specific instructions –


Example: +1300042774 +1400593419 +1200274027
ii ) Assembly level Language - An assembly language (or assembler
language) is a low-level programming language for a computer,
or other programmable device, in which there is a very strong
(generally one-to-one) correspondence between the language
and the architecture's machine code instructions.

Example: LOAD BASEPAY


ADD OVERPAY
STORE GROSSPAY
(iii) High level Language - High-level language is any
programming language that enables development of a
program in much simpler programming context and is
generally independent of the computer's hardware
architecture.

Codes similar to everyday English – Use mathematical


notations (translated via compilers) – Example:
grossPay = basePay + overTimePay

The high-level programming languages are broadly


categorized in to two categories:
(i) Procedure oriented programming(POP) language.
(ii) Object oriented programming(OOP) language.
Procedure Oriented Programming
Language
• Procedure oriented programming basically consist of
writing a list of instruction or actions for the computer
to follow and organizing these instruction into groups
known as functions.
Characteristics of procedure oriented programming:
1. Step by Step Procedure (algorithm)
2. Large programs are divided into smaller programs
known as functions.
3. Most of the functions share global data
4. Data move openly around the system from function to
function
5. Function transforms data from one form to another.
6. Employs top-down approach in program design
Object Oriented Programming
• Object oriented programming as an approach
that provides a way of modularizing programs by
creating partitioned memory area for both data
and functions that can be used as templates for
creating copies of such modules on demand.
• Objects have both data and methods
• Objects of the same class have the same data
elements and methods
• Objects send and receive messages to invoke
actions
OBJECT ORIENTED PARADIGM

Object 2
Object 1

Data Data

Function Function

Object 3

Data

Function

8
BASIC TERMINOLOGY
• Object - An object is an instance of a class
which combines both data and functions
together.
• Method - an action performed by an object (a
verb)
• Attribute - description of objects in a class
• class - a category of similar objects
- does not hold any values of the object’s
attributes
By default all members declared inside a class are private to
that class
OOP Features
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as
objects.
• Functions that operate on the data of an object
are ties together in the data structure.
• Data is hidden and cannot be accessed by
external function.
• New data and functions can be easily added
whenever necessary.
• Follows bottom up approach in program design.
Basic concepts
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
OBJECTS
• Objects are the basic run-time entities in an
object-oriented system.
• They may represent a person, a place, a bank
account, a table of data or any item that the
program must handle.
• The fundamental idea behind object oriented
approach is to combine both data and
function into a single unit and these units are
called objects.
Syntax to create an Objects:
classname objectname=new classname();
Student s1; // declares s1
s1 = new Student(); // allocates space
Student s1 = new Student(); // does both
• An object doesn't exist until an instance of the class has
been created; the class is just a definition. When the object
is physically created, space for that object is allocated
in RAM

• It is possible to have multiple objects created from one class.

• Main Difference Between Class and Objects:

– Object takes Separate Memory.


– For Class, no memory is allocated.
CLASS
• A group of objects that share common
properties for data part and some program
part are collectively called as class.
• Class is a like a Blueprint
• In C ++ a class is a new data type that contains
member variables and member functions that
operate on the variables.
• Class Syntax:
class classname
{
Access - Specifier:
Member Varibale Declaration;
Member Function Declaration;
}
Example
If fruit has been defined as a class then the statement
fruit mango
will create an object mango belonging the class fruit.
Data Abstraction
• Abstraction refers to the act of representing
essential features without including the back
ground details or explanations.

For example, a class Car would be made up of an


Engine, Gearbox, Steering objects, and many
more components.
To build the Car class, one does not need to know
how the different components work internally.
Data Encapsulation
• The wrapping up of data and function into a single unit
(called class) is known as encapsulation.

• The data is not accessible to the outside world and only


those functions which are wrapped in the class can access
it.

• These functions provide the interface between the objects


data and the program.
• Data encapsulation led to the important OOP concept
of data hiding.
• Encapsulation means hiding the internal details or
mechanics of how an object does something.
• A class can contain private, protected and public members.
Inheritance
• The Process of deriving new class from existing class.
– This is called Inheritance.
New Class-Child Class
Existing Class-Parent Class

• Inheritance creates a hierarchy of related classes (types)


which share code and interface.

• In object-oriented programming (OOP), inheritance is when


an object or class is based on another object or class, using
the same implementation;

21
Inheritance
• In OOP the concept of inheritance provides the idea of
reusability. This means tat we can add additional features to
an existing class without modifying it.

• It is classifieds into different types, they are

– Single level inheritance


– Multiple Inheritance
– Multi-level inheritance
– Hybrid inheritance
– Hierarchial inheritance

22
23
Inheritance Examples

24
Polymorphism
• A object(person,place or thing) acts differently in different situations
• For example, consider the operation of addition.
– For two numbers, the operation will generate a sum.
– If the operands are strings, then the operation would produce a third
string by concatenation.

• The process of making an operator to exhibit different


behaviors in different instances is known as operator
overloading.
• Using a single function name to perform different
type of task is known as function overloading.

25
Polymorphism

SHAP
E
Draw

Circle Obj Triangle Obj Rectangle Obj


Draw(circle Draw(triangle) Draw(rectangl
) e)

Above Fig illustrates that a single function name


can be used to handle different number and
different types of argument.
26
Dynamic Binding
• Binding refers to the linking of a procedure
call to the code to be executed in response to
the call.
• Dynamic binding means that the code
associated with a given procedure call is not
known until the time of the call at run time.
• It is associated with polymorphism and
inheritance.
Message Passing
• A Message for an object is a request for execution of a procedure, and
therefore will invoke a function (procedure) in the receiving object that
generates the desired results.
• Message passing involves specifying the name of object, the name of the
function (message) and the information to be sent. Example:

28

You might also like