
Object Oriented Programming
Prateek Narang
Topics
-
OOPS Introduction
-
Classes & Objects Functions
-
Data Members
-
Methods
-
Access Modifers
-
Special Functions



Procedural Programming
Procedural programming (OOP) is a programming paradigm based on the concept of the procedure call.
We have seen many examples of building up a program using bunch of functions.
Data is separate from functions, instead the data is passed to functions to operate upon.

Limitations of Procedural Programming
>> Larger programs become difficult to understand, we have trace the entire data flow across multiple functions.
>> If a data structure changes, then we have to modify a lot of functions to which the data structure has been passed.
>> Larger code becomes fragile, difficult to extend and maintain.
OOPS
Programming paradigm


Object Oriented Programming
Object-oriented programming (OOP) is a computer programming paradigm that organizes software design around data, or objects, rather than functions and logic.
An object can be defined as a data field that has unique attributes and behavior.
This approach to programming is well-suited for programs that are large, complex and actively updated or maintained.

Actions
PlayVideo
SignIn
UploadVideo
Like
Comment
Subscribe
...
There are so many actions that a user can do, and with each action there is certain code that needs to be triggered.

Each action requires a function to be executed at code level.
signIn(){
...
}
uploadVideo(){
...
}
recommendVideo(){
...
}
likeVideo(){
...
}
playVideo(){
...
}
Functions


Function
A function is a block of instructions which only execute when it is called.
void play_music(){
cout<<"Playing Music";
}
int main(){
play_music(); //call the function
return 0;
}

Function
- Declaration - A function declaration tells the compiler about a function's name, return type, and parameters.
- Definition - A function definition provides the actual body of the function.
// Declaration + Definition = Function
void play_music(){
cout << "Enter your favoring song";
int song;
cin >> song;
cout<<"Playing song "<<song;
}

Function Call
- To call a function, write the function's name followed by two parentheses () and a semicolon ;
// Declaration + Definition = Function
void play_music(){
cout << "Enter your favoring song";
int song;
cin >> song;
cout<<"Playing song "<<song;
}
play_music();

Passing Data through 'Parameters'
void play_music(int song_id){
cout<<"Playing song "<<song_id;
}
int main(){
play_music(5);
return 0;
}
You can pass data, known as parameters, into a function. Parameters act as variables inside the function.You can add as many parameters as you want, just separate them with a comma

Default Values of Parameters
void play_music(int song1=1,int song2=2,int song3=3){
cout<<"Playing song "<<song1<<song2<<song3;
}
int main(){
play_music(5);
return 0;
}
You can also use a default parameter value, by using the equals sign (=). If we call the function without an argument, it uses the default value.

Return Values
Functions can also return some data back to the place from where they are called using a 'return' keyword and specifying a return type.
The void keyword, used in the below examples, indicates that the function should not return a value.
void play_music(int song1=1,int song2=2,int song3=3){
cout<<"Playing song "<<song1<<song2<<song3;
}

Return Values
If you want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the function.
bool login(String username){
// some logic
return true;
}
int countFriends(){
// some logic
return 5;
}
void play_music(int song1=1,int song2=2,int song3=3){
cout<<"Playing song "<<song1<<song2<<song3;
}
When two or more functions, have the same name but different parameters it is known as function overloading.

Function Overloading
int area(int side){
return side*side;
}
int area(int l,int r){
return l*r;
}
Call Stack

A call stack is a stack data structure that stores information about the active functions. The main reason for having a call stack is to keep track of the point to which each active function should return control when it completes executing.

Call Stack
int prime(){
...
}
int fact(){
...
}
int main(){
...
}
[Topics ] Object Oriented Programming
By Prateek Narang
[Topics ] Object Oriented Programming
- 23