Oops Pmodule1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 53

Course: Object Oriented Programming System With C++

Module 1: Introduction to OOPs Concept

Department of Computer Science & Engineering


Course Outcome

Upon completion of this course, the students will be able to


1. Explain the fundamental basic concepts of OOPS programming.
2. Familiarize with constructing the C++ program using loops,
conditional statements and functions.
3. Implement class, create objects in C++ programming language.
4. Use C++ programming language to implement OOPs concepts
such as inheritance, polymorphism, and dynamic binding.
5. Implement templates, virtual functions, friend functions and
exception handling.
Target Program Outcomes

 PO1. Engineering knowledge


 PO2. Problem analysis
 PO3. Design/development of solutions
 PO5. Modern tool usage
 PO10. Communication
 PO11. Project management and finance
Module Outcome

Upon completion of this module, you will be able to


Explain the fundamental basic concepts of OOPS
programming.

PO1. Engineering knowledge


PO2. Problem analysis
PO3. Design/development of solutions
PO5. Modern tool usage
Module Reference

Chapter 1: The Big Picture


Chapter 2: C++ Programming Basics
Book: Object Oriented Programming in C++, by Robert Lafore (Fourth Edition)
Object oriented programming -History
 C++ is a general-purpose, middle-level language that was originally
developed by Danish computer scientist Bjarne Stroustrup in 1979 at Bell
Laboratories USA
 C++ is an Object Oriented Programming language and an extension of C
which is a procedural oriented Programming language
 C++ is a mid-level programming language is a combination of low-level &
high-level programming languages( GCC is also written in C/C++)
 It is widely used for systems programming, gaming, finance, scientific
computing, and robotics, among other areas. It’s especially useful in building
complex software systems
Why OOPs

 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

It simplifies the program development

OOPs Allows us to decompose a problem into a number of


Features entities called objects and then builds data and functions.

Oops give clear structure to programs and allow code to be


reused.

Allows portability, can be used develop applications that


can be adapted to multiple platforms
C++ compliers

borland GCC (g+ Oracle c+


Turbo C++
c++ +) +

Edge c++ PGC ++ Visual c++ XL c++


General Structure of C++
Why OOPs contd.

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

>> is called stream extraction operators and input operator Cout<< a;

•Input Stream: If the direction of flow of bytes is from the


device(for example, Keyboard) to the main memory then this process
is called input.
•Output Stream: If the direction of flow of bytes is opposite, i.e.
from main memory to device( display screen ) then this process is
called output.
Concepts in OOPS.

 Class - Basic runtime entity


 Object – instance of class of similar data type
 Inheritance- reusability
 Abstraction – act of representing essential features -ADT
 Encapsulation - Wrapping up data and functions into a single unit
 Polymorphism-ability to take more than one form
 Overloading-function, operator
Principles( or features) of object-oriented programming:

1. class and objects


2. Encapsulation
3. Data abstraction
4. Polymorphism
5. Inheritance
6. Dynamic binding
7. Message passing
Encapsulation: Wrapping of data and functions together as a single unit(called class) is known as encapsulation. By
default data is not accessible to outside and they are only accessible through the functions which are
wrapped in a class. prevention of data direct access by the program is called data hiding or information
hiding
class
class: It is a collection of data and member functions that manipulate data. The data components of class are
called data members and functions that manipulate the data are called member functions.
It can also called as blue print or prototype that defines the variables and functions common to
all objects of certain kind.
It is also known as user defined data type or ADT(abstract data type) A class
is declared by the keyword class.
Objects
An Object is an instance of a Class
Objects are basic runtime entities in an objects-oriented system. They may
represent a person, bank account a table of dta or any item thata the program has
to handle.
It is also use to represent user defined data such as vector, time , lists.
Data abstraction

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.

The principal advantages are :

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++ ||

•Arithmetic Operators- (+,-,*,/,%)


•Relational Operators –( <, >,<=, >=, ==) !=)
•Logical Operators - (&&, ||-or,!)
•Bitwise Operators
•Assignment Operators(=)
( a=a+b, a+=b
Operators in c++ contd.

1. Arithmetic operators are used to perform arithmetic operations on variables and


data. (+,-,*,/,%)
2. A relational operator is used to check the relationship between two operands
If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
( <, >,<=, >=, ==) !=)
Simple C++ program
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter the values of 'a' and ’b'";
cin>>a;
cin>>b;
cout<<“The values that you have entered are : "<<a<<" , "<<b;
return 0;
}
C++ program-Arithmetic operator

#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

and print some special characters


Structures in C++

Structure is a collection of variables of different data types under a single name.


 It is similar to a class
 The struct keyword defines a structure type followed by an identifier
In C++, a structure is a user-defined data type.
The structure creates a data type for grouping items of different data types under a single data
type Example
Syntax
Struct student
struct struct _name {
{ struct Members; int rno;
Char name[15];
} Float grade;
}
Creating Struct Instances
we have created a struct named student.
We can create a struct variable as follows:

Student s;

Accessing Struct Members

To access the struct members, we use the instance of the struct and the dot (.)

operator. For example, to access the member age of struct student:

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;

cout << "Enter the name: ";


cin >> s.name;

cout << "Enter the grade: ";


cin >> s.grade;

cout << "\n\nDisplay the student's information:\n";


cout << "\nThe Roll Number:\t" << s.roll_number << "\n";

cout << "Name: ";


puts(s.name);

cout << “grade: ";


cout << “Grade= ";

cout << grade<<endl;


}
return 0;
}
#include <iostream>
using namespace std;
struct student
{
int rno;
char name[10];
float grade;
};

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

You can pass a struct to a function as an argument.


This is done in the same way as passing a normal argument.
The struct variables can also be passed to a function
Nested Structures
A nested structure i is a structure within structure. One structure can be declared inside another
structure in the same way structure members are declared inside a structure.
struct addr // structure tag
{ Accessing Nested Structure
int houseno ; Member
char city[26] ;
char state[26] ;
}; worker.address.city
struct emp { // structure tag worker.address.houseno=123
int empno ;
char name[26] ;
struct addr address ;
float basic ;
};
struct emp worker ;
Array within a Structure

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.

You might also like