0% found this document useful (0 votes)
1 views77 pages

UNIT-I-Principles of Object Oriented Programming (1)

The document covers the principles of Object-Oriented Programming (OOP) using C++, highlighting the software crisis, quality issues, and the evolution of software technology. It explains key concepts of OOP such as classes, objects, encapsulation, inheritance, polymorphism, and dynamic binding, along with their benefits and applications. Additionally, it contrasts C and C++, providing examples of simple C++ programs and their structure.

Uploaded by

roughusezomato
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)
1 views77 pages

UNIT-I-Principles of Object Oriented Programming (1)

The document covers the principles of Object-Oriented Programming (OOP) using C++, highlighting the software crisis, quality issues, and the evolution of software technology. It explains key concepts of OOP such as classes, objects, encapsulation, inheritance, polymorphism, and dynamic binding, along with their benefits and applications. Additionally, it contrasts C and C++, providing examples of simple C++ programs and their structure.

Uploaded by

roughusezomato
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/ 77

UNIT – I

Chapter-1
Principles of Object-Oriented Programming

Text Book : 1

E. Balaguruswamy, “Object Oriented Programming with C++”

[18TE46]

Mr.Mohana, Electronics & Telecommunication Engineering, RVCE 1


Syllabus

Mr.Mohana, Electronics &


2
Telecommunication Engineering, RVCE
Software Crisis
• Developments in software technology
• New tools and techniques
• new approaches to software design and development
• increasing complexity of software systems as well as
the highly competitive nature of the industry
• rapid advances appear to have created a situation of
crisis within the industry

Mr.Mohana, Electronics &


3
Telecommunication Engineering, RVCE
• Issued need to be addressed to face the crisis:

Mr.Mohana, Electronics &


4
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
5
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
6
Telecommunication Engineering, RVCE
Quality issues: Must be considered
• Correctness
• Maintainability
• Reusability
• Openness and interoperability
• Portability
• Security
• Integrity
• User friendliness

Mr.Mohana, Electronics &


7
Telecommunication Engineering, RVCE
Software Evolution
• Evolution of software technology

Mr.Mohana, Electronics &


8
Telecommunication Engineering, RVCE
Procedure-Oriented Programming

• Problem is viewed as sequence of things to be


done such as reading, calculating and printing
such as cobol, fortran and c.

• The primary focus is on functions.

Mr.Mohana, Electronics &


9
Telecommunication Engineering, RVCE
Typical structure of procedural
oriented programs

Mr.Mohana, Electronics &


10
Telecommunication Engineering, RVCE
Some Characteristics exhibited by procedure-oriented
programming are:

Mr.Mohana, Electronics &


11
Telecommunication Engineering, RVCE
Relationship of data and Functions in procedural programming

Mr.Mohana, Electronics &


12
Telecommunication Engineering, RVCE
Main drawbacks
• Local and global

• Large program it is very difficult to identify


what data is used by which function.

• do not model real world problems very well.

Mr.Mohana, Electronics &


13
Telecommunication Engineering, RVCE
Object Oriented Paradigm
• OOP treats data as a critical element in the program development
and does not allow it to flow freely around the system.

• protects it from accidental modification from outside function.

• decomposition of a problem into a number of entities called


objects and then builds data and function around these objects.

Mr.Mohana, Electronics &


14
Telecommunication Engineering, RVCE
Organization of data and function in OOP

Mr.Mohana, Electronics &


15
Telecommunication Engineering, RVCE
• Some of the features of object oriented
programming are:

Mr.Mohana, Electronics &


16
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
17
Telecommunication Engineering, RVCE
Basic Elements of OOPS

Mr.Mohana, Electronics &


18
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
19
Telecommunication Engineering, RVCE
Basic Concepts of Object-Oriented
Programming
• Objects
• Classes
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing.

Mr.Mohana, Electronics &


20
Telecommunication Engineering, RVCE
Objects
• Basic run-time entities.

• Real word objects.

• User defined data.

• Object interaction (data and code)

• Ex: customer and account are two objects.

.
Mr.Mohana, Electronics &
21
Telecommunication Engineering, RVCE
Representing an object

Mr.Mohana, Electronics &


22
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
23
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
24
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
25
Telecommunication Engineering, RVCE
Classes
• User-defined data type.
• Collection of objects of similar type (or)
collection of variables and functions.
Syntax:
Class name
{
Variables
functions
};

• Objects variables.

Mr.Mohana, Electronics &


26
Telecommunication Engineering, RVCE
Encapsulation
• Wrapping up of data and functions
• Functions which are wrapped in the class can access it.
• Interface between objects
• Data hiding or information hiding: insulation of data from the
direct access.
• Data Abstraction
• Representing the essential features without including the
background details.
• Classes uses the concept of data abstraction : Abstract data types

Mr.Mohana, Electronics &


27
Telecommunication Engineering, RVCE
Data Abstraction
• Data Abstraction
• Representing the essential features without including the
background details.
• Classes uses the concept of data abstraction : Abstract data
types

Mr.Mohana, Electronics &


28
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
29
Telecommunication Engineering, RVCE
Inheritance
• Hierarchical classification.
• Sharing of common characteristics.
• Reusability.

Mr.Mohana, Electronics &


30
Telecommunication Engineering, RVCE
Polymorphism
• Ability to take more than one form.

• Operation may exhibit different behaviours in different


instances

• Operator Overloading : Making an operator to exhibit


different behaviours in different instances

• Function Overloading: Single function name to perform


different types of tasks.

• Extensively used in implementing inheritance.

Mr.Mohana, Electronics &


31
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
32
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
33
Telecommunication Engineering, RVCE
Dynamic binding

• Linking of a procedure call to the code to be


executed in response to the call.

• At run-time the code matching the object


under current reference will be called.

Mr.Mohana, Electronics &


34
Telecommunication Engineering, RVCE
Message passing.

• set of objects communicate with each other.


Steps:
1. Creating classes that define objects and their
behavior,
2. Creating objects from class definitions , and
3. Establishing communication among objects.
Ex: employee.salary(name)
• Life cycle, created and destroyed.

Mr.Mohana, Electronics &


35
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
36
Telecommunication Engineering, RVCE
Benefits of OOP
• Elimination of redundant code & use of existing
code.
• Secure programs.
• Map objects in the problem domain.
• Partition the work
• Easy to upgrade
• Automatic initialization and clear-up of objects.
• Overloading
• Quality software product.
• Complexity can be managed.
Mr.Mohana, Electronics &
37
Telecommunication Engineering, RVCE
Applications of C++
• Image Processing
• Pattern Recognition
• Database Management Systems
• Web based Applications
• Distributed Computing and Applications
• Component based Applications
• Business Process Reengineering
• Enterprise resource planning
• Data security and management
• Mobile Computing.

• Data Warehouse and Data Mining.

• Parallel Computing.
Mr.Mohana, Electronics &
38
Telecommunication Engineering, RVCE
• Real time systems.

• Object Oriented Relational database Management System(OORDBMS).

• Artificial Intelligence and Expert System.

• System Software

• Office Automation System

• Neural Networks

Mr.Mohana, Electronics &


39
Telecommunication Engineering, RVCE
Characteristics of some OOP Languages

Mr.Mohana, Electronics &


40
Telecommunication Engineering, RVCE
Differences between C and C++
• C is Procedure Oriented Programming Language (POP).
C++ is Object Oriented Programming Language (OOP).

• C program has extension .C


C++ program has extension .cpp

• C files use stdio.h as standard header file


• whereas C++ use iostream.h as standard header file

Mr.Mohana, Electronics &


41
Telecommunication Engineering, RVCE
• The I/O commands for C are scanf & printf,
• The I/O commands for for C++ are cin and cout

• C++ has also some special features which were not present in C.
For example: GUI programming tools.

• C++ provides the concept of Data hiding as it supports the


classes
• C does not provide any such concept of data hiding as it does
not support classes.

• C does not support any features of Inheritance.


Inheritance is one of the special characteristics of inheriting
functions and other features of one class to another class.
• C++ provides this feature
Mr.Mohana, Electronics &
42
Telecommunication Engineering, RVCE
• unable to reuse code of c language
• in c++ we can reuse the code

• C++ provides different data visibility modes : Public, Private,


and Protected.
• C does not have different visibility mode to store data. It stores
data in a default mode irrespective of the data type.

• C is used to develop system software.


C++ is mostly used to modal real life problem to program and
use to develop application programs.

Mr.Mohana, Electronics &


43
Telecommunication Engineering, RVCE
UNIT – I

Chapter-2
Beginning with C++

Text Book : 1

E. Balaguruswamy, “Object Oriented Programming with C++”

[18TE46]
Mr.Mohana, Electronics &
44
Telecommunication Engineering, RVCE
Simple C++ Program
• This is the simples C++ program. It displays a
sentence on the console or the terminal
#include <iostream>
int main()
{
cout << "Welcome to C++\n";

return 0;
}
Mr.Mohana, Electronics &
45
Telecommunication Engineering, RVCE
• Collection of functions
• C++ statements terminates with semicolons.

Mr.Mohana, Electronics &


46
Telecommunication Engineering, RVCE
Comments
1. Single line comment.
2. Multiline comment.
Single line comment
• Starts with the double slash symbol //(double
slash) and terminate at the end of the line.
• Can not insert within the text of a program line.
• Ex. // single line.
Multiline comment
• Starts with /* and ends with */
Ex. /* multiline comment */
for(j=0;j<n; /* loops n times */ j++)
Mr.Mohana, Electronics &
47
Telecommunication Engineering, RVCE
Output operator
• cout << " C++ is better than c \n";
• Object cout has a simple interface.
• Cout<<string;

• Operator << is bit-wise left-shift operator.

• Used for different purposes, depending on the context.


• Insertion or put to operator
• Overloading Mr.Mohana, Electronics &
48
Telecommunication Engineering, RVCE
The iostream file
#include <iostream.h>
• Causes the preprocessor to add the contents of the
iostream file to the program.
Return type of main()
• main() returns an integer type value to the OS.
• Every main() in c++ should end with a return(0)
statement.
• main() returns an integer type value.

NOTE : default return type for all functions in C++ is


int.
Mr.Mohana, Electronics &
49
Telecommunication Engineering, RVCE
PROGRAM-1:Average of two numbers
#include <iostream.h>
int main()
{
float number1, number2, sum, average;
cout<<“ enter two numbers : ” ;
cin>>number1;
cin>>number2;
sum=number1+number2;
average=sum/2
cout<< “ sum= ”<< sum <<“ \n ”;
cout<< “ average= ”<< average <<“ \n ”;
return 0;
}
Mr.Mohana, Electronics &
50
Telecommunication Engineering, RVCE
output
Enter two number: 6.5 7.5
Sum= 14
Average=7

Mr.Mohana, Electronics &


51
Telecommunication Engineering, RVCE
Variables
float number1, number2, sum, average;

Note: All variables must be declared before


they are used in the program.

Mr.Mohana, Electronics &


52
Telecommunication Engineering, RVCE
PROGRAM-2:Example-declare each variable before it is
needed
#include <iostream.h>
int main()
{
float f;
double d;

cout << "Enter two floating point numbers: ";


cin >> f >> d;

cout << "Enter a string: ";


char str[80]; // str declared here, just before 1st use
cin >> str;

cout << f << " " << d << " " << str;
return 0;
} Mr.Mohana, Electronics &
53
Telecommunication Engineering, RVCE
Input operator
• cin>>number1;
• Causes the program to wait for the user to type in a number.

• Overloading.
• Extraction or get from operator
Mr.Mohana, Electronics &
54
Telecommunication Engineering, RVCE
Cascading of I/O Operators
• Multiple use of insertion or extraction operator.
• cout<< “ sum= ”<< sum <<“ \n ”;
• cascading
cout<< “ sum= ”<< sum <<“ \n ”
<< “ average= ”<< average <<“ \n ”;

cout<< “ sum= ”<< sum <<“ , ”


<< “ average= ”<< average <<“ \n ”;
Cascade input operator
cin>>number1>> number2;
• overloaded Mr.Mohana, Electronics &
55
Telecommunication Engineering, RVCE
PROGRAM-3: AN EXAMLE WITH CLASS (OR)
USE OF CLASS
#include <iostream.h>
Class person
{
char name[30];
int age;
public:
void getdata (void);
void display(void)
};
Mr.Mohana, Electronics &
56
Telecommunication Engineering, RVCE
Void person :: getdata(void)
{
cout << “ enter name : ”;
cin >> name;
cout << “ enter age : ”;
cin >> age;
}
Void person :: display(void)
{
cout << “ \n name: ”<< name;
cout << “ \n age: ”<< age;
}
Mr.Mohana, Electronics &
57
Telecommunication Engineering, RVCE
Int main()
{
person p;
p.getdata();
p.display();
return 0;
}

Mr.Mohana, Electronics &


58
Telecommunication Engineering, RVCE
OUTPUT:
enter name: R.V.C.E
enter age: 55

Name: R.V.C.E
Age: 55

Mr.Mohana, Electronics &


59
Telecommunication Engineering, RVCE
Structure of the c++ program

• Include files

• Class declaration

• Member function definitions

• Main function program

Mr.Mohana, Electronics &


60
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
61
Telecommunication Engineering, RVCE
List of C++ Compilers
• Some compilers that can be downloaded for free (do check
their conditions/licenses before attempting commercial use)

Mr.Mohana, Electronics &


62
Telecommunication Engineering, RVCE
• Compilers that require payment (some allow free
downloads for trial periods):

Mr.Mohana, Electronics &


63
Telecommunication Engineering, RVCE
Simple C++ Programs

Mr.Mohana, Electronics &


64
Telecommunication Engineering, RVCE
C++ Hello World Program

Mr.Mohana, Electronics &


65
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
66
Telecommunication Engineering, RVCE
C++ Program to Add Two Numbers

Mr.Mohana, Electronics &


67
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
68
Telecommunication Engineering, RVCE
Swap Numbers (Using Temporary Variable)

Mr.Mohana, Electronics &


69
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
70
Telecommunication Engineering, RVCE
swap Numbers Without Using Temporary
Variables

Mr.Mohana, Electronics &


71
Telecommunication Engineering, RVCE
Mr.Mohana, Electronics &
72
Telecommunication Engineering, RVCE
Object Oriented Paradigm

Mr.Mohana, Electronics &


73
Telecommunication Engineering, RVCE
Features of object oriented programming
• 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.

• 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.

Mr.Mohana, Electronics &


74
Telecommunication Engineering, RVCE
Summary
• Software technology has evolved through a series of phases during the last five
decades.
• POP follows top-down approach where problem is viewed as sequence of task to
be performed and functions are written for implementing these tasks.
• POP has two major drawbacks:
• Data can move freely around the program.
• It does not model very well the real-world problems.
• OOP was inventing to overcome the drawbacks of POP. It follows down -up
approach.
• In OOP, problem is considered as a collection of objects and objects are instance
of classes.
• Data abstraction refers to putting together essential features without including
background details.

Mr.Mohana, Electronics &


75
Telecommunication Engineering, RVCE
• Inheritance is the process by which objects of one class acquire properties of
objects of another class.
• Polymorphism means one name, multiple forms. It allows us to have more than
one function with the same name in a program.
• Dynamic binding means that the code associated with a given procedure is not
known until the time of the run time.
• Message passing involves specifying the name of the object, the name of the
function and the information to be sent.
• C++ is a superset of C language.
• C++ ads a number of features such as objects, inheritance, function overloading
and operator overloading to C.
• C++ supports interactive input and output features and introduces anew comment
symbol // that can be used for single line comment.
• Like C programs, execution of all C++ program begins at main() function.

Mr.Mohana, Electronics &


76
Telecommunication Engineering, RVCE
Thank you

Mr.Mohana, Electronics &


77
Telecommunication Engineering, RVCE

You might also like