OOP concepts

Object Oriented Programming

I always wondered why they called it object oriented programming when I studying at an computer Institute in 1999.

Class —-’What is a class in programming?

The best way to understand class is C++.Not an easy to grasp,but class concept is best taught using C++.

Class in real world is a collection of students.Similarly in programming ,specifically C++ Class is collection of data members,functions and methods.

An object is instance of a class.I did not understand this definition when I first studied oop.In laymans term it object is a representative of a class.It can be instantiated and thus occupy memory.

A function or method in C++ is a collection of instructions.

Say think of real world events.The process for making a cup of tea is as follows:

  1. Heat water in kettle.
  2. Pour water in cup.
  3. Add sugar.
  4. Add milk.
  5. Serve.

When these process is placed into collection it called a method or function.

For example:

makeTea()

{

int water;

int sugar;

……

}

But difference between two is demonstrated in Java.

A method and function both are collection of process but in function it always returns.

We are not going in details about functions.

——-code—’’’

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

double number, squareRoot;

cout << “Enter a number: “;

cin >> number;

// sqrt() is a library function to calculate square root

squareRoot = sqrt(number);

cout << “Square root of ” << number << ” = ” << squareRoot;

return 0;

}

—————

The object oriented includes:

  • Inheritance

Inheritance in C++ is process where one class acquires properties of objects of another class.

To put it simply just think of a child inherits traits from his father or mother.

class Car{

int wheels;

int lights;

int speed;

int drive()

{

return 1;

}

int applyBrakes()

{

}

}

//end of class Car.

class Lamborghini:public Car{

int driveFaster();

}

// end of class lamborghini.

In above example class Lamborghini inherits all functions and methods defined in class Car.You do not have to write all functions in Car.Just do anew function.

In C ++ multiple inheritance is possible .That is class Lamborghini can inherit from another class Airplane thus making it flying Lamborghini.!

https://youtu.be/LsMiVOHhQ6o

  • Encapsulation

Encapsulation is process of hiding data and functions from outside interference.

#include <iostream>

using namespace std;

class Adder {

public:

// constructor

Adder(int i = 0) {

total = i;

}

// interface to outside world

void addNum(int number) {

total += number;

}

// interface to outside world

int getTotal() {

return total;

};

private:

// hidden data from outside world

int total;

};

int main() {

Adder a;

a.addNum(10);

a.addNum(20);

a.addNum(30);

cout << “Total ” << a.getTotal() <<endl;

return

}

Here certain data and methods are hidden from public use depending on the programmer..

In above example the method and function :

  • addNum
  • getTotal

can be accesed outside the class.That is if you create another class and inherit from class Adder then you can use above method&function.

But for example the private variable total is hidden.It cannot be accessed outside the class.

This hiding is called encapsulation.

Polymorphism

What is Polymorphism?Polymorphism you might have heard in school.The polymors of carbon like:coal or diamond.These are both carbon in different forms.In different forms they have different attributes unlike Carbon.

In OOP you have Polymorphism.The ability to recognize which function to call based on parameters.Iam not going in details of Polymorphism but just to explain the concept simply.

#include<iostream.h>

using namespace.std;

class myGrocery

{

//we define fruits ,vegatbles,pastry products as datatypes

// say fruit datatypes are :fruit,orange,apple,banana

// vegetable datatypes are :onion,tomato,cucumber

// pastry datatypes are :cake,pastry,pudding

We have three functions od add,1 for adding fruits,one for vegetables,and one for pastry.

void add(fruit apple, fruit orange)

{

int total=apple+orange;// total no of fruits.

}

void add(vegetable onion ,vegetable cucumber)

{

int total=onion+cucumber;// totalvegetables

}

void add(pastry cake ,pastry pudding)

{

int total=cake+pudding;//total of all pastries

}

main()

{

mygrocery objgrc;

objgrc.add( apple,orange);// outputs total fruits if we output total.

objgrc.add(onion,cucumber);//outputs vegetables if we output total

objgrc.add(cake,pastry);// outputs pastries if we output total.

}

}

// end of class

The example is just to prove a concept.Don’t try to run it though.This example can be implemented in other languages called Java,C#.

Our Score
Our Reader Score
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *