Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views68 pages

Oop Module 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 68

Course: Object Oriented Programming

Module 1: Introduction to OOPs Concept

Department of Computer Science & Engineering


Module Reference

Chapter 1: The Big Picture


Chapter 2: C++ Programming Basics
Book: Object Oriented Programming in C++, by Robert Lafore (Fourth Edition)
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 1: INTRODUCTION TO OOPS &
BASIC C++ PROGRAM CONSTRUCTION

Introduction: Basic concepts of Oops, benefits of oops, application of Oops, Difference between
Procedural Oriented programming and object-oriented programming, c++ Data types, Basic
program construction , , Input output statements: cin and cout, Comments, escape sequence in
C++ ,Operators: types of operators in c++ , library functions,
Structures: A Simple Structure, Defining and accessing the structure, Nested.
What is programming language
A programming language is a set of instructions used to communicate with a computer. It allows
developers to write code that the computer can understand and execute, resulting in a specific
action or output. Popular programming languages include Python, Java, JavaScript, and C++.
OOPs contd.

Oops is methodology or paradigm to design a program using classes and


objects
It simplifies the program development
Allows us to decompose a problem into a number of entities called objects and
then builds data and functions.
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
Why OOPs contd.

 Code Reusability via inheritance.


 Efficient Development
 Efficient Handling
 Efficient Accessibility
 Easy to debug
 Easy to bind Data & Functions
Concepts in OOPS.

 Class - User defined Basic runtime entity


 Object – instance of class of similar data type
 Inheritance- reusability
 Abstraction – act of representing essential features
 Encapsulation - Wrapping up data and functions into a single unit
 Polymorphism-ability to take more than one form (eg-Overloading, overriding)
Benefits of oops
OOPs 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.
Benefits of Object-Oriented
Programming (OOP)
Here are some benefits of Object-Oriented Programming (OOP):
Improved code organization and structure
Easier maintenance and modification of code
Better support for inheritance and polymorphism
Improved modularity and reusability of code
Enhanced scalability and flexibility of software systems
Better support for object-based design patterns
Improved readability and understandability of code
Better support for software development methodologies such as Agile and Scrum
Improved collaboration and communication among developers
Improved ability to handle complex systems and applications.
C++
C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at
Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions
of UNIX.
C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming
language that supports procedural, object-oriented, and generic programming.
C++ is regarded as a middle-level language, as it comprises a combination of both high-level and
low-level language features.
C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey,
as an enhancement to the C language and originally named C with Classes but later it was
renamed C++ in 1983.
Procedural oriented programming Object oriented programming
Definition It is a programming language that is derived from structure programming Object-oriented programming is a computer programming design
and based upon the concept of calling procedures. It follows a step-by-step philosophy or methodology that organizes/ models software design around
approach in order to break down a task into a set of variables and routines data or objects rather than functions and logic.
via a sequence of instructions.
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.
Approach It follows a top-down approach. It follows a bottom-up approach.
Data movement In procedural programming, data moves freely within the system from one In OOP, objects can move and communicate with each other via member
function to another. functions.
Orientation It is structure/procedure-oriented. It is object-oriented.
Access modifiers There are no access modifiers in procedural programming. The access modifiers in OOP are named as private, public, and protected.
Inheritance Procedural programming does not have the concept of inheritance. There is a feature of inheritance in object-oriented programming.
Code reusability There is no code reusability present in procedural programming. It offers code reusability by using the feature of inheritance.
Overloading Overloading is not possible in procedural programming. In OOP, there is a concept of function overloading and operator
overloading.
Importance It gives importance to functions over data. It gives importance to data over functions.
Virtual class In procedural programming, there are no virtual classes. In OOP, there is an appearance of virtual classes in inheritance.
Complex problems It is not appropriate for complex problems. It is appropriate for complex problems.
Data hiding There is not any proper way for data hiding. There is a possibility of data hiding.
Program division In Procedural programming, a program is divided into small programs that In OOP, a program is divided into small parts that are referred to as objects.
are referred to as functions.
Examples Examples of Procedural programming include C, Fortran, Pascal, and VB. The examples of object-oriented programming are -.NET, C#, Python,
Java, VB.NET, and C++.
C++ data types
Input and output statements in C++

syntax
Cin>> variable name;

Cout<< variable name;

example
Cin>> a;

Cout<< a;
Example 1
// C++ program to illustrate the use of cout object
#include <iostream>
using namespace std;
int main()
{
// Print standard output
// on the screen
cout << “My First Program";
return 0;
}
Example 2
// C++ program to illustrate printing of more than one statement in a single cout statement
#include <iostream>
using namespace std;
int main()
{
string name = "Akshay";
int age = 18;
// Print multiple variable on screen using cout
cout << "Name : " << name << endl << "Age : " << age << endl;
return 0;
}
Example 3
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: "; //character output with insertion operator
// take integer input
cin >> num; //character input with extraction operator
cout << "You entered: " << num;
return 0;
}
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;
C++ Comments
Why Comments are used in C++?
Comments in C++ are used to summarize an algorithm, identify a variable’s purpose, or clarify a
code segment that appears unclear. Comments are also used for:
Comments are used for easier debugging.
It makes a program more readable and gives an overall description of the code.
Comments are helpful in skipping the execution of some parts of the code.
Every time a program or code is reused after long periods of time, the comment recaps all the
information of the code quickly.
Library Functions In C++

Library functions which are also called as “built-in” functions are the functions that are already
available and implemented in C++.
We can directly call these functions in our program as per our requirements. Library functions in
C++ are declared and defined in special files called “Header Files” which we can reference in our
C++ programs using the “include” directive.

<iostream>, <cstring>, <cmath>, <cstdlib> headers that we have used from time to time.
Structure Of C++ Program
C++ Operators

Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
Operators in C++ can be classified into 6 types:
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Bitwise Operators in C - Examples and Applications | Hero Vired
Other Operators
Operators in C++
C++ Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,
Unary operators
Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and -- respectively.
++ increases the value of the operand by 1
-- decreases it by 1
C++ Assignment Operators

In C++, assignment operators are used to assign values to variables.


C++ Relational Operators
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.
Note: Relational operators are used in decision-making and loops.
C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the expression
is true, it returns 1 whereas if the expression is false, it returns 0.
Explanation of logical operator program
(3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
(3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false).
(3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
(3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
(3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
(3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
!(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
!(5 == 5) evaluates to 0 because the operand (5 == 5) is 1 (true).
C++ Bitwise Operators

In C++, bitwise operators are used to perform operations on individual bits. They can only be
used alongside char and int data types.
Other C++ Operators

Here's a list of some other common operators available in C++.


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
Syntax Example
struct struct _name
Struct student
{ 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;
Example 1
Example 2
Example 3
Example 4
Brief exaplanation
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;
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
struct addrinside a structure.
// structure tag
{ Accessing Nested
int houseno ; Structure Member
char city[26] ;
char state[26] ;
}; worker.address.city
struct emp { // structure tag worker.address.houseno=12
int empno ; 3
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 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++)
{
cout << "Enter the rno ";

You might also like