OOP Weekend Lecture 01 02
OOP Weekend Lecture 01 02
👉 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 🏦)
Example
👉 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!
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.
💡 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.
👉 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.
├── 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.
├── Cat
├── Dog
💡 Example # 03
A Teacher 🎓 and a Student both belongs to the Person category and share common properties
like 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
Play
├── TV → "Playing a Video"
├── Music Player → "Playing a Song"
├── Game Console → "Starting a Game"
💡 Example # 02
A person waves 👋 → To a friend, it’s a greeting. To a waiter, it means "Bring the bill!".
💡 Example # 03
The word "Run" 🏃 → A human runs, a program runs, and a car engine runs!
👉 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!
🚘 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.
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.
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;
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!
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};
}
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;
Car c;
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:
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.
-----------------------------------------------------------------------------------------------------------------
🔹 Requirements:
1️. Declare a structure for Book.
2️. Assign values for the attributes using Dot Operator.
3️. Display book details.
Task 3: Find out the error from the given code snippet.
struct Student {
string name
int rollNo;
};
SOLUTION
TASK 1
Solution
#include <iostream>
struct Employee
string employeeName;
int employeeID;
int salary;
};
void set(Employee e)
e.employeeName="John";
e.employeeID=1001;
e.salary=50000;
int main()
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