Dr. N.G.P.
ARTS AND SCIENCE COLLEGE
Coimbatore – 641 048 | Tamil Nadu | India
24CMU3CB- Problem Solving and
Programming
with C++
Dr. NGPASC
COIMBATORE | INDIA
Unit I Introduction to C++
Contents
• Introduction to C++ : Evaluation of Programming Paradigm – Key Concept
of OOP’S
• Objects
• Classes
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Message Communication
• Popular OOPS Languages
• Merits and Demerits of OOPs
• Application of OOPs.
Dr. NGPASC
COIMBATORE | INDIA
Introduction to C++
• It is an object oriented programming language.
• It was initially named as ‘ C with Classes’.
• C++ was developed by Bjarne Stroustrup at AT& T Bell Laboratories USA in early
1980’s.
• C++ is a superset of C.
• All C programs are also C++ programs and C program will run under C++ compiler.
• C++ additionally supports Classes, function overloading and operator overloading.
• These features enable us to create abstract data types, inherit properties from
existing data types and support polymorphism, thus making C++ a truly object
oriented language.
Dr. NGPASC
COIMBATORE | INDIA
Software evolution:
Software evolution has various layers. Each layer represents improvement over the
previous layer.
Dr. NGPASC
COIMBATORE | INDIA
• These techniques include modular programming, top-down programming, bottom-
up programming and structured programming.
• To build today’s complex software, it is just enough to put together a sequence of
programming statements and set of procedures and modules
• C, structured programming was a powerful tool that enabled programmers
to write moderately complex programs fairly easily.
• However, as the programs grew, even the structured approach failed to show the
desired results in terms of bug-free, easy-to-maintain and reusable programs.
• OOP is an approach to program organization and development that attempts to
eliminate some of the drawbacks of conventional programming methods by
incorporating the best of structured features with several powerful new concepts.
Dr. NGPASC
COIMBATORE | INDIA
Evaluation of Programming Paradigm
• Paradigm can also be termed as method to solve some problem or do some
task.
• Programming paradigm is an approach to solve problem using some
programming language
• There are lots for programming language that are known but all of them need to
follow some strategy when they are implemented and this methodology /
strategy is paradigms.
• Apart from varieties of programming language there are lots of paradigms to
fulfill each and every demand.
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
Procedure oriented programming:
Problem is decomposed into subproblems, each problem can be solved by a
separate function.
• Pictorially that can be represented as follows:
Dr. NGPASC
COIMBATORE | INDIA
• Less importance is given to data , because a data can be globally accessed by any
function
• The global data can be corrupted easily.
• We can not model real world problems very easily.
Dr. NGPASC
COIMBATORE | INDIA
Characteristics of procedure oriented programming:
• Large programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly among functions.
• Functions transform data from one form to another
• Top-down approach is followed.
Object – oriented programming paradigm :
• Importance is given to data , i.e., it can not move freely around the system. By this way we
can protect data from accidental modifications.
• Programs are divided into objects and member functions.
• Object characters are defined by data structures.
• Functions that operate on the data are grouped together in the data structure.
• The data of the object can be accessed only by the functions associated with that object.
Dr. NGPASC
COIMBATORE | INDIA
Organization of data and functions in OOP:
Data is hidden (encapsulated) and can not be accessed by external functions.
• Objects may communicate with each other through functions.
• Bottom – up approach is followed.
Definition of object-oriented programming:
“Object oriented programming is 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”.
Dr. NGPASC
COIMBATORE | INDIA
There are some basic concepts that act as the building blocks of OOPs.
•Classes
• Objects
•Abstraction
•Encapsulation
•Inheritance
•Polymorphism
•Dynamic binding
•Message passing
Dr. NGPASC
COIMBATORE | INDIA
Object
An Object can be defined as an entity that has a state and behavior, or in other words,
anything that exists physically in the world is called an object.
It can represent a dog, a person, a place, a table, etc.
• Object is a run-time entity. Example: an item, a place, a bank account, person,
student, etc.,
• Object is treated as real world objects.
• It consumes memory space like structure in C.
• It interacts by sending messages to one another.
• Each object contains data and code to manipulate that data.
• Object can interact without having to know details of each others data or code.
Example:
Object : STUDENT
Data : Roll number, Name, Marks etc.
Functions: Total, Average, Result etc,
Dr. NGPASC
COIMBATORE | INDIA
Class:
• Objects contain data and code to operate on data.
• Objects are variables of type Class.
• Class is a user-defined data type , each object is associated with the data of type
class.
• Example; birds peacock;
This means the object ‘ peacock’ belongs to the class ‘birds’.
Dr. NGPASC
COIMBATORE | INDIA
Classes
• Class can be defined as a blueprint of the object. It is basically a collection of objects which
act as building blocks.
Dr. NGPASC
COIMBATORE | INDIA
Abstraction
• Abstraction helps in the data hiding process.
• It helps in displaying the essential features without showing the details or
the functionality to the user.
• It avoids unnecessary information or irrelevant details and shows only
that specific part which the user wants to see.
• Classes are known as abstract data type (ADT) because they
define list of abstract attributes such as type, size and functions to
operate on these objects.
Dr. NGPASC
COIMBATORE | INDIA
Encapsulation
• The wrapping up of data and functions together in a single unit is
known as encapsulation.
• It can be achieved by making the data members' scope private
and the member function’s scope public to access these data
members.
• Encapsulation makes the data non-accessible to the outside world.
• Data can be accessed by the function of that class.
• The function provides the interface between the object’s data and
the program.
• The insulation of the data from direct access by the program is
called data hiding.
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
Inheritance:
• Object of one class posses the properities of another class.
• Example : Pet animal(Dog) has the general property of Animals and it posses its
own characteristics.
• Each derived class shares common property with the class from which it is
derived( base class).
• Inheritance supports the concept of reusability.
• The new derived class will have the combined features of both the classes.
Dr. NGPASC
COIMBATORE | INDIA
Polymorphism
• Polymorphism means many forms. It is the ability to take more than one form.
• It is a feature that provides a function or an operator with more than one
definition.
• It can be implemented using function overloading, operator overload, function
overriding, virtual function.
• Example: Consider the operation ADD, it adds two numbers. If the two
operands are string , then the operation produces a third string .
Dr. NGPASC
COIMBATORE | INDIA
Message passing:
• After creating objects from the class definition, we can make communications between
objects with the help of messages or methods
• message passing means specifying the name of the object, the name of the
function(message) and the information to be sent (parameters).
• Example:
Student.rollno
Object message information
Dr. NGPASC
COIMBATORE | INDIA
Dynamic binding(late binding):
• It is a process of linking the procedure calls( codes) to the calling point
(Programs) during runtime.
• At run –time, the code matching the object under current references will be
called.
Dr. NGPASC
COIMBATORE | INDIA
Popular Object-oriented languages:
• Object oriented programming languages are classified into two types based on
the concepts they support.
• The two types are :
• Object-based languages
• Object-oriented languages
• Object-based languages is that style of programming that supports encapsulation
and object identity.
• The other features available in this type are Data hiding, Automatic initialization
and clear up of objects, operator overloading.
• It also supports programming with objects.
Dr. NGPASC
COIMBATORE | INDIA
• The two features that are not supported by object-based languages are dynamic
binding and inheritance.
• E.g. Ada It also supports programming with objects.
• Object-oriented languages incorporates in it features like data hiding,
encapsulation, operator overloading, automatic initialization and clear-up of
objects, along with inheritance and dynamic binding.
• E.g. C++, Java, C#, PHP ,Python, Ruby and pascal
• In general, Object-oriented language=Object-based + inheritance + Dynamic
binding
Dr. NGPASC
COIMBATORE | INDIA
Merits of OOP: (Advantages of OOP)
• This new technology promises greater productivity, better quality software and
lesser maintenance cost. Other advantages are :
• Existing classes can be extended using inheritance that avoids redundant code.
• In OOPs, it is easy to maintain code as there are classes and objects, which helps
in making it easy to maintain rather than restructuring.
• It also helps in data hiding, keeping the data and information safe from leaking
or getting exposed.
• It is easy to partition work in a project based on objects
• Object oriented systems can be easily upgraded from small to large systems
• Software complexities are easily manageable. Object-oriented programming is
easy to implement.
Dr. NGPASC
COIMBATORE | INDIA
Demerits of OOP: (Disadvantages of OOP)
• The length of the programmes developed using OOP language is much larger
than the procedural approach.
• Since the programme becomes larger in size, it requires more time to be
executed that leads to slower execution of the programme.
• It is not suitable for all types of problems.
• Programmers need to have brilliant designing skill and programming skill along
with proper planning because using OOP is little bit tricky.
• Everything is treated as object in OOP so before applying it we need to have
excellent thinking in terms of objects.
Dr. NGPASC
COIMBATORE | INDIA
Applications Of Object-Oriented Programming:
The most popular applications of object-oriented programming has been in the area of user
interface design such as windows.
• Real-time systems
• Simulating and modeling
• Object-oriented databases
• Hypertext, Hypermedia and expert text
• Artificial Intelligence and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• Computer Integrated Manufacture/Computer Aided Design and Manufacture etc
Dr. NGPASC
COIMBATORE | INDIA
Applications Of Object-Oriented Programming:
• Since C++ allows us to create hierarchy-related objects, special object-oriented libraries
can be build that can be used later by many programmers. C++ programs are easily
maintainable and expandable.
• C++ can be used to develop databases, compilers, editors and any complex real – life
application systems.
• 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 application systems.
• It can also be used to develop hierarchy – related real-world problems.
Dr. NGPASC
COIMBATORE | INDIA