C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX
Most C++ compilers don't care what extension you give your source code, but if you don't specify otherwise, many will use .cpp by default
Most frequently used and free available compiler is GNU C/C++ compiler, otherwise you can have compilers either from HP or Solaris if you have respective Operating Systems.
2. 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.
3. 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.
4. 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 be done such as
reading, calculating and printing.
• The primary focus is on functions.
5. 5
• It means “a set of procedures” which is a “set of
subroutines” or a “set of functions“.
• Functions are called repeatedly in a program to
execute tasks performed by them.
• For example, a program may involve-
1. collecting data from user (reading),
2. performing some kind of calculations on the
collected data (calculation), and
3. finally displaying the result to the user when
requested (printing).
• All the 3 tasks of reading, calculating and printing
can be written in a program with the help of 3
different functions which performs these 3
different tasks.
7. 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.
9. 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.
10. 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.
• protects data from accidental modification from
outside functions
• OOP allows decomposition of a problem into a
number of entities called objects
12. Striking features of OOP
• 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
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
13. Difference between Procedure oriented Programming and
Object Oriented Programming
Procedure-Oriented
Programming
Object Oriented Programming
Divided Into In POP, program is divided into
small parts called functions.
In OOP, program is divided into
parts called 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.
14. Data Moving In POP, Data can move
freely from function to
function in the system.
In OOP, objects can move
and communicate with
each other through
member functions.
Expansion To add new data and
function in POP is not so
easy.
OOP provides an easy way
to add new data and
function.
Data Access In POP, Most function
uses Global data for
sharing that can be
accessed freely from
function to function in
the system.
In OOP, data can not move
easily from function to
function. it can be kept
public or private so we can
control the access of data.
15. Data Hiding POP does not have any
proper way for hiding
data so it is less secure.
OOP provides Data
Hiding so provides more
security.
Overloading In POP, Overloading is
not possible.
In OOP, overloading is
possible in the form of
Function Overloading
and Operator
Overloading.
Examples Example of POP are : C,
VB, FORTRAN, Pascal.
Example of OOP are :
C++, JAVA, VB.NET,
C#.NET.
16. • Let's say that you are working for a vehicle parts
manufacturer that needs to update it's online inventory
system.
• Your boss tells you to program two similar but separate
forms for a website, one form that processes information
about cars and one that does the same for trucks.
• For cars, we will need to record the following
information:
Color, Engine Size, Transmission Type, Number of doors
• For trucks, the information will be similar, but slightly
different. We need:
Color, Engine Size, Transmission Type, Cab Size, Towing
Capacity
Real World example for difference
17. Scenario 1
(addition of bus form)
• Suppose that we suddenly need to add a bus form,
that records the following information:
Color, Engine Size, Transmission Type
Number of passengers
• Procedural: We need to recreate the entire form,
repeating the code for Color, Engine Size, and
Transmission Type.
• OOP: We simply extend the vehicle class with a bus
class and add the method:
numberOfPassengers.
18. Scenario 2
(sending email instead of storing
database)
Instead of storing color in a database like we previously
did, for some strange reason our client wants the color
emailed to him.
• Procedural: We change three different forms: cars,
trucks, and buses to email the color to the client rather
than storing it in the database.
• OOP: We change the color method in the vehicle class
and because the car, truck, and bus classes all extend
(or inherit from, to put it another way) the vehicle
class, they are automatically updated.
19. Scenario 3 (moving from generic car
to specific cars)
• We want to move from a generic car to specific makes,
for example: Nissan and Mazda.
• Procedural: We create a new form for each make,
repeating all of the code for generic car information
and adding the code specific to each make.
• OOP: We extend the car class with a Nissan class and a
Mazda class and add methods for each set of unique
information for that car make.
20. Scenario 4
(bug removal)
• We found a bug in the ‘transmission type area’ of our
form and need to fix it.
• Procedural: We open and update each form.
• OOP: We fix the transmission Type method in the
vehicle class and the change perpetuates in every class
that inherits from it.
21. 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.
22. Class v/s Structure
• Class usually used for
large programs.
• Can be inherited.
• Can be null or empty.
• Declared methods in
class are automatically
locked (private by
default).
• Class can have
constructor as well as
destructor.
• Structures usually used
for small programs.
• Cannot be inherited.
• Cannot be NULL.
• Methods can’t be
locked (public by
default).
• Structure couldn’t have
constructor and
destructor. 22
23. Object
• Objects are the basic run-time entities in an object-
oriented system. Object- is an instance(Property) 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.
24. 24
Object has a life cycle. They can be created and destroyed
25. 25
• Hence, every object has 3 important features
– 1. Identity (e.g. Name)
– 2. State (e.g. drive bus, fly plane etc.) – these are
assigned on the basis of the object’s characteristics – A
manager object would have different responsibilities
than a developer, since their job descriptions (a
characteristic) would be different:
– 3. Behaviour (Relationships with other objects)– in order
to send requests/messages to each other, objects need
to have some relationship/connection. E.g. A bus driver
needs to have access/connection to a bus in order to
drive it.
26. 26
Structure of a Class in C++
class name {
declarations
constructor definition(s)
method definitions
}
attributes and
symbolic constants
how to create and
initialize objects
how to manipulate
the state of objects
These parts of a class can
actually be in any order
29. • 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.
30. • 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.
Displayed code is known using "public" modifier
is known as Abstraction.
Hiding complex code using "private" modifier is
known as Encapsulation.
31. 31
Encapsulation hides the complexity of code whereas abstraction is
thinking in terms of design way to increase efficiency.
32. • 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.
33. • 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
34. • 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/method 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.
36. • Dynamic Binding
means code is linked with associated procedural call
at run time (execution time). It is also called as Late
Binding.
Binding refers to the process that is used to convert
identifiers (such as variable and function names)
into machine language addresses. Although binding
is used for both variables and functions.
Late binding is slightly less efficient since it involves
an extra level of indirection.
With early binding, the compiler can tell the CPU to
jump directly to the function’s address.
36
37. • Message Passing
Message Passing is nothing but sending and receiving
of information by the objects same as people
exchange information. So this helps in building
systems that simulate real life. Following are the basic
steps in message passing.
• Creating classes that define objects and its behavior.
• Creating objects from class definitions
• Establishing communication among objects
• In OOPs, Message Passing involves specifying the
name of objects, the name of the function, and the
information to be sent.
37
38. For example,
if “customer” and “account” are to object in a
program, then the customer object may send
a message to the account object requesting
for the bank balance.
Each object contain data, and code to
manipulate data.
Objects can interact without having to know
details of each other’s data or code.
It is sufficient to know the type of message
accepted, and the type of response returned
by the objects. 38
39. 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 (using arrays).
example you want to shoot multiple bullets at same
time in a game.
• 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
40. 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.