Basic Concepts Of OOPs
Basic Concepts Of OOPs
What is class?
· A class is a collection of objects of
same type.
· It is a user defined data type.
· Class defines basic structure and
functionality of an object.
· It is a collection of data member and data
function.
· Once class is defined any number of
objects can be created which belong to that class.
· It is a set of objects that share a common
structure and behavior.
EXAMPLE
class student
{
private:
int
rollno;
int
marks
public:
void
getdata();
void
putdata();
}
What is object?
·
Objects
are the basic run-time entities in an OOPs.
·
We
can create number of object with one class.
·
Object
is a variable of user define data type.
·
The
data stored within an object represents the current state of the object.
·
Object
represents real word entity into a program.
What is
Encapsulation?
· The wrapping
up of data and functions into a single unit (called class) is known as
encapsulation. Class is the best example of it.
· The data is
not accessible to the outside world, and only those functions which are bonded
in the class can access it.
What is data abstraction?
· Abstraction refers to the act of
representing main features without including the background details or
explanations.
· Consider a real life example of driving a car. The man only knows that brakes will stop the car and pressing the accelerators will increase
the speed of car but he does not know about the inner mechanism of the car. This is what abstraction is.
What does
polymorphism mean in C++ language?
· Polymorphism means more than one forms.
· Real life example of polymorphism, a
man at the same time he is a husband, a father, an employee. So the same person
performs different roll in different situations. This is called polymorphism.
· There are
two types of polymorphism, namely, compile time polymorphism and run
time polymorphism.
How is
polymorphism activated at (a) compile time and (b) run time? Or
How do we carry out compile time
polymorphism and run time polymorphism.
a) Compile
time polymorphism:
· Functions
overloading and operator overloading are used to achieve the compile time
polymorphism.
· The
overloaded member functions are selected for invoking by matching arguments,
both type of argument and number of argument.
· This
information is known to the compiler at the compile time, therefore the compile
is able to select the appropriate function for the particular call at the
compile time.
· Compile time
polymorphism is also known as the static binding, static
linking or early binding.
· In short, an
object is bound to its function call at the compile time is known as compile
time polymorphism.
b) Run
time polymorphism:
· Function overriding is used to achieve the run time polymorphism.
· In run time polymorphism appropriate member function is
selected while the program is running.
· Using of virtual function C++ supports run time polymorphism.
· Run time polymorphism is also known as late binding or dynamic
binding because the appropriate function is selected dynamically at
run time.
What is function overriding?
Same name different argument is called function overriding.
What is function overriding?
Same name different argument is called function overriding.
Example
Class A
{
Int x;
Public:
Void show()
{
}
};
Class B:public A
{
Int y;
Public:
Void show()
{
}
};
What is
inheritance? How does it enable reusability?
· The
Procedure of deriving a new class from an old one is called inheritance (or
derivation).
· The old
class is referred to as the ‘base class’ and new one is called the ‘derived
class’ or ‘subclass’.
· C++ strongly
supports the concepts of reusability, and it is achieved using ‘inheritance’.
· The derived
class inherits some or all of the data from the base class.
· A class has also inherit properties from more than one class or from more than one level.
· In
inheritance derived class inherits all/some properties of base class and this
enable code reusability. So we do not need to define the properties in derived
class as in base class again.
What is data
hiding?
· Data hiding means technique of hiding internal details of object.
· Data hiding is achieved using the encapsulation.
· In encapsulation the data and functions are wrapped into a single
unit (called class).
· In that the data is not accessible to the outside world, and only
those functions which are wrapped in the class can access it.
Difference between class and object.
Class
|
Object
|
By default All the members of a class are private.
|
All members of a class are not public by default.
|
Private members cannot be access through any object of that class.
|
Public members can be access through any object of that class.
|
We cannot access private data anywhere in the program
|
We can access public data anywhere in the program
|
Example class student
{
private:
int a;
}
|
Example class student
{
public:
int a;
}
|