Unit 2-1
Unit 2-1
ENGINEERING
Department of Computer Engineering
• Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
Example
1. class ABC : private XYZ //private derivation
{ }
Destructor:
• An object of derived class is destroyed first
• Object of base class is destroyed
Types of Inheritance
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Single Inheritance
A class is allowed to inherit from only one class.
class A
{ public:
A( )
{ cout<<“Constructor of class A”; }
};
class B: public A
{ public:
B()
{ cout<<“Constructor of class B”; }
};
int main()
{
B b1;
}
Multilevel Inheritance
A derived class is created from another derived
class.
class A
{ public:
A( )
{ cout<<“Constructor of class A”; }
};
class B: public A
{ public:
int main()
B()
{
{ cout<<“Constructor of class B”; } C c1;
}; }
Class C: public B
{ public:
C()
{ cout<<“Constructor of class C”; }
};
Multiple Inheritance
One subclass is derived from more than one base
class.
The number of base classes will be separated by a
comma ( , ) and the access mode for every base
class must be specified.
class A
{ public:
A( )
{ cout<<“Constructor of class A”; }
};
class B
{ public:
int main()
B()
{
{ cout<<“Constructor of class B”; } C c1;
}; }
class C: public A, public B
{ public:
C()
{ cout<<“Constructor of class C”; }
};
Ambiguity in Multiple
Inheritance
Class A Class B
display() display()
Class C Main()
C c1;
c1.display();
Ambiguity in Multiple
Inheritance
To solve this ambiguity scope resolution operator :: is used.
ObjectName.ClassName::FunctionName()
Main()
C c1;
c1.A::display();
Hierarchical Inheritance
More than one subclasses are derived from a
single base class
class A
{ public:
A( ) { cout<<“Constructor A”; }
};
class B : public A
{ public:
B( ) { cout<<“Constructor B”; }
};
class C: public A
{ public:
C( ) { cout<<“Constructor C”; }
};
int main()
{ B b1;
C c1;
}
Hybrid Inheritance
• Hybrid Inheritance is implemented by combining more
than one type of inheritance.
• For example: Combining Hierarchical inheritance and
Multiple Inheritance.
class A
{ public:
A( ) { cout<<“Constructor A”; }
};
class B
{ public:
B( ) { cout<<“Constructor B”; }
int main()
};
{
class C: public A C c1;
{ public: D d1;
C( ) { cout<<“Constructor C”; } }
};
Class D: public A, public B
{ public:
D( ) { cout<<“Constructor D”; }
Multipath Inheritance
A derived class with two base classes and these two base
classes have one common base class is called multipath
inheritance.
Ambiguity in
multipath
inheritance
Ambiguity in multipath
inheritance
Ways to solve this ambiguity :
Syntax 1: Syntax 2:
}; };
class A
{
public: int a; int main()
{
};
class B: virtual public A D d1;
{
public: int b; d1.a = 10;
}; d1.b = 20;
d1.c = 30;
Class C: virtual public A
d1.d = 40;
{ cout<<“a = “<<d1.a;
public: int c; cout<<“b = “<<d1.b;
}; cout<<“c = “<<d1.c;
Class D: public B, public C cout<<“d = “<<d1.d;
}
{
public: int d;
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.
• Return type and parameters are different but
function name remains same.
#include<iostream>
using namespace std;
class A
{
int a=10;
public:
int main()
void display()
{
{ A a1;
cout<<a<<endl; a1.display();
} a1.display(20);
void display(int b) return 0;
}
{
cout<<b<<endl;
}
};
Function overriding
• Function overriding is a feature that allows us to
have a same function in derived class which is
already present in the base class.
• It is like creating a new version of an old function, in
the child class.
• When you call the function which is present in both
base and child class, child class function is invoked.
• The function of base class is ignored.
class Base
{ public:
void display() //overridden function
{ cout<<“Function in base class”;}
};
class Derived: public Base
{ public:
void display() //overriding function
{ cout<<“Function in derived class;}
};
int main()
{ Derived d;
d.display(); }
Access overridden function
• To access the overridden function of the base class,
we use the scope resolution operator ::
cout << "Value of pointer variable 1 : " << *ptr1 << endl;
cout << "Value of pointer variable 2 : " << *ptr2 << endl;
delete ptr1;
delete ptr2;
return 0;
}
Pointers to Objects
• Pointers to objects aim to make a pointer that can
access the object, not the variables.
• Pointer to object in C++ refers to accessing an
object.
• It stores the address of an object.
• There are two approaches by which you can
access members of class.
• One is using simple object and the other is by
using a pointer to an object in C++.
Pointers to Objects
• Syntax:
classname objectname;
classname *pointer_to_object;
pointer_to_object = &objectname;
return 0;
}
Pointer to Derived Class
int *ptr;
int arr[5];
ptr = arr; // store the address of the first
// element of arr in ptr
OR
int *ptr;
int arr[3]={1,2,3};
ptr = &arr[0];
cout<<*ptr;
Accessing Arrays using pointers
Array of pointer
• An array of pointers is an array of pointer variables.
• We can declare a pointer that can point to whole array
instead of only one element of an array.
• Syntax:
dataType (*pointer_name) [size];
• Example:
int (*ptr)[3]; // array of pointers
int a[3]={1,2,3}; // array of integer
ptr = &a; // ptr points to whole array a
cout<<*(ptr[0]); // 1
Function pointer
Syntax:
• Declaring
return_type (*FuncPtr) (parameter type, ....);
• Referencing
FuncPtr = function_name;
• Dereferencing
data_type x = *FuncPtr;
Function pointer used to call the
function
#include <iostream>
using namespace std;
int multiply(int a, int b) { return a * b; }
int main()
{
int (*fp)(int, int);
fp = multiply;
int product = fp(15, 2);
cout << "The value of the product is: " << product;
return 0;
}
Pointer to Pointer
• Syntax:
• Example:
int var = 10;
int *ptr1 = &var;
int **ptr2 = &ptr1;
Pointer to Pointer
Passing parameters to Function
1. Pass by value
2. Pass by reference
3. Pass by Pointer
Pass by Value
• Example:
int * MyFunction() { statements; }
Return pointer from function
int global_var = 25;
int * MyFunction() { return &global_var; }
int main ()
{
int a=20;
int *p = &a;
cout<<“Before: “<<*p<<"\n";
p = MyFunction();
cout<<“After: “<<*p<<"\n";
return 0;
}
Null Pointer
• The NULL is a constant with a value of zero
defined in several standard libraries, including
iostream.
• Assign the value NULL to a pointer variable in
case you do not have exact address to be assigned.
• This is done at the time of variable declaration.
• A pointer that is assigned NULL is called
a null pointer.
Null Pointer
#include <iostream>
using namespace std;
int main ()
{
int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
Void Pointer