Oops Pmodule1
Oops Pmodule1
Oops Pmodule1
Procedural Language
C, Fortran etc.
Tough to Handle Lengthy Programs
Divided in Functions/ Modules
Structured Programming
Problems with Structured Programming
Accessibility of Data Across Functions
Global vs. Local Data in Procedural Language
Too many Global Data tough to Handle
Oops is methodology or paradigm to design a program
using classes and objects
Code Reusability
Efficient Development
Efficient Handling
Efficient Accessibility
Easy to debug
Easy to bind Data & Functions
simple C++ programs
C++ data types
Data Types-C++
INPUT AND OUTPUT STATEMENTS IN C++
syntax
Cin>> variable name;
Cout<< variable name;
example
Cin>> a;
<< is called insertion operation and output operator.
Abstraction refers to the act of representing essential features without including the back ground details or
explanation.
Classes use the concept of abstraction and are defined as a list of attributes such as size, weight, cost and
functions to operate on these attributes.
It encapsulate all essential properties of the object that are to be created. The attributes are called as data
members as they hold data and the functions which operate on these data are called as member functions.
Class use the concept of data abstraction so they are called abstract data type (ADT)
Polymorphism
Polymorphism comes from the Greek words “poly” and
“morphism”. “poly” means
many and “morphism” means form i.e.. many forms.
Polymorphism means the ability to take more than
one form. For example, an operation have different behavior
in different instances. The behavior depends
upon the type of the data used in the operation.
Different ways to achieving polymorphism in C++ program:
1) Function overloading 2) Operator overloading
Single function name can be used to handle
different number and different types of arguments
Single function name performs different type of taks
is known as function overloading
Inheritance:
Inheritance is the process by which one object can acquire the properties of another.
Inheritance is the most promising concept of OOP, which helps realize the goal of constructing software
from reusable parts, rather than hand coding every system from scratch. Inheritance not only supports
reuse across systems, but also directly facilitates extensibility within a system. Inheritance coupled with
polymorphism and dynamic binding minimizes the amount of existing code to be modified while
enhancing a system.
When the class child, inherits the class parent, the class child is referred to as derived class (sub
class) and the class parent as a base class (super class). In this case, the class child has two parts: a derived
part and an incremental part. The derived part is inherited from the class parent. The incremental part is
the new code written specifically for the class child.
Dynamic binding:
Binding refers to linking of procedure call to the code to be executed in response to the call.
Dynamic binding(or late binding) means the code associated with a given procedure call in not known
until the time of call at run time
Message passing:
An object oriented program consists of set of object that communicate with each other.
The process involoves in
1. Creating classes that define objects
2. Create objects from class definition
3. Establishing communication between objects
Objects communicates with each other by sending and receiving information .
A message for an object is a request for execution of a procedure and there fore invoke the function that is
called for an object and generates result
Benefits of oops
Oop offers several benefits to both the program designer and the user. Object-oriented contributes to
the solution of many problems associated with the development and quality of software products.
1. Through inheritance we can eliminate redundant code and extend the use of existing classes.
2. 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.
3. This principle of data hiding helps the programmer to build secure programs that can’t be invaded by
code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out any interference.
5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large systems.
7. Message passing techniques for communication between objects makes the interface
description with external systems much simpler.
8. Software complexity can be easily managed.
APPLICATION OF OOPs
The most popular application of oops in the area of user interface design such as windows. There are hundreds of
windowing systems developed using oop techniques.
Real business systems are often much more complex and contain many more objects with complicated attributes and
methods. Oop is useful in this type of applications because it can simplify a complex problem. The promising areas for
application of oop includes.
1. Real – Time systems.
2. Simulation and modeling
3. Object oriented databases
4. Hypertext,hypermedia and expertext.
5. Al and expert systems.
6. Neural networks and parallel programming.
7. Dicision support and office automation systems.
8. CIM / CAM / CAD system.
Procedural oriented programming
It is defined as a programming language derived from the structure programming and based on
calling procedures. The procedures are the functions, routines, or subroutines that consist of the
computational ste
During the program's execution, a procedure can be called at any point, either by other
procedures or by itself.
Procedural programming follows a top-down approach during the designing of a program. It
gives importance to the concept of the function and divides the large programs into smaller parts
or called as functions.
Object oriented programming
can be defined as a programming model which is based upon the concept of objects. Objects
contain data in the form of attributes and code in the form of methods. In object-oriented
programming, computer programs are designed using the concept of objects that interact with the
real world. Object-oriented programming languages are various but the most popular ones are
class-based, meaning that objects are instances of classes, which also determine their types.
OOP is said to be the most popular programming model among developers. It is well suited for
programs that are large, complex, and actively updated or maintained. It makes the development
and maintenance of software easy by providing major concepts such as abstraction, inheritance,
polymorphism, and encapsulation.
Procedural oriented programming Object oriented programming
1.
Definition It is a programming language that is derived from Object-oriented programming is a computer
structure programming and based upon the concept of programming design philosophy or methodology
calling procedures. It follows a step-by-step approach that organizes/ models software design around
in order to break down a task into a set of variables and data or objects rather than functions and logic.
routines via a sequence of instructions.
2.
Security It is less secure than OOPs. Data hiding is possible in object-oriented
programming due to abstraction. So, it is more
secure than procedural programming.
3.
Approach It follows a top-down approach. It follows a bottom-up approach.
4.
Data movement In procedural programming, data moves freely within In OOP, objects can move and communicate with
the system from one function to another. each other via member functions.
5.
Orientation It is structure/procedure-oriented. It is object-oriented.
6.
Access modifiers There are no access modifiers in procedural The access modifiers in OOP are named as
programming. private, public, and protected.
7.
Inheritance Procedural programming does not have the concept of There is a feature of inheritance in object-
inheritance. oriented programming.
8.
Code reusability There is no code reusability present in procedural It offers code reusability by using the feature of
programming. inheritance.
9.
Overloading Overloading is not possible in procedural In OOP, there is a concept of function
programming. overloading and operator overloading.
10.
Importance It gives importance to functions over data. It gives importance to data over functions.
11.
Virtual class In procedural programming, there are no virtual In OOP, there is an appearance of virtual classes
classes. in inheritance.
12.
Complex It is not appropriate for complex problems. It is appropriate for complex problems.
problems
13.
Data hiding There is not any proper way for data hiding. There is a possibility of data hiding.
14.
Program division In Procedural programming, a program is divided into In OOP, a program is divided into small parts that
Symbolic constant
Constant are like variables except that their values cannot be changed
Syntax
Const <data type> < variable name> = <value>;
example
Const float pi =13.4;
Operators in c++ ||
#include <iostream..h>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter the values of 'a' and ’b'";
cin>>a;
cin>>b;
add=a+b;
sum=a-b;
div=a/b;
mul=a*b;
cout<<“The values that you have entered are : "<<a<<" , "<<b;
return 0;
}
C++ program- Relational operator
// relational operators
#include <iostream>
using namespace std;
int main()
{int x = 10;
int y = 15;
int z = 10;
cout<<(x < y);
cout<<(y > z);
cout<<(y >= x );
cout<<(z >= x );
cout<<(x==z);
return 0
;}
Escape sequences
◦ Escape sequences are control characters used to move cursor
Student s;
To access the struct members, we use the instance of the struct and the dot (.)
s. rno = Al23;
Sample Structure program
struct Student {
char name[50];
int roll_number;
char grade;
};
int main()
{
struct Student s;
cout << "Enter the rno ";
cin>> s.roll_number;
int main()
{
student s[100];
int i;
for(i=1;i<=3;i++)
{
cin>>s[i].rno>>s[i].name>>s[i].grade;
}
cout<< "struct value";
for(i=1;i<=3;i++)
{
cout<<s[i].rno<<endl;
cout<<s[i].name<<endl;
cout<<s[i].grade<<endl;
}
return 0;
}
Struct as Function Argument
An array within a structure is a member of the structure and can be accessed just as we access
other elements of the structure.
#include <iostream>
using namespace std;
struct Student
{
char name[50];
int roll_number;
float mark[5]; int tot;float avg;
};
int main() {
int n, i;
cout << "Enter the number of students: ";
cin >> n;
struct Student s[10];
cout << "\n students' information:\n";
for (i = 0; i < n; i++)
Assignment-lab
1. a) Create a Structure STUDENT with the fields name, roll_num, marks (marks in 5 different subjects),
total and average and write the functions to perform following operations:
1. Input the details of n students.
2. Calculate the total and average marks for each student.
3. Sort the details of students based on roll number.
Assignment-function
Write c++ code , a function to find sum of N numbers , a function to find factorial by passing
argument initial value
and final value for the loop and return the output to main function.