OOP Unit 5
OOP Unit 5
Classes:
is a user-defined data type, which holds its own data members and member functions,
which can be accessed and used by creating an instance of that class. A C++ class is
like a blueprint for an object.
1
Difference between object and class:
Class Object
The class has to be declared first and An object is created many times as per
only once. requirement.
2
Class Object
Private:
If the data members are declared as private access then they cannot be accessed from
other functions outside the class. It can only be accessed by the functions declared
within the class. It is declared by the key word „private‟
Public:
If the data members are declared public access then they can be accessed from other
functions outside the class. It is declared by the key word „public‟.
3
Private Public
members cannot be accessed from outside the members are accessible
class from outside the class
It provides high security of its data member It does not provide security for
its data member
It can’t be inherited The public member is
inherited from drive class
Constructors:
4
Example:
class MyClass {
public:
MyClass() // Constructor
{
cout << "Hello World!";
}
};
int main() {
MyClass myObj;
return 0;
}
Types of constructor:-
C++ has the following types of Constructors.
1. Default Constructor
2. Parameterized Constructor
3. Multiple Constructors
4. Constructor with default arguments
5. Copy Constructor
1. Default Constructor
class class_name
{
Public:
Class_name ( ) // default Constructor
{
}
};
int main()
{
Class_name object;
return 0;
}
5
2. Parameterized Constructor
Syntax:
class class_name
{
Public:
Class_name (argument _list) // parameterized Constructor
{
}
};
Copy Constructor
Copy constructor is used to initialize an object from another object. In this way declaration of
constructor involves object of same class in its argument list.
Destructors in C++
A destructor is a special member function that works just opposite to constructor, unlike
constructors that are used for initializing an object, destructors destroy (or delete) the object.
Syntax:
6
Inheritance
The mechanism of deriving a new class from an old one is called inheritance.
In C++, the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is
the specialized class for the base class.
a. Sub Class: The class that inherits properties from another class is called Sub class
or Derived Class.
b. Super Class: The class whose properties are inherited by sub class is called Base
Class or Super class.
7
Types of Inheritance
1) Single inheritance
2) Multiple inheritances
3) Multilevel inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
1) Single Inheritance:-
One derived class inherits from only one base class. It is the most simplest
form of Inheritance.
8
2. Multiple Inheritances: In this type of inheritance a single derived class may inherit
from two or more than two base classes.
Syntax:
3. Multilevel Inheritance:
In this type of inheritance, a derived class is created from another derived class.
9
4.Hierarchical Inheritance
In this type of inheritance, more than one sub class is inherited from a single base
class. i.e. more than one derived class is created from a single base class.
Polymorphism:
10
Types of Polymorphism: In C++ polymorphism is mainly divided into two types:
1. Compile time Polymorphism
a) Function or (Method) overloading
b) Operator overloading
2. Runtime Polymorphism
a) Virtual Function
a. Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded. Functions can
be overloaded by change in number of arguments or/and change in type of
arguments.
11
b. Operator Overloading:
For example, we can make the operator (‘+’) for string class to concatenate
two strings. We know that this is the addition operator whose task is to add
two operands. So a single operator ‘+’when placed between integer operands ,
adds them and when placed between string operands, concatenates them.
Example: // CPP program to illustrate Operator Overloading
#include<iostream>
class Complex {
12
Output: 12 + i9
The overloaded member functions are „selected‟ for invoking by matching arguments,
both type and number. This information is known to the compiler at the compile time
and compiler is able to select the appropriate function for a particular call at the
compile time itself. This is called Early Binding or Static Binding or Static Linking.
13
Also known as compile time polymorphism. Early binding means that an object is
bound to its function call at the compile time.
It would be nice if the appropriate member function could be selected while the
program is
Running. This is known as runtime polymorphism. C++ supports a mechanism
known as virtual function to achieve run time polymorphism.
At the runtime, when it is known what class objects are under consideration, the
appropriate
version of the function is invoked. Since the function is linked with a particular class
much later after the compilation, this process is termed as late binding. It is also
known as dynamic binding because the selection of the appropriate function is done
dynamically at run time.
Function overriding on the other hand occurs when a derived class has a definition
for one of the member functions of the base class. That base function is said to be
overridden.
VIRTUAL FUNCTIONS
When we use the same function name in both the base and derived classes, the
function in the bas class is declared as virtual using the keyword virtual preceding its
normal declaration.
When a function is made virtual, C++ determines which function to use at runtime
based on the type of object pointed to by the base pointer, rather than the type of the
pointer. Thus, by making the base pointer to point to different objects, we can execute
different versions of the virtual function
#include<iostream>
Using namespace stu;
class Base
{
public:
void display()
14
{
cout<<”Display Base”;
}
virtual void show()
{
cout<<”Show Base”;
}
};
void main()
{
Base b;
Derived d;
Base *ptr;
ptr=&b;
ptr->display(); //calls Base
ptr->show(); //calls Base
ptr=&d;
ptr->display(); //calls Base
ptr->show(); //class Derived
}
15
Differences b/w Compile Time and Run time Polymorphism
In Compile time Polymorphism, the call In Run time Polymorphism, the call is
is resolved by the compiler. not resolved by the compiler.
16
What is the 'this' pointer?
In C++, This is a keyword that refers to the current instance of the class.
Example:
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
void setdata (int x)
{
this->x = x;
}
void print()
{ cout << "x = " << x << endl; }
};
int main()
{
Test obj;
obj.setdata(20);
obj.print();
return 0;
}
17
Important questions of UNIT-5
18