Chapter 1- OOP in C++
Prepared By:
Prof.Sanekt Shah
Assistant Professor
Alpha College of Engineering and Technology
1
What is Object Oriented Programming ?
• Object-oriented programming (OOP) is a programming
paradigm based on the concept of "objects", which may
contain data, in the form of fields, often known
as attributes; and code, in the form of procedures, often known
as methods.
2
Difference between POP and OOP
Procedure Oriented
Programming
Object Oriented
Programming
Divided Into Large programs are divided
into smaller programs known
as functions.
Large programs are divided
into classes and objects.
Importance In POP, Importance is not
given to data but to functions
as well as sequence of actions
to be done.
In OOP, Importance is given
to the data rather than
procedures or functions
because it works as a real
world.
Approach POP follows Top Down
approach.
OOP follows Bottom Up
approach.
Access Specifiers POP does not have any access
specifier.
OOP has access specifiers
named Public, Private,
Protected, etc.
Adding Data and
Function Difficult Easy
3
Difference between POP and OOP
Procedure Oriented
Programming
Object Oriented
Programming
Concept Concepts like inheritance,
polymorphism, data
encapsulation, abstraction,
access specifiers are not
available.
Concepts like inheritance,
polymorphism, data
encapsulation, abstraction,
access specifiers are
available and can be used
easily
Overloading In POP, Overloading is not
possible.
In OOP, overloading is
possible in the form of
Function Overloading and
Operator Overloading.
Example Example of POP are : C, VB,
FORTRAN, Pascal.
Example of OOP are : C++,
JAVA, VB.NET, C#.NET
4
Concept of OOP Object
An object is an instance of a class.
An object means anything from real world like as person, computer,
bench etc...
Every object has at least one unique identity.
An object is a component of a program that knows how to interact with
other pieces of the program.
An object is the variable of the type class.
For example, table,pen
5
Concept of OOP
Class
A class is a template that specifies the attributes and behaviour of
things or objects.
A class is a blueprint or prototype from which objects are
created.
A class is the implementation of an abstract data type (ADT).
It defines attributes and methods.
6
Example class employee
// Class
{
char name[10]; // Data member
int id; // Data member
public:
void getdata() // Member function
{
cout<<”enter name and id of employee: ”; cin>>name>>id;
}
}a; // Object Declaration
• In above example class employee is created and ‘a’ is object of this class. • Object declaration can be also done in main() function as follows:
int main()
{
employee a;
} 7
Concept of OOP
Data Abstraction Just represent essential features without including the background
details.
They encapsulate all the essential properties of the objects that are to
be created.
The attributes are sometimes called ‘Data members’ because they hold information.
The functions that operate on these data are sometimes called
‘methods’ or ‘member functions’. It is used to implement in class to provide data security.
8
Concept of OOP
Data Encapsulation
Wrapping up of a data and functions into single unit is known as
encapsulation.
In C++ the data is not accessible to the outside world.
Only those functions can access it which is wrapped together within
single unit.
9
Inheritance
• Inheritance is the process, by which class can acquire the properties and methods of another class.
• The mechanism of deriving a new class from an old class is called inheritance.
• The new class is called derived class and old class is called base class.
• The derived class may have all the features of the base class.
• Programmer can add new features to the derived class.
• For example, Father is a base class and Son is derived class.
10
Concept of OOP
Polymorphism
A Greek word Polymorphism means the ability to take more than one form.
Polymorphism allows a single name to be used for more than one related purpose.
The concept of polymorphism is characterized by the idea of ‘one interface, multiple
methods’,
That means using a generic interface for a group of related activities.
The advantage of polymorphism is that it helps to reduce complexity by allowing one
interface to specify a general class of action’. It is the compiler’s job to select the specific
action as it applies to each situation.
It means ability of operators and functions to act differently in different situations.
Example:
int total(int, int);
int total(int, int, float); 11
Concept of OOP Static Binding
Static Binding defines the properties of the variables at compile time. Therefore
they can’t be changed.
Dynamic Binding
Dynamic Binding means linking of procedure call to the code to be executed in
response to the call.
It is also known as late binding, because It will not bind the code until the time
of call at run time. In other words properties of the variables are determined at
runtimes.
It is associated with polymorphism and inheritance.
Message Passing
A program contains set of object that communicates with each other.
12
Concept of OOP
Basic Step to Communicate
Creating classes that define objects and their behaviour.
Creating objects from class definition
Establishing communication among objects.
Message passing involves the object name, function name and the information
to be sent.
Example: employee.salary(name);
In above statement employee is an object. salary is message, and name is
information to be sent.
13
Benefit of OOP
1. We can eliminate redundant code though inheritance.
2. Saving development time and cost by using existing module.
3. Build secure program by data hiding.
4. It is easy to partition the work in a project based on object.
5. Data centered design approach captures more details of a programming model.
6. It can be easily upgraded from small to large system.
7. It is easy to map objects in the problem domain to those in the program.
8. Through message passing interface between objects makes simpler description with external system.
9. Software complexity can be easily managed.
14
Application of OOP 1. Real-time systems
2. Simulation and modeling
3. Object oriented database
4. Artificial intelligence and expert systems
5. Neural networks and parallel programming
6. Decision support and office automation
7. CIM/CAM/CAD systems.
8. Distributed/Parallel/Heterogeneous computing
9. Data warehouse and Data mining/Web mining 15
why do we need preprocessor directive
#include <iostream.h>?
• It is the predefined library function used for input and output also called as header files.
• iostream is the header file which contains all the functions of program like cout, cin etc. and #include tells the
preprocessor to include these header file in the program.
• include <iostream.h>
#include is known as a preprocessor directive,which is used to load files…
• <> indicate the start and end of file name to be loaded.. you can use " " quotes too instead of <>
<> are for header files(files with extension .h)
"" for c files (files with extension .c)
• The file iostream.h is located in your include path. The include path indicates the directories on your computer in
which to search for a file, if the file is not located in the current directory.
• iostream.h is a file containing definitions for input/output functions that u use in ur program.
16
C++ is not a pure Object Oriented Language..
• C++ is not a pure object oriented
language because you can write code
without creating a class in C++, whereas
Java IS a pure object oriented language
because every function requires a class.
17
Explain the difference between structure and class
in C++.
18
• structure :- In structure have a by default
public.
In class have a by default private.
• Structure cannot be inherited. But class can be
inherit.
• There is no data hiding features comes with
structures. Classes do, private, protected and
public.
A structure can't be abstract, a class can.
A structure is a value type, while a class is a
reference type.
• A structure is contain only data member , but
class
contain data member and member function.
• In a Structure we can't initialise the value to the
variable but in class variable we assign the
values.
• Structure are value type, They are stored as a
stack on memory. where as class are reference
type. They are stored as heap on memory.
Standard output (cout)
19
1. The standard output by default is the screen, and the C++ stream object defined to access
it is cout. The cout is used together with the insertion operator, which is written as <<.
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
2. Multiple insertion operations (<<) may be chained in a single statement:
cout << "This " << " is a " << "single C++ statement";
Output:
This is a single C++ statement.
Standard output (cout)
3. To insert a line break, a new-line character shall be inserted at the exact position the line should be broken. In C++, a new-line character can be specified as \n (i.e., a backslash character followed by a lowercase n).
For example:
cout << "First sentence.\n";
cout << "Second sentence.\nThird sentence.";
Output:
First sentence. Second sentence. Third sentence.
4. The endl manipulator can also be used to break lines. For example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
Output:
First sentence.
Second sentence.
The endl manipulator produces a newline character, exactly as the insertion of '\n' does.
20
Standard input (cin)
21
The standard input by default is the keyboard, and the C++ stream object defined to
access it is cin. For formatted input operations, cin is used together with the extraction
operator, which is written as >>. This operator is then followed by the variable where
the extracted data is stored. For example:
int age;
cin >> age;
The first statement declares a variable of type int called age, and the second extracts
from cin a value to be stored in it. This operation makes the program wait for input
from cin; generally, this means that the program will wait for the user to enter some
sequence with the keyboard.
// Example
#include <iostream.h>
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
Output:
• Please enter an integer value: 2The value you entered is 2 and its double is 4.
22
University Questions
1. What is Object Oriented Programming? Explain Features of Object Oriented Programming Language.(DEC-2010)(MAY-
2012)(DEC-2012)(Slide No.2,5 to 11)
2. Explain Object Oriented Methodology and Themes(DEC-2011) )(Slide No.2,5 to 11)
3. Explain Difference Between Object Oriented Programming and Procedure Oriented Programming ?(DEC-2013).(slide
3,4)
4. Explain Basic Characteristics of Object Oriented Programming(May-2014). )(Slide No.2,5 to 11)
5. why do we need preprocessor directive #include <iostream.h>?(slide no 16)(winter -2016)
6. What are the unique advantage of Object Oriented Paradigm?(slide no 14)(winter 2016)
7. Why C++ is not a pure Object Oriented Language?(Slide No 17)(Summer-2016)
8. Difference Between Structure and Class (Slide no 18) (Winter 2015)
9. Explain << and >> Operator (slide no 19 to 22) (winter 2015)
23