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

  1. Encapsualtion
  2. Abstraction Hiding
  3. Inheritance
  4. 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)

  1. Default Constructor
    1. Object initialisation
      1. Initialises object’s attributes
      2. 0 for numerical types
      3. null for pointers
  2. Destructor
    1. Called when an object is to be destroyed (explicitly or out of scope)
    2. Release the resources that were held by object
  3. Copy Constructor
    1. Used to create a new object as a copy of an existing object
  4. Copy (overloaded) Assignment Operator
    1. Used to assign values of one object to other
    2. For the = operator
  5. Move constructor and move assignment operator
    1. C++ 11 & later
    2. Overloads &&
    3. Efficient transfer of resources btw objects

Default class methods (Python)

  1. __init__ - constructor
  2. __str__ - - returns human readable string representation of the object
  3. __del__ - destructor, not guarenteed to be executed immediately
  4. __eq__ and __ne__ - compare objects for equality (==) and inequality (!=)
  5. __lt__, __le__, __gt__, __ge__ - methods for <, <=, >, >=
  6. __hash__ - returns object hash - when using objects as dictionary keys
  7. __repr__ - returns official string representation of an object
  8. __call__ - allows an object to be called like a function
  9. __enter__ and __exit__ - used for implementing context managers using the with statement

Default class methods (JAVA)

  1. Constructor
  2. finilize()
  3. toString()
  4. equals(Object obj)
  5. hashCode()
  6. clone() - overriding method should implement the Clonable interface
  7. 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 and parameter list
  • return type and access 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

  1. Public
  2. Private
  3. Protected