Procedural programming vs oop
- Procedural programming deals with writing procedures (functions) that perform operations on data
- Languages meant for procedural programming - FORTRAN, COBOL, C
- OOP deal with modelling of real-world entities within the program (through classes). Classes encapsulate both data and functionalties
- Laguages - C++, Objective-C
Why OOP?
- Helps keep your code DRY
- Clear structure and increased reusability
4 Principles of OOP
- Encapsualtion
- Abstraction Hiding
- Inheritance
- Polymorphism
Encapsulation
The idea of bundling data (attributes) and operations on the data (methods) into a single unit (class)
Abstraction
Hiding away the implementation details and showing the users only the essential attributes the real-world
Inheritance
Allows for a new class (child) to inherit the attributes and methods from a preexisting class (parent)
Polymorphism
Default class methods (CPP)
- Default Constructor
- Object initialisation
- Initialises object’s attributes
0
for numerical typesnull
for pointers
- Object initialisation
- Destructor
- Called when an object is to be destroyed (explicitly or out of scope)
- Release the resources that were held by object
- Copy Constructor
- Used to create a new object as a copy of an existing object
- Copy (overloaded) Assignment Operator
- Used to assign values of one object to other
- For the = operator
- Move constructor and move assignment operator
- C++ 11 & later
- Overloads
&&
- Efficient transfer of resources btw objects
Default class methods (Python)
__init__
- constructor__str__
- - returns human readable string representation of the object__del__
- destructor, not guarenteed to be executed immediately__eq__
and__ne__
- compare objects for equality (==) and inequality (!=)__lt__
,__le__
,__gt__
,__ge__
- methods for <, <=, >, >=__hash__
- returns object hash - when using objects as dictionary keys__repr__
- returns official string representation of an object__call__
- allows an object to be called like a function__enter__
and__exit__
- used for implementing context managers using thewith
statement
Default class methods (JAVA)
- Constructor
- finilize()
- toString()
- equals(Object obj)
- hashCode()
- clone() - overriding method should implement the
Clonable
interface - getClass()
Note
Use
@Override
to override the certain default methods,@Override public String toString() { // ... }
Note
Java doesn’t have a default copy constructor. But it’s easy to create our own
public class MyClass { private int i; public MyClass(MyClass obj) { this.i = obj.i; } }
And the copy constructor is different from the clone function
Function signature
- Function signature is made up of it’s
name
andparameter list
return type
andaccess specifiers
are not a part of the function signature
Function Overloading
- Certain languages (not neccessarily OOP) allows to define multiple methods with the same name but different within a class
- Overloading cannot be done solely by having different return types and access specifiers
- It is a complie-time concept, i.e, the compiler decides which method to call
Function overriding
- An OOP concept that allows a sub-class to provide a specific implementation to a method that’s already defined in the base class
- Inheritance is required
- The overriding method must have the same signature as the method in the base class
How to destroy an object
Defining class methods
Within the class,
class MyClass {
public:
MyClass() {} // constructor
void func() {
cout<< "Hello";
}
}
Outside the class,
class MyClass {
public:
MyClass () {}
void func () {}
}
void MyClass::func() {
cout<< "Hello!";
}
Access specifiers in classes
- Public
- Private
- Protected