Object Oriented Programming

Object Oriented Programming

Topics In OOPS
  1. What is OOPS ?
  2. Class
  3. Objects
  4. Constructor & destructor
  5. Encapsulation
  6. Abstraction
  7. Inheritance
  8. Access Specifiers
  9. Polymorphism
  10. Virtual Function & Abstract Class
  11. Friend Function
Why OOPS ?
Previously there was a procedure oriented programming language where the code is divided into tasks and functions . It used high level languages like C,Cobol,Fortran,etc .

Global Data:- Accessible from entire program so it is difficult to find which function has made changes in the data for that reason **OOPS is introduced

What is OOPS?
Main focus on data.
Bind the data to the functions using OOPS.
In OOPS program is divided into objects and objects further divided into data and functions

What is class ?
Class is a blueprint of an objects.
It is a fundamental unit of OOPS.
It is a user defined datatype.
Class contains some data/functions or methods.


Ex: Here Car is a Class and Tesla , Bugatti, Tata,etc are the objects.
And there color,typres,mileage,weight,etc are their properties.

Ex:-
#include<iostream>
using namespace std;
class Fruit{
public:
  string name;
  string color;
};
int main(){
    Fruit apple;
    apple.name="Apple";
    apple.color="Red";
    cout<<apple.name<<" "<<apple.color<<endl;
    return 0;
}

OUTPUT:- Apple Red

What are Objects ?
Objects are nothing but the variables of datatype class.
An object is an instance of class that has its own state and behaviour. An object can represent the real world entity , such as a car, a person , or a laptop

Syntax: class_name object_name;

Another Way to create object :-
class_name *object_name=new class_name();

How to access properties ?
Fruit *mango=new Fruit();
mango->name="mango";
mango->color="Yellow";

Constructor In OOPS ?
Used to initialize an object value.
This is a function which is called when object is created.
Same name as class name
Types: 1)Default Constructor
            2)Parametrised Constructor
            3)Copy Constructor
1) Default Constructor
Here values are given by default
Ex:-
#include<iostream>
using namespace std;
class Rectangle{
public:
     int x;
     int y;
     Rectangle(){ //constructor
         x=2;
         y=4;
     }
};
int main(){
    Rectangle *r=new Rectangle();
    cout<<r->x<<" "<<r->y<<endl;
    return 0;
}

OUTPUT:- 2 4

2) Parametrised Constructor


Comments