Object Oriented Programming through C+
+
INTRODUCTION
◼ Developments in software technology continue
to be dynamic.
◼ New tools and techniques are announced in
quick succession.
◼ This has forced the software engineers and
industry to continuously look for new
approaches to software design and development.
◼ The following issued need to be addressed to
face the crisis:
INTRODUCTION
◼ How to represent real-life entities of problems in
system design?
◼ How to design system with open interfaces?
◼ How to ensure reusability and extensibility of
modules?
◼ How to develop modules that are tolerant of any
changes in future?
◼ How to improve software productivity and decrease
software cost?
◼ How to improve the quality of software?
◼ How to manage time schedules?
Software Evaluation
Software Evaluation
◼ With the advent of languages such as c, structured
programming became very popular and was the main
technique of the 1980’s.
◼ Structured programming was a powerful tool that enabled
programmers to write moderately complex programs fairly
easily.
◼ However, as the programs grew larger, even the structured
approach failed to show the desired result in terms of bug-
free, easy-to- maintain, and reusable programs.
Software Evaluation
◼ 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.
◼ It is a new way of organizing and developing programs and
has nothing to do with any particular language.
◼ However, not all languages are suitable to implement the
OOP concepts easily.
Procedure-Oriented Programming
◼ In the procedure oriented approach, the problem is viewed
as the sequence of things to be done such as reading,
calculating and printing such as COBOL, FORTRAN and
C.
◼ The primary focus is on functions.
◼ A typical structure for procedural programming is shown
in fig below.
◼ The technique of hierarchical decomposition has been used
to specify the tasks to be completed for solving a problem.
Procedure-Oriented Programming
Drawbacks of 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.
◼ In a 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. Global data are
more vulnerable to an inadvertent change by a function.
◼ In a large program it is very difficult to identify what data is
used by which function.
Drawbacks of Procedure-Oriented
Programming
◼ In case we need to revise an external data structure, we
also need to revise all functions that access the data.
◼ This provides an opportunity for bugs to creep in.
◼ The procedural approach is that we do not model real
world problems very well.
◼
◼ This is because functions are action-oriented and do not
really corresponding to the element of the problem.
Relationship between Data and
Functions
Advantages of Procedure-Oriented
Programming
Some Characteristics exhibited by procedure-oriented programming
are:
◼ Emphasis is on doing things (algorithms).
◼ Large programs are divided into smaller programs known as
functions.
◼ Most of the functions share global data.
◼ Data move openly around the system from function to function.
◼ Functions transform data from one form to another.
◼ Employs top-down approach in program design.
Object Oriented Programming
Paradigm
◼ The major motivating factor in the invention of object-
oriented approach is to remove some of the flaws
encountered in the procedural approach.
◼ 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 function that operate on
it, and protects it from accidental modification from
outside function.
Object Oriented Programming
Paradigm
◼ OOP allows decomposition of a problem into a number of
entities called objects and then builds data and function
around these objects.
◼ The organization of data and function in object-oriented
programs is shown in fig.1.3.
◼ The data of an object can be accessed only by the function
associated with that object.
◼ However, function of one object can access the function of
other objects.
Object Oriented Programming
Paradigm
Some of the features of object oriented
programming are:
◼ Emphasis is on data rather than procedure.
◼ Programs are divided into what are known as
objects.
◼ Data structures are designed such that they
characterize the objects.
◼ Functions that operate on the data of an
object are ties together in the data structure.
Object Oriented Programming
Paradigm
◼ Data is hidden and cannot be accessed by
external function.
◼ Objects may communicate with each other
through function.
◼ New data and functions can be easily added
whenever necessary.
◼ Follows bottom up approach in program
design.
Object Oriented Programming
Paradigm
Object Oriented Programming
Paradigm
◼ “Object oriented programming as an
approach that provides a way of
modularizing programs by creating
partitioned memory area for both data and
functions and that can be used as templates
for creating copies of such modules on
demand”.
Basic Concepts of Object Oriented
Programming
◼ It is necessary to understand some of the concepts used
extensively in object-oriented programming. These include:
◼ Objects
◼ Classes
◼ Data abstraction and encapsulation
◼ Inheritance
◼ Polymorphism
◼ Dynamic binding
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 has to handle.
◼ They may also represent user-defined data
such as vectors, time and lists.
Objects
◼ Programming problem is analyzed in term of
objects and the nature of communication
between them.
◼ 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 a record in Pascal, or a
structure in c.
Objects
◼ When a program is executed, the objects interact by
sending messages to one another.
◼ For example, if “customer” and “account” are two
objects 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
◼ Objects can interact without having to know details
of each other’s data or code.
◼ It is a sufficient to know the type of message
accepted, and the type of response returned by the
objects.
◼ Although different author represent them differently
below figure shows two notations that are popularly
used in object-oriented analysis and design.
Objects
Classes
◼ We just mentioned that objects contain data, and code to
manipulate that data.
◼ The entire set of data and code of an object can be made a user-
defined data type with the help of class.
◼ In fact, objects are variables of the type class.
◼ Once a class has been defined, we can create any number of
objects belonging to that class.
◼ Each object is associated with the data of type class with which
they are created.
Classes
◼ A class is thus a collection of objects similar types.
◼ For examples, Mango, Apple and orange members of class fruit.
◼ Classes are user-defined that types and behave like the built-in
types of a programming language.
◼
◼ The syntax used to create an object is not different then the syntax
used to create an integer object in C.
◼ If fruit has been defines as a class, then the statement
◼ Fruit Mango;
◼ Will create an object mango belonging to the class fruit.
Data Encapsulation
◼ The wrapping up of data and function into a single unit
(called class) is known as encapsulation.
◼ Data and encapsulation is the most striking feature of a class.
◼ 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 object’s
data and the program.
◼ This insulation of the data from direct access by the program
is called data hiding or information hiding
Data Abstraction
◼ Abstraction refers to the act of representing essential features without
including the background details or explanation.
◼ Classes use the concept of abstraction and are defined as a list of abstract
attributes such as size, wait, and cost, and function operate on these
attributes.
◼
◼ They encapsulate all the essential properties of the object that are to be
created.
◼ The attributes are some time called data members because they hold
information.
◼ The functions that operate on these data are sometimes called methods or
member function.
Inheritance
◼ Inheritance is the process by which objects of one class
acquired the properties of objects of another classes.
◼ It supports the concept of hierarchical classification
◼ For example the bird, ‘robin’ is a part of class ‘flying bird’
which is again a part of the class ‘bird’.
◼ The principal behind this sort of division is that each
derived class shares common characteristics with the class
from which it is derived as illustrated in fig 1.6.
Inheritance
Inheritance
◼ In OOP, the concept of inheritance provides the idea of reusability.
◼ This means that we can add additional features to an existing class
without modifying it.
◼
◼ This is possible by deriving a new class from the existing one.
◼ The new class will have the combined feature of both the classes.
◼ The real appeal and power of the inheritance mechanism is that it
Allows the programmer to reuse a class i.e almost, but not exactly,
what he wants, and to tailor the class in such a way that it does not
introduced any undesirable side-effects into the rest of classes.
Polymorphism
◼ Polymorphism is another important OOP concept.
◼ Polymorphism, a Greek term, means the ability to take more than on form.
◼ An operation may exhibit different behavior is different instances.
◼ The behavior depends upon the types of data used in the operation.
◼ 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.
Polymorphism
◼ Fig. 1.7 illustrates that a single function name can be used
to handle different number and different types of argument.
◼ This is something similar to a particular word having
several different meanings depending upon the context.
◼ Using a single function name to perform different type of
task is known as function overloading.
◼ Polymorphism is extensively used in implementing
inheritance.
Polymorphism
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.
◼ A function call associated with a polymorphic reference
depends on the dynamic type of that reference.
Dynamic Binding
◼ Consider the procedure “draw” in fig. 1.7. by
inheritance, every object will have this
procedure.
◼ Its algorithm is, however, unique to each object
and so the draw procedure will be redefined in
each class that defines the object.
◼ At run-time, the code matching the object under
current reference will be called.
Message Passing
◼ An object-oriented program consists of a set of
objects that communicate with each other. The
process of programming in an object-oriented
language, involves the following basic steps:
◼ 1. Creating classes that define object and their
behavior,
◼ 2. Creating objects from class definitions, and
◼ 3. Establishing communication among objects.
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.
◼ Object has a life cycle.
◼ They can be created and destroyed. Communication with
an object is feasible as long as it is alive.
◼ Message passing involves specifying the name of object, the
name of the function (message) and the information to be
sent. Example:
Message Passing
Benefits of OOP
◼ Through inheritance, we can eliminate redundant code extend the use of
existing classes.
◼ We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the code
from scratch. This leads to saving of development time and higher
productivity.
◼ The principle of data hiding helps the programmer to build secure program
that can not be invaded by code in other parts of a programs.
◼ It is possible to have multiple instances of an object to co-exist without any
interference.
◼ It is possible to map object in the problem domain to those in the program.
Benefits of OOP
◼ It is easy to partition the work in a project based on objects.
◼ The data-centered design approach enables us to capture
more detail of a model can implemental form.
◼
◼ Object-oriented system can be easily upgraded from small to
large system.
◼ Message passing techniques for communication between
objects makes to interface descriptions with external systems
much simpler.
◼ Software complexity can be easily managed.
Application of OOP
◼ Real-time system
◼ Simulation and modeling
◼ Object-oriented data bases
◼ Hypertext, Hypermedia, and expertext
◼ AI and expert systems
◼ Neural networks and parallel programming
◼ Decision support and office automation
systems
◼ CIM/CAM/CAD systems
Introduction of 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.
◼ Stroustrup, an admirer of Simula67 and a strong supporter of C,
wanted to combine the best of both the languages and create a
more powerful language that could support object-oriented
programming features and still retain the power and elegance
of C.
◼ The result was C++.
◼ Therefore, C++ is an extension of C with a major
addition of the class construct feature of Simula67.
◼ Since the class was a major addition to the original C
language, Stroustrup initially called the new language
‘C with classes’.
◼ However, later in 1983, the name was changed to C++.
The idea of C++ comes from the C increment operator
++, thereby suggesting that C++ is an augmented
version of C.
◼ C+ + is a superset of C. Almost all c programs are also C++
programs. However, there are a few minor differences
that will prevent a c program to run under C++ complier.
◼ The most important facilities that C++ adds on to C care
classes, inheritance, function overloading and operator
overloading.
◼ These features enable creating of abstract data types,
inherit properties from existing data types and support
polymorphism, thereby making C++ a truly object-
oriented language.
Structure of C++ Program
◼ A typical C++ program would contain four sections as
shown in fig. 1.9.
◼ This section may be placed in separate code files and
then compiled independently or jointly.
◼ It is a common practice to organize a program into
three separate files.
◼ The class declarations are placed in a header file and the
definitions of member functions go into another file.
Structure of C++ Program
◼ This approach enables the programmer to
separate the abstract specification of the
interface from the implementation details
(member function definition).
◼ Finally, the main program that uses the class
is places in a third file which “includes: the
previous two files as well as any other file
required.
Structure of C++ Program
Structure of C++ Program
Structure of C++ Program
◼ This approach is based on the concept of
client-server model as shown in fig. 1.10.
◼ The class definition including the member
functions constitute the server that provides
services to the main program known as client.
◼ The client uses the server through the public
interface of the class.
Creating, Compiling and Linking the
Source File
◼ vi, nano, ed editors used in unix environment.
◼ Windows provides integrated environment.
◼ Open new file and save the files.
◼ Save the files .cpp extension.
◼ For compilation in unix cc filename.cpp
◼ In windows alt+f9
◼ For execution in unix ./a.out
◼ In windows ctl+f9
Simple C++ Program
◼ Printing A String
#include<iostream>
Using namespace std;
int main()
{
cout<<” c++ is better than c \n”;
return 0;
}
Program feature
◼ Like C, the C++ program is a collection of function.
◼ The above example contain only one function
main().
◼ As usual execution begins at main().
◼ Every C++ program must have a main().
◼ C++ is a free form language.
◼ With a few exception, the compiler ignore
carriage return and white spaces.
◼ Like C, the C++ statements terminate with
semicolons
Comments
◼ The double slash comment is basically a single line comment.
Multiline comments can be written as follows:
// This is an example of
// C++ program to illustrate
// some of its features
◼ The C comment symbols /*,*/ are still valid and are more
suitable for multiline comments. The following comment is
allowed:
/* This is an example of
C++ program to illustrate
some of its features
The iostream File
◼ We have used the following #include
directive in the program:
#include <iostream>
◼ The #include directive instructs the compiler
to include the contents of the file enclosed
within angular brackets into the source file.
The header file iostream.h should be
included at the beginning of all programs
that use input/output statements.
Namespace
◼ Namespace is a new concept introduced by the ANSI C++
standards committee.
◼ This defines a scope for the identifiers that are used in a
program.
◼ For using the identifier defined in the namespace scope we
must include the using directive, like
◼ Using namespace std;
◼ Here, std is the namespace where ANSI C++ standard class
libraries are defined.
◼ All ANSI C++ programs must include this directive. This will
bring all the identifiers defined in std to the current global
scope.
◼ Using and namespace are the new keyword of C++.
Return Type of main()
◼ In C++, main () returns an integer value to the
operating system. Therefore, every main () in
C++ should end with a return (0) statement;
otherwise a warning an error might occur.
Since main () returns an integer type for main
() is explicitly specified as int. Note that the
default return type for all function in C++ is
int.
Input Operator
◼ The statement
◼ cin >> number1;
◼ Is an input statement and causes the program to wait
for the user to type in a number.
◼ The number keyed in is placed in the variable number1.
◼ The identifier cin (pronounced ‘C in’) is a predefined
object in C++ that corresponds to the standard input
stream. Here, this stream represents the keyboard.
Input Operator
◼ The operator >> is known as extraction or get
from operator.
◼ It extracts (or takes) the value from the keyboard
and assigns it to the variable on its right fig 1.8.
◼ This corresponds to a familiar scanf() operation.
Like <<, the operator >> can also be overloaded.
Input Operator
Output operator
◼ The output statement is
◼ Cout<<”C++ is better than C.”;
◼ Causes the string in quotation marks to be displayed on the
screen.
◼ This statement introduces two new C++ features,
◼ cout and <<. The identifier cout(pronounced as C out) is a
predefined object that represents the standard output stream in
C++.
◼ Here, the standard output stream represents the screen. It is also
possible to redirect the output to other output devices.
◼ The operator << is called the insertion or put to operator.
Output operator
Cascading of I/O Operators
◼ The statement
Cout << “Sum = “ << sum << “\n”;
◼ First sends the string “Sum = “ to cout and then sends the
value of sum.
◼ Finally, it sends the newline character so that the next
output will be in the new line.
◼ The multiple use of << in one statement is called cascading.
◼ When cascading an output operator, we should ensure
necessary blank spaces between different items.
◼ Using the cascading technique, the last two statements can
be combined as follows:
◼ Cout << “Sum = “ << sum << “\n”
◼ << “Average = “ << average << “\n”;
Tokens
◼ The smallest individual units in a program are
known as tokens.
◼ C++ has the following tokens
◼ Keywords
◼ Identifiers
◼ Constants
◼ Strings
◼ operators
keywords
◼ Known as reserved words
◼ Implements specific c++ language features
◼ Can not be used as names of identifiers
keywords
Identifiers
◼ Identifiers refers to the names of variables,
functions, classes etc created by the programmer.
◼ The rules for identifiers are common in c and c++
◼ It consists of a letter followed by any sequence of
letters, digits, and underscores.
◼ The name can not start with a digit.
◼ Identifiers cannot have special characters in them
◼ Identifiers are case-sensitive
◼ Keywords cannot be used as identifiers
constants
◼ Constants refer to the fixed values that do
not change during execution of a program.
◼ C++ supports integer, character, floating and
string constants.
◼ Example:
Strings
◼ Object whose string type is defined in the
<string> file
◼ has a large repository of functions
◼ char cs[ ] = “Napoleon”; // C-string
◼ string s = “Napoleon”; // C++ - string
◼ cout << s << “ has “ << s.length() << “
characters.\n”;
◼ s.replace(5, 2,”ia”); //changes s to “Napolian
Basic Data Types
Primitive Built-in Types:
Several of the basic
types can be
modified using one
or more of these
type modifiers:
signed
unsigned
short
long
Primitive Built-in Types:
Type Typical Bit Width Typical Range
Char or signed char 1byte -128 to 127
unsigned char 1byte 0 to 255
Int or long int or signed int 4bytes -2147483648 to
or long signed int 2147483647
unsigned int 4bytes 0 to 4294967295
short int or signed short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to 65,535
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15
digits)
long double 8bytes +/- 1.7e +/- 308 (~15
digits)