0% found this document useful (0 votes)
10 views

Lecture 1 OOP

this is c++ opss lec 1

Uploaded by

hi070125f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 1 OOP

this is c++ opss lec 1

Uploaded by

hi070125f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Object Oriented Programming

Lecture 01

Engr. Dr. Ghulam Fareed Laghari

Department of Computer Science


BIMS, Rawalpindi
2

CONTENT
Programming

Computer Program

Programming Languages

Programming Paradigms

Object Oriented Programming (OOP)

C++
3

Programming
Process of creating instructions that a computer can execute.

Instructions are written in a programming language, i.e., a formal language


designed to communicate with computers.

Goal of programming is to solve problems by writing code that manipulates data,


performs calculations, or automates tasks.

Briefly, Programming is the systematic process of designing and writing


instructions, namely code, that a computer can interpret and execute to perform
specific tasks.
4

Programming
Analogy compares programming to giving step-by-step instructions to someone who lacks the ability to
reason on their own.

Example: Imagine instructing a less-than-competent friend to build a Lego set without the provided
instructional manual. Your friend is incapable of doing anything on his own, so you must give him
precise, detailed instructions for every step. If even one piece is placed incorrectly, the entire set could be
ruined.

Now, replace your friend with a computer—that’s how programming works.

Computers are not inherently smart; they only do what we explicitly tell them to do through code.

Highlights the need for exactness in programming and how computers rely entirely on clear,
unambiguous instructions.
5

Programming
Typically, the steps and methodologies involved in creating, refining, and managing
software through programming include:
Problem Analysis: Understanding the problem to be solved.
Design: Planning how the program will solve the problem, often using algorithms or
flowcharts.
Coding: Writing the actual code in a specific programming language like Python,
Java, or C++.
Testing and Debugging: Ensuring the program works as intended and fixing any
errors.
Maintenance: Updating and improving the code over time as needed.
6

Programming
Computers only understand machine code, which is a series of 1s and 0s that they can process
and interpret.

Example: Imagine trying to communicate with a friend who only speaks Spanish, while you
speak English—it would be impossible to communicate directly. To bridge the gap, you'd have
to translate your instructions from English into Spanish.

Similarly, when programming, you need to translate your high-level instructions into machine
code (binary) for the computer to understand.

However, manually converting every programming instruction into binary would be impractical,
which is why we use programming languages that automatically handle this translation for us.
7

Computer Program
Computer Program is a structured set of instructions written in a programming language that directs a
computer to perform specific tasks, for instance, calculations, data processing, or controlling hardware
devices.

Programming is required to:

Create a computer program because it involves designing and writing these instructions in a
structured way that the computer can understand.

Translate human ideas/logic and problem-solving methods into machine-readable code that the
computer can interpret and execute.

Without programming, there would be no way to communicate with the computer or instruct it to
perform tasks.
8

Programming Languages
 Programming can be executed utilizing a programming language.

 Three primary types of programming languages are:

Machine Languages: The most basic level of programming language, consisting of binary code (0s and 1s)
that can be directly executed by a computer's hardware.

Assembly Languages: A low-level programming language that uses symbolic representations (mnemonics)
of machine instructions, allowing for easier programming compared to machine languages. Assembly
languages are translated into machine code by an assembler.

High-Level Languages: Programming languages that are more abstract and human-readable, such as Python,
Java, or C++. Designed to simplify programming tasks and require a compiler or interpreter to convert the
high-level code into machine code that the computer can execute.
9

Programming Paradigms
Programming paradigms are fundamental approaches to programming that influence how developers
structure and organize code. They provide different methodologies for problem-solving and program design.

Common paradigms include:

Imperative Programming: Focuses on explicit commands for the computer to perform, describing how
to achieve a specific task through a sequence of statements.

Procedural Programming: A subtype of imperative programming that organizes code into procedures
or routines, facilitating code reuse through functions. In this paradigm, a program consists of a sequence
of statements that manipulate variables. During execution, the program continuously alters the contents
of the computer's memory, enabling dynamic data management and control flow.
10

Programming Paradigms
Declarative Programming: Emphasizes expressing what the program should accomplish without
specifying how to achieve it. Examples: Structured Query Language (SQL) and functional
programming languages.

Functional Programming: Treats computation as the evaluation of mathematical functions,


emphasizing immutability, meaning that once a data structure is created, it cannot be changed. Instead of
modifying existing data, new data structures are created as needed, leading to code that is easier to test,
debug, and maintain.

Object-Oriented Programming (OOP): Centers around objects, which combine data and behavior,
promoting code reusability and organization through encapsulation, inheritance, and polymorphism.
Examples include: Java, C++, Python, C#, Ruby, PHP etc.

 Each paradigm has its strengths and weaknesses and can be chosen based on the specific requirements of a project.
11

Object Oriented Programming (OOP)


 A programming paradigm that revolves around the concept of objects, which are instances of classes. These objects
encapsulate:

Data (attributes or properties) and

Behavior (methods or functions)

 Allowing for more structured and organized code.

 Four pillars of OOP are:

Encapsulation

Inheritance

Polymorphism

Abstraction
12

Object Oriented Programming (OOP)


1. Encapsulation:
Definition: Encapsulation is the bundling of data and the methods that operate on that data
within a single unit or object.

Objective: Restricts direct access to some of an object's components, promoting data hiding
and protecting the integrity of the data. This means that internal object details are hidden
from the outside, and interactions with the object occur through well-defined interfaces
(methods).

Example: A class BankAccount may have private attributes like balance and public
methods like deposit() and withdraw(), controlling how the balance is modified.
13

Object Oriented Programming (OOP)


2. Inheritance
Definition: Inheritance allows one class (subclass or derived class) to inherit
attributes and methods from another class (superclass or base class).
Objective: It promotes code reuse and establishes a hierarchical relationship
between classes. Subclasses can extend or override behaviors defined in
superclasses, facilitating the creation of specialized classes.
Example: A class Animal might have a method speak(). A subclass Dog can
inherit this method and provide its own implementation, such as barking.
14

Object Oriented Programming (OOP)


3. Polymorphism
Definition: Allows objects of different classes to be treated as objects of a common
superclass, enabling a single interface to represent different underlying forms (data types).

Purpose: It enhances flexibility and allows for dynamic method resolution. This means that
the same method name can behave differently based on the object that calls it.

Example: If both Dog and Cat classes inherit from the Animal class, they can both
implement the speak() method. When the speak() method is called on an Animal reference
that points to either a Dog or Cat object, the appropriate method for the specific object is
invoked.
15

Object Oriented Programming (OOP)


4. Abstraction
Definition: Simplifies complex systems by modeling classes based on the
essential characteristics of an object while hiding unnecessary details.
Objective: It reduces complexity and increases efficiency in code
management by focusing on relevant properties and behaviors.
Example: An interface Vehicle might define methods like start() and stop(),
while the specific implementations in classes like Car and Bicycle provide the
details.
16

Object Oriented Programming (OOP)


Advantages
Code Reusability: By using inheritance and polymorphism, existing code can be reused,
reducing redundancy (being unnecessary or excessive due to duplication or repetition).

Improved Maintenance: Encapsulation allows changes to be made internally without


affecting external code, making it easier to maintain and update software.

Enhanced Collaboration: OOP promotes teamwork, as different developers can work on


different objects or classes simultaneously without conflicts.

Four pillars collectively provide a framework for designing and implementing object-
oriented systems, making code more manageable, reusable, and easier to understand.
17

C++
Software Development Activities
Source Program
Editing

Compiling
Compile
Linking with precompiled files:
Library routines
 Library modules Edit Link
 Object files
Other object files
Loading
Think Load
Executing

Viewing the behavior of the program


Execute
18

C++
Example: Code execution

Source file, namely, Test.cpp


Compile
Test.cpp Test.obj

Link
Test.obj + Libraries Test.exe
19

C++
Integrated Development Environments (IDEs)
Definition: Comprehensive software applications that facilitate software development by integrating various
tools into a single interface.

Fundamental Characteristics:

 Code Editor: A text editor with syntax highlighting and code completion to assist in writing code.

 Compiler/Interpreter: Tools that translate code into executable programs or scripts.

 Debugger: A utility for testing and troubleshooting code, allowing developers to identify and fix errors.

 Build Automation: Tools that automate the process of compiling and linking code, streamlining project
builds.
20

C++
Integrated Development Environments (IDEs)
Benefits:

 Enhanced Productivity: Consolidates/combines essential tools, allowing developers to work


more efficiently.

 Improved Code Management: Facilitates organization and navigation of large codebases.

 Streamlined Workflow: Reduces context switching by providing all necessary tools in one
environment.

Examples: Microsoft Visual Studio, Borland etc.

Encompasses all the attributes for developing software, e.g., Editor, Compiler, Linker, Loader,
Debugger, Viewer etc.
21

C++
Download and install Microsoft Visual Studio 2017/2019

https://visualstudio.microsoft.com/downloads/
22

C++
Beginning with your first C++ Code
23

C++
 Code Explanation

1. #include <iostream>: Includes the Input/Output Stream library, which is necessary for using cout, cin, and other I/O
operations.

2. using namespace std;: Allows the program to use the standard namespace, which includes features like cout and endl
without needing to prefix them with std::.

3. void main(): Defines the main function, which is the entry point of a C++ program. Use int main(), if the main function
should return an integer.

4. { and }: These curly braces define the beginning and end of the main function's body.

5. cout << "Hello World" << endl;: This line outputs the string "Hello World" to the console. cout is used for output, << is the
stream insertion operator, and endl not only moves the output cursor to the next line but also ensures that the output is
displayed immediately.
THANK YOU

24

You might also like