Object Oriented Programming Language

OOPs (Object Oriented Programming Language ):
It is used to decompose a problem into a number of entities called objects and then builds data and  functions around these entities.

=> Object oriented programming is technique where we can define the object.
=> It divided the program into an object.
=> It follows the bottom up approach
=> Data is hidden and can not be accessed by external function.
=> New data and function can be easily added whenever necessary.

opps follows the some concepts.
1) classes 2) Objects
3) Data Abstraction 4) Data Encapsulation
5) Inheritance 6) Polymorphism
7) Dynamic Binding 8) Message Passing.


1) Objects:
=> It is basic run time entities.
=> It is also a instance of class.
=> With object we can access the member function & data member of class.


2) classes
=> Class is the template for object. It is the logical unit.
=> A class contains the different data types of data member and member function in single unit.
=> 3 access specifier are used in the class.
a) Private : It accessible by only the same class member.
b) Public  : Other data member and function can be access.
c) Protected: It is generally used when inheritance.

Difference between class and structure.
=> By defualt all member of structure are public.
=> By defualt all member of class are private.

// Demo of CLass & Object
#include <iostream.h>
#include<conio.h>
class Demo
{
public:
void display(){
cout<<"Welcome to Programming World";
}
};

void main()
{
clrscr();
Demo d;
d.display();
getch();
}

3) Data Abstraction
=> Data Abstraction means showing essential feature and hiding non - essential feature from user.
=> For Eg.  Yahoo Mail...

When you provide the user name and password and click on submit button..It will show Compose,Inbox,Outbox,Sentmails...so and so when you click on compose it will open...but user doesn't
know what are the actions performed internally....It just Opens....that is essential; User doesn't know internal actions ...that is non-essential things...

For Eg. Tv Remote..
Remote is a interface between user and tv..right. which has buttons like 0 to 10 ,on /of etc but we dont know circuits inside remote...User does not  need to know..Just he is using essential thing that is remote.


4) Data Encapsulation
=> Encapsulation means which binds the data and methods in single unit (class).

=>  For Example:
A car is having multiple parts..like steering,wheels,engine...etc..which binds together to form a single object that is car. So, Here multiple parts of cars encapsulates itself together to form a single object that is Car.

In real time we are using Encapsulation for security purpose...

Encapsulation = Abstraction + Data Hiding.

5) Inheritance
=> Inheritance is process by which object of one class acquire the propertices of object of another class.
=> For Eg.

For Eg.,

class Address
{
String name;
Srting House_no;
String Street name;
}
class LatestAddress extends Address
{
String City;
String State;
String Country;
}
public class Vishal
{
{
LatestAddress la = new LatestAddress();
//Assign variable accordingly...
}
}

In the above Example class LatestAddress getting all features from the Address class.
In the LatestAddress class we have total 6 properties..3 are inherited from Address class and 3 properties are
incorporated. So In the class Vishal we are declaring the object of class LatestAddress and then assign new variables using the properties of the previous base classes... So this is a nice example of inheritance..

6) Polymorphism
=> Polymorphism means ability to take more than one form that an operation can exhibit different behavior at different instance depend upon the data passed in the operation.

=> For Eg.
1>We behave differently in front of elders, and friends. A single person is behaving differently at different time.


2> Consider the stadium of common wealth games. Single stadium but it perform multiple task like swimming, lawn tennis etc.


// virtual function demo
#include <iostream.h>
#include<conio.h>
class B
{
public:
virtual void display(){
cout<<"Content of base class";
}
};

class D1 : public B
{
public:
void display(){
cout<<"Content of 1st derived class";
}
};

class D2 : public B
{
public:
void display(){
cout<<"Content of 2nd derived class";
}
};

int main()
{
clrscr();
B *b;
D1 d1;
D2 d2;

b=&d1;
b->display();/*calls display of class derived D1*/

b=&d2;
b->display();/*calls display of class derived D2*/

getch();
return 0;
}


7) Dynamic Binding
=> Binding refers to the linking of a procedure call the code to be execute in responce call the code to be execute in responce to the call.

Dynamic bining also known as late biding means that the code associted with a given procedure is not known until the time of the call at run time.

It is ass with polymorphism & Inheritance.



8) Message Passing.
An object oriented program consist of a set of objects that communicate with each other.
The process of programming in an object oriented language, involves the following steps:
i) Creating classeds that defines objects and their behavior.
ii) Creating object from class defination.
iii) Establishing communication amoung object.


Summary:
OOPs have following features:

1. Object              - Instance of Class
2. Class               - Blue print of Object
3. Encapsulation       - Protecting our Data
4. Polymorphism        - Different behaviors at different instances
5. Abstraction         - Hiding our irrelevant Data
6. Inheritence         - One property of object is acquiring to another property of object




Friend Function:
It is possible to non member function access to the private member of class by using a friend.
A friend function has access to all private & protected of the class for which it is a friend.

// Friend function
#include <iostream.h>
#include<conio.h>
class height;
class width
{
int w;
public:
void getData(int wt)
{
w=wt;
cout<<"Width : "<<w<<endl;
}

friend void area(width,height);
};

class height
{
int h;
public:
void getData(int ht)
{
h=ht;
cout<<"Height : "<<h<<endl;
}

friend void area(width,height);
};

void area(width w,height h)
{
int a;
a=w.w*h.h;
cout<<"Area is:"<<a;
}
void main()
{
clrscr();
width w;
height h;

w.getData(10);
h.getData(20);
area(w,h);

getch();
}


Inline function:
If a function is define as a inline function then the body of function will be posted at the calling function.

// Inline function
#include <iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x, float y){
return (x*y);
}

inline float cude(float x){
return (x*x*x);
}
};

void main()
{
line obj;
float a,b;
clrscr();
cout <<""Enter two values:";
cin>>a>>b;
cout<<"\n Multiplication value is "<<obj.mul(a,b);
cout<<"\n Cube value is "<<obj.cude(a);
getch();
}
Constructor:
=> The constructor has the same name as its class name.
=> The constructor has no any return type.
=> If we will not define any constructor in class then compiler will provide a dummy constructor.
=> THe main purpose of constructor is to allocate the memory for the object.
=> We can also initialise the data member in the class.
=> If we want to overload the constructor it means the constructor will accept some parameters.
=> We can not define a constructor as a virtual function
=> The constructor should be define in punlic section of class.
=> A constructor is a special member function whose  take is to initialise the object of its class.
// constructor demo
#include <iostream.h>
#include<conio.h>
class demo
{
public:
demo()
{
cout<<"This is constructor..!"<<endl;
cout<<"This is call without object..!"<<endl;
}
};

void main()
{
clrscr();
demo d;
getch();
}

Destructor:
=> The Destructor is a member function which has also the same name as the class name. But the name of the class will be following a filled sign (~).
=> The main purpose of destructor is deallocate the memory.
=> We can not overload a destructor becoz it can not accept the parameters.
=> The Destructor should be define in punlic section of class.
=> e.g.:
// destructor demo
#include <iostream.h>
#include<conio.h>
class demo
{
public:
demo()
{
cout<<"This is constructor..!"<<endl;
cout<<"This is call without object..!"<<endl;
}

~demo()
{
cout<<"This is destructor..!"<<endl;
}
};

void main()
{
clrscr();
demo d;
getch();
}

Static data member:
=> When we use a normal variable in the class, every  object will get a separate 2 - copy of that variable. It means the memory allocation of that variable will be done separate 2 for every object
=>But when we declare a variable as static, only one copy of that variable will be available for all the object.


5) Inheritance
=> Inheritance is process by which object of one class acquire the propertices of object
of another class.
=> The derived class inherits all the capabilities of the base class, but can add
embellishments and refinements of its own.
=> advantages of inheritance in C++?
* It permits code reusability.
* Reusability saves time in program development.
* It encourages the reuse of proven and debugged high-quality software,
thus reducing problem after a system becomes functional.
=> There are different types of inheritance.
1] Single 2] Multiple
3] Hierachical 4]multilevel
5] Hybrid

Comments

Popular posts from this blog

English Speaking in Hindi Day-1