Oops (Object Oriented Programming Structure)
Oops (Object Oriented Programming Structure)
1. OOPs :
OOPs is just a technique which has some concepts for Objects . Before going to
know about those concepts we need to understand what an Object is and Why we go for
OOPs technique .
2. Object :
In computer terms we call every real world entity as an object . In the real world ,
everything ( laptop , human , animal , trees , etc… ) has its own property and
functionality .
● Ex :
○ Object : Human
○ Properties : Eyes, nose , mouth , fingers
○ Functionalities : Watching , Eating , Writing
Hence If we create an application for a real world entity we should handle that
object’s properties and functionalities in an efficient manner to get an optimized
application . That’s why we are going for OOPs technique.
3.1 Class :
Class is a programming part where we can define properties and functionality of
an object in a single place . It will act as a template ( like blueprint in construction ) for
the object . We can create objects using this template .
Real world Ex : In construction we have a blueprint and using that we can build
any number of buildings with the same property .
Like here we can create any number of objects with the same property using that
class template .
Inside class we can define constructor and destructor .
Constructor is used to create objects and a destructor is used for destroying
objects.
Code :
class Student {
// properties
string name;
int id ;
string dep;
//functionality
void writing() {}
void reading() {}
Example 1
3.2 Encapsulation :
3.3 Inheritance :
In the real world most of the things Inherit properties from another thing and
additional to that it has its own property .
● Ex : Son is getting some behaviors from his parents like eating, breathing,
walking .
In those situations in application we do not need to rewrite parent class behavior to
the child the child class . We can just extend the parent class in the child class .
Code :
class Parent {
// properties
Eyes
Nose
Mouth
//functionality
eating();
sleeping();
walking();
breathing();
// properties
Eyes //Getting automatically from Parent
Nose //Getting automatically from Parent
Mouth //Getting automatically from Parent
//functionality
eating(); //Getting automatically from Parent
sleeping(); //Getting automatically from Parent
walking(); //Getting automatically from Parent
breathing(); //Getting automatically from Parent
painting();
singing();
}
Example 2
3.4 Polymorphism :
Polymorphism means having many forms . For example a woman can be a sister ,
mother , teacher in different places . like that the object's behavior can be different in
different places .
● It has two different categories :
○ Compile Time polymorphism .
○ Runtime polymorphism .
What is run time ? :- After the successful compilation , the program will be
executed and that state is called runtime .
Code :
class Area {
int area(int a ) // area of square
{
return a*a;
}
int area(int l,int m) // area of rectangle
{
Return l*m;
}
float area(float r) // area of circle
{
return 3.14*r*r;
}
}
Example 3
Ex :
class Student
{
int id ;
int name ;
Student(){
id=0;
name=””;
}
Student(int id1)
{
id=id1;
name=””;
}
Student(int id1,string name1)
{
id=id1;
name=name1;
}
}
Example 4
3.4.2.1 Overriding
Method Overriding is a polymorphism which happens between child and parent
classes. In Method Overriding the method signature should be matched . In Overriding ,
the parent class method is always overridden by the child class method in child class .
Method overriding is based on the underlying object not based on reference.
Example :
class parent {
void print(){
System.out.println("parent class");
}
}
class child extends parent{
void print(){
System.out.println("Child class");
}
}
class Main{
public static void main(String[] args)
{
child c=new child();
c.print(); // child class
}
}
Example 5
We can get parent class specific methods using parent class objects .
Example :
#include<iostream>
using namespace std;
class parent{
public:
virtual void print(){
cout<<"hello";
}
};
class child : public parent{
public:
virtual void print3(){
cout<<"hai";
}
};
int main()
{
parent* p;
child c;
p=&c;
p->print();
}
Example 6
3.5.1 public
If we declare a variable or function under public , then we can access those
variables or functions anywhere in the program using the object reference .
It provides program level access .
3.5.2 private
If we declare a variable of a function under private, then we can access those
variables or functions inside the same class only .
It provides class level access .
3.5.3 protected
If we declare a variable of a function under protected then we can access those
variables or functions inside the same class or Its child class .
Data abstraction can also be used to optimize the code without exposing the
internal implementation . Ex : In MS-DOS they are using only 16 bits for storing DATE
information .