Oops Unit4 Preeti
Oops Unit4 Preeti
Const Variables
If we declare a variable as const, we cannot change its value. A const variable must be
assigned a value at the time of its declaration.
Once initialized, if we try to change its value, then we will get compilation error. The
following two cases will give us an error since in both these cases, we are trying to
change the value of a const variable.
int main()
{
const int a = 5; // declaring and initializing a const variable
a = 10; // this will give compilation error
return 0;
}
int main()
{
const int a = 5; // declaring and initializing a const variable
a++; // this will give compilation error
return 0;
}
Constant member functions are those functions that are denied permission to change
the values of the data members of their class. To make a member function constant,
the keyword “const” is appended to the function prototype and also to the function
definition header.
Like member functions and member function arguments, the objects of a class can
also be declared as const. an object declared as const cannot be modified and hence,
can invoke only const member functions as these functions ensure not to modify the
object.
A const object can be created by prefixing the const keyword to the object
declaration. Any attempt to change the data member of const objects results in a
compile-time error.
Syntax:
(i) For function declaration within a class.
<return_type> <function_name>() const
Example:
int get_data() const;
Example:
int get_data() const
{
//function body
Example:
int Demo :: get_data() const
{
}
•When a function is declared as const, it can be called on any type of
object, const object as well as non-const objects.
• Whenever an object is declared as const, it needs to be initialized at the
time of declaration. however, the object initialization while declaring is
possible only with the help of constructors.
A function becomes const when the const keyword is used in the function’s
declaration. The idea of const functions is not to allow them to modify the object on
which they are called. It is recommended practice to make as many functions const
as possible so that accidental changes to objects are avoided.
class Demo
{
int x;
public:
void set_data(int a)
{
x=a;
}
int get_data()
{
++x;
return x;
}
};
main()
{
Demo d;
d.set_data(10);
cout<<endl<<d.get_data();
return 0;
}
Output
11
Static Data Members:
A data member of a class can be qualified as static. The properties of a static member
variable are similar to that of Cs static variable. A static data member has certain
special characteristics. They are:-
• It is initialized to zero when the first object of its class is created. No other
initialization is permitted.
• Only one copy of that member is created for the entire class and is shared
by all the objects of that class, no matter how many objects are created.
• It is visible only within the class, but its lifetime is the entire program.
A static variable is normally used to maintain value common to the entire class. For
e.g, to hold the count of objects created. Note that the type and scope of each static
member variable must be declared outside the class definition. This is necessary
because the static data members are stored separately rather than as a part of an object.
Declaration
static data_type member_name;
If you are calling a static data member within a member function, member function
should be declared as static (i.e. a static member function can access the static data
members)
Example
#include <iostream>
using namespace std;
class Demo
{
public:
static int ABC;
};
//defining
int Demo :: ABC =10;
int main()
{
Output
Value of ABC: 10
Like a static member variable, we can also have static member functions. A member
function that is declared static has the following properties:-
• A static function can have access to only other static members (function or
variable) declared in the same class.
• A static member function can be called using the class name (instead of its
object) as follows-
Class_name::Function_name();
Consider the example, here static data member is accessing through the static member
function:
#include <iostream>
using namespace std;class Demo
{
private:
static int X; public:
static void fun()
{
cout <<"Value of X: " << X << endl;
}
};//defining
int Demo :: X =10;
int main()
{
Demo X; X.fun();
return 0;
}
Output
Value of X: 10
Polymorphism:
The word “polymorphism” means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than one
form.
A real-life example of polymorphism is a person who at the same time can have
different characteristics. A man at the same time is a father, a husband, and an
employee. So the same person exhibits different behavior in different situations.
This is called polymorphism. Polymorphism is considered one of the important
features of Object-Oriented Programming.
Types of Polymorphism
• Compile-time Polymorphism.
• Runtime Polymorphism.
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
A. Function Overloading
When there are multiple functions with the same name but different parameters, then
the functions are said to be overloaded, hence this is known as Function
Overloading. Functions can be overloaded by changing the number of
arguments or/and changing the type of arguments. In simple terms, it is a feature
of object-oriented programming providing many functions to have the same name
but distinct parameters when numerous tasks are listed under one function name.
There are certain Rules of Function Overloading that should be followed while
overloading a function.
Below is the C++ program to show function overloading or compile-time
polymorphism:
// Driver code
int main()
{
Geeks obj1;
Output
value of x is 7
value of x is 9.132
value of x and y is 85, 64
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type,
this ability is known as operator overloading. For example, we can make use of the
addition operator (+) for string class to concatenate two strings. We know that the
task of this operator is to add two operands. So a single operator ‘+’, when placed
between integer operands, adds them and when placed between string operands,
concatenates them.
Below is the C++ program to demonstrate operator overloading:
class Complex {
private:
int real, imag;
public:
Complex(int r = 0,
int i = 0)
{
real = r;
imag = i;
}
// Driver code
int main()
{
Complex c1(10, 5), c2(2, 4);
// An example call to "operator+"
Complex c3 = c1 + c2;
c3.print();
}
Output
12 + i9
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and
dynamic polymorphism are other names for runtime polymorphism. The function
call is resolved at runtime in runtime polymorphism. In contrast, with compile time
polymorphism, the compiler determines which function call to bind to the object
after deducing it at runtime.
A. Function Overriding
Function Overriding 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.
class base {
public:
virtual void print()
{
cout << "print base class" <<
endl;
}
void show()
{
cout << "show base class" <<
endl;
}
};
void show()
{
cout << "show derived class" <<
endl;
}
};
// Driver code
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Output
print derived class
show base class
Virtual Function
A virtual function is a member function that is declared in the base class using the
keyword virtual and is re-defined (Overridden) in the derived class.
Some Key Points About Virtual Functions:
• Virtual functions are Dynamic in nature.
• They are defined by inserting the keyword “virtual” inside a base class
and are always declared with a base class and overridden in a child class
• A virtual function is called during Runtime
Below is the C++ program to demonstrate virtual function:
// C++ Program to demonstrate
#include <iostream>
class Base {
public:
// virtual function
"\n\n";
void print()
"\n\n";
};
public:
void display()
"\n\n";
void print()
};
// Driver code
int main()
Base* base;
Child child;
base = &child;
base->Base::display();
base->print();
Output
Called virtual Base Class function
Called Base print function
Dynamic Binding:
Dynamic binding in C++ is a practice of connecting the function calls with the
function definitions by avoiding the issues with static binding, which occurred at build
time. Because dynamic binding is flexible, it avoids the drawbacks of static binding,
which connected the function call and definition at build time.
In simple terms, Dynamic binding is the connection between the function declaration
and the function call.
So, choosing a certain function to run up until runtime is what is meant by the
term Dynamic binding. A function will be invoked depending on the kind of item.
Usage of Dynamic Binding
It is also possible to use dynamic binding with a single function name to handle
multiple objects. Debugging the code and errors is also made easier and complexity
is reduced with the help of Dynamic Binding.
Static Binding Vs Dynamic Binding
Static Binding Dynamic Binding
Real objects never use static binding Real objects use dynamic binding
Virtual functions
A virtual function is a member function declared in a base class and re-declared in a
derived class (overridden). You can execute the virtual function of the derived class
when you refer to its object using a pointer or reference to the base class. The concept
of dynamic binding is implemented with the help of virtual functions.
Example:
• C++
// C++ Program to Demonstrate the implementation of Dynamic
// Binding without the help of Virtual Function
#include <bits/stdc++.h>
using namespace std;
class A {
public:
void Add(int a, int b) // Function Definition
{
cout << a+b << endl;
return;
}
// Function Definition
void Sub(int a, int b)
{
cout << a-b;
}
};
int main()
{
A obj;
obj.Add(10, 12);
obj.Sub(12, 10);
return 0;
}
Output
22
2
Example:
// C++ Program to Demonstrate the Concept of Dynamic binding with the
help of virtual function
#include <iostream>
using namespace std;
class A {
public:
// function that call print
void call_Function()
{
print();
}
// the display function
void print()
{
cout << "Printing the Base class Content" << endl;
}
};
// B inherited publicly
class B : public A {
public:
void print() // B's display
{
cout << "Printing the Derived class Content"
<< endl;
}
};
int main()
{
A obj; // Creating A's pbject
obj.call_Function(); // Calling call_Function
B obj1;; // calling GFG2
Obj1.call_Function();
return 0;
}
Output
Printing the Base class Content
Printing the Base class Content