What Is Encapsulation: Different Types of Polymorphism
What Is Encapsulation: Different Types of Polymorphism
What Is Encapsulation: Different Types of Polymorphism
several different forms. In computer science, it describes the concept that objects of different types can be
accessed through the same interface. Each type can provide its own, independent implementation of this
interface. It is one of the core concepts of object-oriented programming (OOP).
If you’re wondering if an object is polymorphic, you can perform a simple test. If the object successfully
passes multiple is-a or instanceof tests, it’s polymorphic. As I’ve described in my post about inheritance,
all Java classes extend the class Object. Due to this, all objects in Java are polymorphic because they pass
at least two instanceof checks.
static or compile-time
dynamic
: What is Encapsulation
Hiding internal state and requiring all interaction to be performed through an object's methods is known
as data encapsulation — a fundamental principle of object-oriented programming.
The main purpose or use of the encapsulation is to provide the security to the data of a class.
To make the data secure we need to use private access modifiers that will restrict the access to the data
outside the class. The access modifiers are used to define the access level or scope of the class members
like data members and functions.
Private :-
The access scope of the private members are restricted to the class scope only, means private
members are not accessible or available from the another class.
Default :-
It is not a keyword, if any access modifiers keyword is not defined then it will be considered as
default.
The access scope of the default members are restricted to the current or same package(folder).
The default members are not accessible or available from classes that are not present in the same
package.
Protected :-
The access level or scope of the protected class members are restricted to within the current or
same package and from another package if and only if a class is inherited by the class from
another package.
Public :-
The access modifier has widest scope mean the public class members can be accessed from any
class despite package of the classes and relationship.
class UserLogin
{
// private instance variables
private String username;
private String password;
public void setName(String name)
{
username = name;
}
public String getName()
{
return username;
}
public void setPassword(String pass)
{
password = pass;
}
public String getPassword()
{
return password;
}
}
Example: A program to demonstrate passing objects by value to a member function of the same class
Q4: Define Inheritance and its types
However, inheritance may be implemented in different combinations in Object-Oriented Programming
languages as illustrated in figure and they include:
Single Inheritance
Multi Level Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Multipath inheritance
Multiple Inheritance
Single Inheritance
When a Derived Class to inherit properties and behavior from a single Base Class , it is called as
single inheritance.
A derived class is created from another derived class is called Multi Level Inheritance .
Hierarchical Inheritance
More than one derived classes are created from a single base class, is called Hierarchical Inheritance .
Any combination of above three inheritance (single, hierarchical and multi level) is called as hybrid
inheritance .
Multipath inheritance
Multiple inheritance is a method of inheritance in which one derived class can inherit properties of base
class in different paths.
Multiple inheritances allows programmers to create classes that combine aspects of multiple classes and
their corresponding hierarchies. In .Net Framework, the classes are only allowed to inherit from a single
parent class, which is called single inheritance
Function overriding is a feature that allows us to have a same function that allows us to have a same
function in child class which is already present in the parent class. A child class inherits the data members
and member functions of parent class, but when you want to override a functionality in the child class
then you can use function overriding.
function overloading
Function overloading is a C++ programming feature that allows us to have more than one function having
same name but different parameter list. It means the data type and sequence of the parameters, for
example the parameters list of a function myfuncn (int a, float b) is (int, float).
Constructor overloading is a concept of having more than one constructor with different
parameters list, in such a way so that each constructor performs a different task. For e.g. Vector
class has 4 types of constructors.
A class constructor is a special member function of a class that is executed whenever we create
new objects of that class.. A constructor will have exact same name as the class and it does not
have any return type at all, not even void. Constructors can be very useful for setting initial
values for certain member variables
Code sample
class Line {
public:
voidsetLength( doublelen );
doublegetLength( void );
Line();...
Operator overloading means that the operation performed by the operator depends on the type of operands
provided to the operator. For example, (a) the bit left-shift operator << is overloaded to perform stream
insertion if the left operand is a ostream object such as cout; (b) the operator * could
means multiplication for two numbers of built-in types or indirection if it operates on an address. C++ lets
you extend operator overloading to user-defined types (classes).
Operator overloading is similar to function overloading, where you have many versions of the same
function differentiated by their parameter lists.
IN C++ A friend class can access the private and protected members of the class in which it is declared as
a friend.
Another use of a friend class is for a part of a data structure, represented by a class, to provide access to
the main class representing that data structure
A friend function of a class is defined outside that class' scope but it has the right to access all private and
protected members of the class. Even though the prototypes for friend functions appear in the class
definition, friends are not member functions
Q:11 What is virtual function?
A virtual function is a member function that is declared within a base class and redefined by a derived
class. To create virtual function, precede the function’s declaration in the base class with the keyword
virtual. When a class containing virtual function is inherited, the derived class redefines the virtual
function to suit its own needs.
Q2: Define a base class shape with protected data members; width and height and setter
member functions for setting width and height, also define the derived class rectangle
publically inherited from class shape. Class rectangle should have a member function
getarea() which should compute and return area of a rectangle. Declare an object of
rectangle in main function and set its width and height then compute its area.
#include <iostream>
class Shape {
public:
void setWidth(int w) {
width = w;
void setHeight(int h) {
height = h;
protected:
int width;
int height;
};
public:
int getArea() {
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
Q3: (Modifying Question:2) Define another class named paint_cost with a member
function get_cost() which returns paint cost of the rectangle on the basis of its area; assume
cost=area*100. Inherit class rectangle from class shape and paint_cost.
#include <iostream>
class Shape {
public:
void setWidth(int w) {
width = w;
void setHeight(int h) {
height = h;
protected:
int width;
int height;
};
class PaintCost {
public:
};
public:
int getArea() {
};
int main(void) {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
cout << "Total paint cost: Rs." << Rect.getCost(area) << endl;
return 0;
Q4: Define a class display with three member functions named “print” first print function
should print an integer passed to it, the second function should print a floating passed to it
and third function should print a character array/string passed to it. Declare an object in
main function of the class you defined and test the functions you made.
#include <iostream>
class printData {
public:
void print(int i) {
void print(double f) {
void print(char* c) {
};
int main(void)
printData pd;
pd.print(5);
pd.print(50.26);
pd.print("Hello Superior");
return 0;
Q5: Define a class named; complex with two data members real and imaginary and
setter/getter member functions. Overload an addition “+” operator and multiplication “*”
operator for your class. Declare two objects in main function of the class you defined and
test the functions and operators you overloaded.
#include <iostream>
class Complex {
public:
double getVolume(void) {
length = len;
breadth = bre;
height = hei;
Complex box;
return box;
private:
double length;
double breadth;
double height;
};
int main() {
Complex Box1;
Complex Box2;
Complex Box3;
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
volume = Box1.getVolume();
volume = Box2.getVolume();
volume = Box3.getVolume();
return 0;
Q7: Create a class named 'Shape' with a function to print "This is a shape". Then create
two other classes named 'Rectangle' and 'Circle' inheriting the Shape class, both having a
function to print "This is rectangular shape" and "This is circular shape" respectively.
Then make pointer type object of shape class and apply polymorphism by using virtual
function.
#include<iostream>
class Circle
protected:
float radius ;
public:
void Enter_r(void)
cout << "\n\t Enter the radius: "; cin >> radius ;
void Display_ca(void)
};
class Rectangle
protected:
public:
void Enter_lb(void)
void Display_ar(void)
};
public:
void volume_cy(void)
};
int main()
{
Circle c ;
c.Enter_r( );
c.Display_ca( );
Rectangle r ;
cout << "\n\n Getting the length and breadth of the rectangle\n\n";
r.Enter_lb( );
r.Display_ar( );
Cylinder cy ;
cout << "\n\n Getting the height and radius of the cylinder\n";
cy.Enter_r( );
cy.Enter_lb( );
cy.volume_cy( );
return 0;
Q8: Create a class named 'A' with a function to print "A Class Function". Then create two
other classes named 'B' and 'C' inheriting the A class, both having a function to print "B
Class Function" and “C Class Function " respectively. Then make pointer type object of A
class and apply polymorphism by using virtual function.
# include <iostream>
class A
{
public:
virtual void pp ()
};
class B : public A
public:
void pp ()
};
class C : public A
public:
void pp ()
};
int main ()
A *p;
B a;
C b;
p=&a;
p-> pp();
p=&b;
p->pp();