Skip to content

Commit

Permalink
Topics on oops
Browse files Browse the repository at this point in the history
  • Loading branch information
Isha307 committed Aug 20, 2023
1 parent 99b599d commit 0af0ecf
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}

]
}
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
42 changes: 42 additions & 0 deletions OOPS/Polymorphism/FriendClass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
A friend class can access private and protected members of other classes in which it is declared as a friend.
It is sometimes useful to allow a particular class to access private and protected members of other classes.

In CPP:

`
class GFG {
private:
int private_variable;
protected:
int protected_variable;
public:
GFG(){
private_variable = 10;
protected_variable = 99;
}

friend class F;
};

// Here, class F is declared as a
// friend inside class GFG. Therefore,
// F is a friend of class GFG. Class F
// can access the private members of
// class GFG.
class F {
public:
void display(GFG& t)
{
cout << "The value of Private Variable = "
<< t.private_variable << endl;
cout << "The value of Protected Variable = "
<< t.protected_variable;
}
};
`

In JAVA:
The concept of friends is not in Java.

In Python:
There is no privacy in Python. Everyone can access anything anyway. So there's no need for a friend function sort of concept.
108 changes: 108 additions & 0 deletions OOPS/Polymorphism/FunctionOverriding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
In CPP:

`
class Parent{
access_modifier:
// overridden function
return_type name_of_the_function(){}
};

}

class child : public Parent {

access_modifier:
// overriding function
return_type name_of_the_function(){}
};

}

class Parent {
public:
void GeeksforGeeks_Print(){
cout << "Base Function" << endl;
}
};

class Child : public Parent {
public:
void GeeksforGeeks_Print(){
cout << "Derived Function" << endl;
// call of overridden function
Parent::GeeksforGeeks_Print();
}
};

int main(){
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
}
`

In java:

`
class Parent {
void show() { System.out.println("Parent's show()"); }
}

// Inherited class
class Child extends Parent {
@Override void show(){
System.out.println("Child's show()");
}
}

// Driver class
class Main {
public static void main(String[] args)
{
// If a Parent type reference refers
// to a Parent object, then Parent's
// show is called
Parent obj1 = new Parent();
obj1.show();

// If a Parent type reference refers
// to a Child object Child's show()
// is called. This is called RUN TIME
// POLYMORPHISM.
Parent obj2 = new Child();
obj2.show();
}
}
`

In Python:

`
class Parent():

# Constructor
def __init__(self):
self.value = "Inside Parent"

# Parent's show method
def show(self):
print(self.value)

# Defining child class
class Child(Parent):

# Constructor
def __init__(self):
self.value = "Inside Child"

# Child's show method
def show(self):
print(self.value)


# Driver's code
obj1 = Parent()
obj2 = Child()

obj1.show()
obj2.show()
`
109 changes: 109 additions & 0 deletions OOPS/Polymorphism/MethodOverloading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
1) Method Overloading: changing no. of arguments

In java:
`
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
`


In Python:

Python does not support method overloading by default.
But there are different ways to achieve method overloading in Python.

`
from multipledispatch import dispatch

# passing one parameter


@dispatch(int, int)
def product(first, second):
result = first*second
print(result)

# passing two parameters


@dispatch(int, int, int)
def product(first, second, third):
result = first * second * third
print(result)

# you can also pass data type of any value as per requirement


@dispatch(float, float, float)
def product(first, second, third):
result = first * second * third
print(result)


# calling product method with 2 arguments
product(2, 3) # this will give output of 6

# calling product method with 3 arguments but all int
product(2, 3, 2) # this will give output of 12

# calling product method with 3 arguments but all float
product(2.2, 3.4, 2.3) # this will give output of 17.985999999999997

`
In Backend, Dispatcher creates an object which stores different implementation and on runtime, it selects the appropriate method as the type and number of parameters passed.

In Cpp:

`
void add(int a, int b){
cout << "sum = " << (a + b);
}
void add(double a, double b){
cout << endl << "sum = " << (a + b);
}
int main(){
add(10, 2);
add(5.3, 6.2);
}

`

2) Method Overloading: changing data type of arguments

In java:

`class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
`

In Cpp:
`
void print(int i) {
cout<< i << endl;
}
void print(double f) {
cout << f << endl;
}
void print(char const *c) {
cout << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
}
`
23 changes: 23 additions & 0 deletions OOPS/Polymorphism/Polymorphism.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

Types of Polymorphism
=> Compile-time Polymorphism
=> Runtime Polymorphism

1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.

A. Function Overloading
Functions can be overloaded by changing the number of arguments or/and changing the type of arguments

B. Operator Overloading

2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding.
Late binding and dynamic polymorphism are other names for runtime polymorphism.

A. Function Overriding

Function Overriding occurs when a derived class has a definition for one of the member functions of the base class.
That base function is said to be overridden.

Loading

0 comments on commit 0af0ecf

Please sign in to comment.