Professional Documents
Culture Documents
BY MR. TARUS
Pointers
Variable which stores the address of another variable.
Pointer variables can be of type i.e, int, float, char, array, function, etc.
It makes you able to access any memory location in the computer's memory.
Changes made inside the function is Changes made inside the function is
not reflected on other functions reflected outside the function also
Actual and formal arguments will be Actual and formal arguments will be
created in different memory location created in same memory location
C++ Function Overriding:
If derived class defines same function as defined in its base class, it is known as
function overriding
overriding.cpp
class Animal {
public:
void eat(){
int main()
cout<<"Eating...";
{
}
Dog d;
};
d.eat();
class Dog: public Animal
return 0;
{
}
public:
void eat()
{
cout<<"Eating bread...";
} };
C++ virtual function
A member function in the base class that you redefine in a derived class.
Declared using the virtual keyword.
Used to tell the compiler to perform dynamic linkage or late binding on the function.
A 'virtual' is a keyword preceding the normal declaration of a function.
When the function is made virtual, C++ determines which function is to be invoked
at the runtime based on the type of the object pointed by the base class pointer.
There is a necessity to use the single pointer to refer to all the objects of the different
classes. So, we create the pointer to the base class that refers to all the derived
objects. But, when base class pointer contains the address of the derived class
object, always executes the base class function. This issue can only be resolved by
using the 'virtual' function.
When you refer to a derived class object using a pointer or a reference to the base
class, you can call a virtual function for that object and execute the derived class’s
version of the function.
Pto…
Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
The pointer can only access the base class members but not the members of the
derived class.
Although C++ permits the base pointer to point to any object derived from the
base class, it cannot directly access the members of the derived class.
Therefore, there is a need for virtual function which allows the base pointer to
access the members of the derived class.
C++ virtual function ; virtual1.cpp
class A {
public:
virtual void display() int main()
{ {
cout<<"Base class is invoked"; A *a; //pointer of base class
} B b; //object of derived class
};
a = &b;
class B : public A {
a->display();//Late Binding occurs
public:
void display()
}
{
cout <<"Derived Class is invoked";
}
};
Pure Virtual Function
Not used for performing any task. It only serves as a placeholder.
Function declared in the base class that has no definition relative to the base class.
A class containing the pure virtual function cannot be used to declare the objects
of its own, such classes are known as abstract base classes.
The main objective of the base class is to provide the traits to the derived classes
and to create the base pointer used for achieving the runtime polymorphism.
Syntax:
virtual void display() = 0;
class Base int main()
{
public: {
virtual void show() = 0;
Base *bptr; //Base b;
};
class Derived : public Base Derived d;
{
bptr = &d;
public:
void show() bptr->show();
{
return 0;
cout<<"Derived class is derived from the
base class."; }
} Base class contains the pure virtual function (abstract base class).
}; We cannot create the object of the base class.
C++ Polymorphism
• The process of using a function or an operator for more than one purpose.
• Having many forms.
• Two types of
polymorphism in C++:
Two types:
Compile time polymo
Runtime polymo
Compile time polymorphism:
Overloaded functions are invoked by matching the type and number of arguments.
This information is available at the compile time and, therefore, compiler selects
the appropriate function at the compile time.
achieved by function overloading and operator overloading which is also known as
static binding or early binding.
thisPointer1.cpp
class Employee { int main()
public:
{
int id; //data member (also instance variable)
string name; //data member(also instance variable) Employee e1 =Employee(101,
float salary;
"Sonoo", 890000); //creating an
object of Employee
Employee(int id, string name, float salary)
{ Employee e2=Employee(102,
this->id = id; "Nakul", 59000); //creating an
this->name = name; object of Employee
this->salary = salary;
}
e1.display();
void display()
{ e2.display();
cout<<id<<" "<<name<<" "<<salary<<endl; return 0;
} }
};