0% found this document useful (0 votes)
2 views17 pages

OOP Weekend Lecture 01 02

Object-Oriented Programming (OOP) originated in the 1960s with Simula and was further developed by Alan Kay in the 1970s with Smalltalk. OOP is a programming paradigm that uses objects to encapsulate data and functions, promoting code organization, reusability, and error reduction. The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction, which help model real-world entities in software development.

Uploaded by

ayesha.widemedia
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)
2 views17 pages

OOP Weekend Lecture 01 02

Object-Oriented Programming (OOP) originated in the 1960s with Simula and was further developed by Alan Kay in the 1970s with Smalltalk. OOP is a programming paradigm that uses objects to encapsulate data and functions, promoting code organization, reusability, and error reduction. The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction, which help model real-world entities in software development.

Uploaded by

ayesha.widemedia
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/ 17

OBJECT ORIENTED PROGRAMING

How & When OOP invented?


👉 1960s → OOP ideas started with Simula, the first language that has OOP concpets but still
used procedural programing elements, developed in Norway by Ole-Johan Dahl and
Kristen Nygaard.

👉 1970s → Alan Kay expanded these ideas and created Smalltalk, the first true/pure OOP
language. Alan Kay invented OOP.

👉 1980s & 1990s → OOP became popular with languages like C++, Java, and Python.

Simula SmartTalk
Used for Simulation purpose ( Traffic 🚦, Used for Software Development
Banking 🏦)

What is Object-Oriented Programming (OOP)?


OOP is a Method of designing programs using objects. Objects are real-world things.
Each object has 2 things:

👉 Properties (Data) → Example: A Car 🚗 has color, brand, speed

👉 Actions (Functions) → Example: A Car 🚗 can start, stop, accelerate

Example

💡 Think about a Mobile Phone 📱:

Object Properties (Data Members/ Varaibles) Actions (Functions)


Phone 📱 Model, Battery %, Brand Call(), Text(), Browse()

👉 In OOP, we create "objects" like real-world things that store both data and actions
inside them.
Why Do We Use OOP?
OOP helps us write better programs because:
✅ Code is Organized → Everything is grouped inside objects.
✅ Easy to Reuse → We can use the same code multiple times.
✅ Reduces Errors → Fixing problems is simpler.
✅ Models Real-World Things → We create software that works like real life.

💡 Example:
Imagine you're building an Online Shopping Website 🛒.
Instead of writing separate code for each customer, you can create a Customer object and reuse
it for thousands of users!

OOP vs Structured Programming


Before OOP, programmers used Structured Programming, where data and functions were
separate.

Structured Programming (Old Object-Oriented Programming


Feature
Way) (OOP)
Data &
Stored separately Combined inside objects
Functions
Code
Hard to reuse code Easy to reuse code
Reusability
Creating a list of customer details Creating a "Customer" class with all
Example
separately details inside
Best For Small programs Large & complex applications
The Four Pillars of OOP (With More Examples &
Diagrams!)
1. Encapsulation 🔒 (Hiding & Protecting Data)

Encapsulation 🔒 means hiding the details of an object and allowing access only in a
controlled way.

💡 Example # 01

A Bank Account 🏦:

👉 You can check your balance but can’t directly change it.

👉 You need authentication (password or PIN) to modify data.

Private Data: Balance, Account Number


Public Functions: Deposit, Withdraw, Check Balance

💡 Example # 02

A TV Remote 🎛️ → You press buttons, but you don’t see how it works inside.

💡 Example # 03

A Mobile App Login System → You enter your password, but the app doesn’t show how it
checks your password inside.

🔒 Encapsulation protects data so that it can't be changed without permission.


2. Inheritance (Reusing Features from Parent to Child)

👉 Inheritance means one class can inherit properties and behaviors from another class.

💡 Example # 01

A Car 🚗 and a Bike 🏍 both belong to the Vehicle category and share common properties like
Color, Price, Model, Brand.

Vehicle (Properties = Color, Price, Model, Brand)

├── Car 🚗
├── Bike 🏍

🚗 Car & Bike 🏍 inherit properties from Vehicle (just like children inherit traits from parents).

💡 Example # 02

A Cat and a Dog both belongs to the Animal Category and share common properties like Eat,
Sleep.

Animal (Properties = Eat, Sleep)

├── Cat
├── Dog

💡 Example # 03

A Teacher 🎓 and a Student both belongs to the Person category and share common properties
like Name, Gender, Weight, Qualification.

Person (Properties = Name, Gender, Weight, Qualification)

├── Teacher 🎓
├── Student
3. Polymorphism (One Action, Many Forms)

👉 Polymorphism means a single action (function) can behave differently for different
objects.

💡 Example # 01

Imagine pressing a "Play" button ▶️ on different devices:

👉 On a TV, it plays a video 📺.

👉 On a Music Player, it plays a song 🎵.

👉 On a Game Console, it starts a game 🎮.

Play
├── TV → "Playing a Video"
├── Music Player → "Playing a Song"
├── Game Console → "Starting a Game"

🔄 Same function (Play), but different outputs!

💡 Example # 02

A person waves 👋 → To a friend, it’s a greeting. To a waiter, it means "Bring the bill!".

🔄 Same function (Wave), but different outputs!

💡 Example # 03

The word "Run" 🏃 → A human runs, a program runs, and a car engine runs!

🔄 Same function (Run), but different outputs!


4. Abstraction (Hiding Details & Showing What’s Important)

👉 Abstraction means hiding the internal workings of something and only showing the
necessary parts.

💡 Example # 01

When you drive a car 🚘, you press the accelerator, but you don’t need to know how the
engine works!

Driver → (Uses Steering, Accelerator, Brakes)


Engine Details (Hidden!)

🚘 Abstraction hides complex details, just like how a driver only sees the important controls.

💡 Example # 02

ATM Machine 🏧 → You enter a PIN and withdraw cash, but you don’t see how the bank
processes the request.

💡 Example # 03

Mobile App Interface 📱 → You tap a button, and an action happens, but you don’t see the
code behind it.

Where is OOP Used in the Real World?


💻 Software Development → OOP is used to make desktop and web applications.
🎮 Game Development → Games have multiple objects like "Players", "Enemies", "Weapons".
📱 Mobile Apps → Apps like WhatsApp, Instagram, and Facebook are built using OOP.
🚗 Car Manufacturing → OOP is used in software that controls modern cars (Tesla, BMW).
️ Artificial Intelligence → AI systems use OOP for robots, chatbots, and smart assistants.
Conclusion - Why OOP is Amazing!
👉 Encapsulation → Protects data (like a bank account).
👉 Inheritance → Reuses code (like a child inheriting features from parents).
👉 Polymorphism → One function, different behaviors (like a "Play" button).
👉 Abstraction → Hides complexity (like driving a car without knowing engine details).

Quick Questions for You!


1. Think of an example of Encapsulation in daily life.
2. Where have you seen Polymorphism used in the real world?
3. Choose and Object, write down its 3 properties and 3 actions.
4. Suppose mobile, tablet both belongs to electronics category, write down their 3
common properties and 2 unique categories.
5. Give me a real life example of Polymorphism.

What is a Structure in C++?


📌 A Structure in C++ is a collection of variables (data members) grouped together under one name.

📌 Structure is written in C++ with keyword struct.

📌 All the variables inside the structure are public.

How to write a structure?


We have a Car and its properties are name, color and speed. Write down its structure in C++.

struct Car
{
string name;
string color;
int speed;
};
Can we store values to the variables directly inside the
structure?
struct Car
{
string name = “KIA”;
string color = “Black”;
int speed = 220 ;
};
ANSWER: ERROR! = DATA MEMBER INITIALIZER IS NOT ALLOWED.

How we can store values to the variables inside the


structure?
There are different ways to do that:

👉 Assign Values using DIRECT INTIALIZATION

👉 Assign Values using DOT OPERATOR

👉 Assign Values using FUNCTION


 Function inside structure
 Function outside structure
Question
We have a Car and its properties are name, color and speed. Write
down its structure in C++. And store care name as KIA, Color as
Black and speed as 220. Display the data on console.
DIRECT INTIALIZATION DOT OPERATOR
#include <iostream> #include <iostream>
using namespace std; using namespace std;
struct Car struct Car
{ {
string name; string name;
string color; string color;
int speed; int speed;
}; };
int main() int main()
{ {
// Create Object of Class and // Create Object of Class and
assign values assign values
Car c = {"KIA", "Black" , 220}; Car c;
c.name="KIA";
//Display the Data c.color="Black";
cout<<c.name<<endl; c.speed=220;
cout<<c.color<<endl;
cout<<c.speed<<endl; //Display the Data
return 0; cout<<c.name<<endl;
} cout<<c.color<<endl;
cout<<c.speed<<endl;
return 0;
}
Output: Output:

KIA KIA
Black Black
220 220
=== Code Execution Successful === === Code Execution Successful ===
Question
We have a Car and its properties are name, color and speed. Write
down its structure in C++. And store care name as KIA, Color as
Black and speed as 220. Display the data on console.
FUNCTION INSIDE STRUCTURE FUNCTION OUTSIDE STRUCTURE
#include <iostream> #include <iostream>
using namespace std; using namespace std;
struct Car struct Car
{ {
string name; string name;
string color; string color;
int speed; int speed;
void setvalue()
{ };
name="KIA"; void setvalue(Car c)
color="Black"; {
speed=220; c.name="KIA";
c.color="Black";
cout<<name<<endl; c.speed=220;
cout<<color<<endl;
cout<<speed<<endl; cout<<c.name<<endl;
} cout<<c.color<<endl;
}; cout<<c.speed<<endl;
}
int main() int main()
{ {
// Create Object of Class and assign values // Create Object of Class and assign
values
Car c; Car c;

// Call the function setvalue() // Call the function setvalue()


c.setvalue(); setvalue(c);
return 0; return 0;
} }

Output: Output:

KIA KIA
Black Black
220 220
=== Code Execution Successful === === Code Execution Successful ===
Do It Your Self !
1: Now everyone change the struct keyword to class keyword and see what happens!

What is a Class in C++?


📌 A Class in C++ is a advanced version of structure, collection of variables (data members)
grouped together under one name.

📌 Class is written in C++ with keyword class.

📌 All the variables inside the structure are private.

How a function can access the variable inside the class?


Answer: No, because class is by default private so it keep everything inside it as private.

Test it!
#include <iostream> Output
using namespace std;
class Car Error!
{
string name;
string color;
int speed;
};
int main()
{
// Create Object of Class and assign
values
Car c = {"KIA", "Black" , 220};

//Display the Data


cout<<c.name<<endl;
cout<<c.color<<endl;
cout<<c.speed<<endl;
return 0;

}
How to Resolve it?
Question
We have a Car and its properties are name, color and speed. Write
down its class in C++. And store care name as KIA, Color as Black
and speed as 220. Display the data on console.
DIRECT INTIALIZATION FUNCTION INSIDE CLASS
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class Car class Car
{ {
public: private:
string name; string name;
string color; string color;
int speed; int speed;
}; public:
int main() void setvalue()
{ {
// Create Object of Class and assign name="KIA";
values color="Black";
Car c = {"KIA", "Black" , 220}; speed=220;

//Display the Data cout<<name<<endl;


cout<<c.name<<endl; cout<<color<<endl;
cout<<c.color<<endl; cout<<speed<<endl;
cout<<c.speed<<endl; }
return 0; };
}
int main()
{
// Create Object of Class and assign values

Car c;

// Call the function setvalue()


c.setvalue();
return 0;
}
Output: Output:

KIA KIA
Black Black
220 220
=== Code Execution Successful === === Code Execution Successful ===
Point to be followed!
If you want that the functions can access the data of class then always do this:

 Make all the variables Private


 Make all the Functions Public

Class VS Structure
Class Structure
Created using keyword class Created using keyword struct
Access is by default private Access is by default public
Secured No
Inheritance can done No
Encapsulation (Data Hiding using private) No
Abstraction No
PRACTICE TASKS

Task 1: Create a structure Employee with the following attributes Employee Name, Employee
ID and Salary. Assign the values using function names set(). The set() function must be outside
the structure.

✅ Expected Output Example:

The Name of the Employee is: John

The ID is: 1001

The Salary is: 50000

-----------------------------------------------------------------------------------------------------------------

Task 2: Create a structure Book with the following attributes:


✔ Book Title
✔ Author Name
✔ Book ID
✔ Price

🔹 Requirements:
1️. Declare a structure for Book.
2️. Assign values for the attributes using Dot Operator.
3️. Display book details.

✅ Expected Output Example:

The Title is: C++ Programming


The Author is: Bjarne Stroustrup
The ID is: 101
The Price is: 39.99 $
-------------------------------------------------------------------------------------------------------------------

Task 3: Find out the error from the given code snippet.

struct Student {

string name

int rollNo;

};
SOLUTION
TASK 1

Solution

#include <iostream>

using namespace std;

struct Employee

string employeeName;

int employeeID;

int salary;

};

void set(Employee e)

e.employeeName="John";

e.employeeID=1001;

e.salary=50000;

cout<<"The Name of the Employee is: "<<e.employeeName<<endl;

cout<<"The ID is: "<<e.employeeID<<endl;

cout<<"The Salary is: "<<e.salary<<endl;

int main()

// Create Object of Class and assign values


Employee e ;

// Call the function setvalue()

set(e);

return 0;

TASK 2
#include <iostream>
using namespace std;
struct Book
{

string booktitle;
string authname;
int bookid;
float price;

};
int main()
{
// Create Object of Class and assign values
Book b ;
b.booktitle="C++ Programming";
b.authname="Bjarne Striustrap";
b.bookid=101;
b.price=3.99;

//Output
cout<<"The Tile is: "<<b.booktitle<<endl;
cout<<"The Author is: "<<b.authname<<endl;
cout<<"The ID is: "<<b.bookid<<endl;
cout<<"The Price is: "<<b.price<<" $"<<endl;
return 0;
}

TASK 3

Error

Semicolon missing after string name.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy