BASIC CONCEPTS OF OOP’S
- CLASS
- OBJECT
- DATA ABSTRACTION
- DATA HIDING
- ENCAPSULATION
- INHERITANCE
- POLYMORPHISM
- DYNAMIC BINDING
- MESSAGE COMMUNICATION
CLASSES AND OBJECTS
Class: class is a user defined data type that represents an entity, which is a collection of members and methods.
The entire set of data and code of an object can be made the user define data type with help of a class. Infact objects are variables of type class. Once a class has been defined, we can create any number of objects belonging to that class.
Syntax:
class <class
name>
{
access specifier/visibility mode:
variable declarations/data members;
access
specifier/visibility mode:
function declarations/member
functions;
};
The
keyword class specifies that what follows is abstract data of type class. The
class body contains the declarations of variables and functions, which are
called as class members. The variables declare inside the class are known as data
members and functions are known as member functions.
C++
supports three types’ access specifiers
a) public
b) private
c) protect
Public: It indicates the accessibility of a member to the outside class i.e., the data members can be accessed by any function from the outside. These members are used to provide an interface for the outside classes.
Private: The members can be accessed only by the member functions. The class members that have been declared as private can accessed only from within the class. By default, the members of class are private. Data hiding is possible with the help of private keyword.
Protected: The member data can be accessed by member functions of that class. The member is also accessible to member functions and the friend functions that are derived from this class. But they are not available to any one of the outside function. This specifier is most recently in the concept of Inheritance.
DIFFERENCES BETWEEN THE THREE ACCESS SPECIFIERS
Public |
Private |
Protected |
Accessible from
own class members |
Accessible from
own class members |
Accessible from
own class members |
Accessible from
derived class members |
Not Accessible
from derived class members |
Accessible from
derived class members |
Accessible from objects
outside the class |
Not Accessible
from objects outside the class |
Not Accessible
from objects outside the class |
The declaration of
an object is similar to that of a variable of any basic type.
Syntax: classname obj_name;
Example: student s;
where s is an object
of class student.
Class: is a collection of data & functions.
Class is an entity, which may represent a person, place, and location anything
which we can feel it in real-life.
Object:
is a functionable element
that is used to access the members and methods of a class. Simply, object is an
instance of class.
Example
Program on Class and Object
#include<iostream.h>
#include<conio.h>
class examp
{
int a,b,t;
public:
void getdata();
void swap();
void disp();
};
void
examp::getdata()
{
cout<<"enter
a and b values"<<endl;
cin>>a>>b;
}
void examp::swap()
{
t=a;
a=b;
b=t;
}
void examp::disp()
{
cout<<"After
swapping";
cout<<a<<"\t"<<b;
}
void main()
{
examp e;
clrscr();
e.getdata();
e.swap();
e.disp();
getch();
}
Output:
enter a and b values
2 3
After swapping 3 2
DATA
ABSTRACTION
Abstraction refers to an act of representing only external features without including the background details or explanations. The process of extracting the essential features without exploring non-essential things is part of object. Abstraction permits the programmer to look at something without being concerned with its internal details
Example
Driver is an
implementer here. If he was about to stop he has to stop he is not responsible
for the rest of the non-essential things. Creating new data types using
encapsulated data item, which suits to an application to be programmed is known
as data abstraction.
DATA
HIDING AND ENCAPSULATION
Data hiding is an insulation of the data from the direct access of the program is called as Data Hiding. It is a process of attaching the data closely to the system by protecting the data from other parts of the system. It means that data is concealed within a class, so that it cannot be accessed by functions outside the class even by mistake. The mechanism used to hide data is to put it in a class and make it private.
Encapsulation it is a method of wrapping up of data and
functions into a single unit. This is the central idea of OOP. The data cannot
be accessed by the outside functions but the data can only be accessed by only
those functions, which are wrapped in that single unit.
/* DATA HIDING AND
ENCAPSULATION */
#include<iostream.h>
#include<conio.h>
class hiding
{
private:
int age; /* data hiding */
public:
void setData(int);
void print();
};
void hiding::setData(int
x)
{
if(age>0)
age=x;
}
void hiding::print()
{
cout<<age<<endl;
}
void main()
{
clrscr();
hiding h1;
h1.setData(13);
h1.print();
getch();
}
Output:
13
INHERITANCE
The
mechanism of deriving a new class from an old one is called as Inheritance. The
old class is called as base class and the new one is called as derived
class. The Derived class inherits
some or all the features of the base class. A class can also inherit properties
from more than one class or from more than one class.
Example :
|
WITHIN CLASS |
OUTSIDE CLASS |
DERIVED CLASS |
PUBLIC |
YES |
YES |
YES |
PRIVATE |
YES |
NO |
NO |
PROTECTED |
YES |
NO |
YES |
- Function Overloading: 2 or more functions can have same name with different parameters
- Function overriding:2 or more functions can have same name with same parameters
- Inheritance should be there if you are using function overriding. It can not be done with in a class for this we required derived class and base class
No comments:
Post a Comment