0% found this document useful (0 votes)
3 views44 pages

C++ Unit 3

The document provides a comprehensive overview of inheritance in C++, detailing its definition, types (single, multiple, hierarchical, multilevel, and hybrid), and advantages such as reusability and maintainability. It explains the concepts of base and derived classes, along with visibility modes and the implications of public, protected, and private inheritance. Additionally, it addresses issues like ambiguity in multiple inheritance and the use of virtual base classes to prevent multiple instances of a class in an inheritance hierarchy.

Uploaded by

tamilselvi.k.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views44 pages

C++ Unit 3

The document provides a comprehensive overview of inheritance in C++, detailing its definition, types (single, multiple, hierarchical, multilevel, and hybrid), and advantages such as reusability and maintainability. It explains the concepts of base and derived classes, along with visibility modes and the implications of public, protected, and private inheritance. Additionally, it addresses issues like ambiguity in multiple inheritance and the use of virtual base classes to prevent multiple instances of a class in an inheritance hierarchy.

Uploaded by

tamilselvi.k.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Unit – III

2 marks
1. What is an Inheritance in C++?
• Inheritance is one of the key features of object-oriented programming in C++.
• It allows us to create a new class (derived class) from an existing class (base class).
• The derived class inherits the features from the base class and can have additional
features of its own.
• Derived class (child) - The class that inherits from another class
• Base class (parent) - The class being inherited from

2. Define Base Class.


Base class:
• A base class is a class in object-oriented programming language, from which other
classes are derived.
• The class which inherits the base class has all members of a base class as well as can also
have some additional properties.
• The base class members and member functions are inherited to object of the derived
class.
• A base class is also called parent class or superclass.
Syntax:
class base_classname
{

};

3. Define Derived Class.


Derived class:
A class that is created from an existing class.
The derived class inherits all members and member functions of a base class.
The derived class can have more functionality with respect to the base class and can easily
access the base class.
A derived class is also called a child class or subclass.
Syntax:
class child_class : visibility_mode parent_class

//class definition of the child class

};

4. List out the advantage of Inheritance?


• Reusability: Using the concept of inheritance, the programmer can create as many
derived classes form the base class as needed while adding specific features to each
derived class as needed.
• save time and effort: The above concept of reusability achieved by inheritance saves the
programmer time and effort. Since the main code written can be reused in various
situation as needed
• Data hiding: The base class can decide to keep some data private so that it cannot be
altered by the derived class
• Extensibility: It extensibility the base class logic as per business logic of the derived
class
• Easy to understand: It is easy to understand large program with use of inheritance
• Reliability: Increases program structure which result in greater reliability
• Maintainability: It is easy to debug a program when divided in parts.
Inheritance provides an opportunity to capture the program

5. Outline Types of Inheritance.


● Single inheritance
● Multiple inheritance
● Hierarchical inheritance
● Multilevel inheritance
● Hybrid inheritance
Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.

Where 'A' is the base class, and 'B' is the derived class.

Syntax:

Syntax:

class A

... .. ...

};

class B: public A

... .. ...

};
Here, class B is derived from the base class A.

Example:

#include <iostream>

using namespace std;

class A

public:

void displayA ()

cout<<"I am Base class A content.\n";

};

// sub class derived from base class

class B : public A

public:

void displayB ()

cout<<"I am Derived class B content.";

};

int main ()

{
B obj;

obj.displayA();

obj.displayB();

return 0;

OutPut:

I am Base class A content.


I am Derived class B content.

Multilevel Inheritance
If a class is derived from another derived class, then it is called multilevel inheritance.

So in C++ multilevel inheritance, a class has more than one parent class.

Multilevel inheritance is a process of deriving a class from another derived class.

As shown in above block diagram, class C has class B and class A as parent classes. Depending
on the relation the level of inheritance can be extended to any level.

Syntax:

class A // base class

{
...........

};

class B : acess_specifier A // derived class

...........

};

class C : access_specifier B // derived from derived class B

...........

};

Example:

#include <iostream>

using namespace std;

class A

public:

void displayA ()

cout<<"I am Base class A content.\n";

};
class B : public A

public:

void displayB ()

cout<<"I am Intermediate class B content.\n";

};

class C : public B

public:

void displayc ()

cout<<"I am Derived class C content.";

};

int main ()

C obj;

obj.displayA();

obj.displayB();

obj.displayc();

return 0;
}

Output:

I am Base class A content.

I am Intermediate class B content.

I am Derived class C content.

Example 2:

#include <iostream>

using namespace std;

class base

public:

int x;

void getdata()

cout << "Enter value of x= "; cin >> x;

};

class derive1 : public base // derived class from base class

public:

int y;

void readdata()
{

cout << "\nEnter value of y= "; cin >> y;

};

class derive2 : public derive1 // derived from class derive1

private:

int z;

public:

void indata()

cout << "\nEnter value of z= "; cin >> z;

void product()

cout << "\nProduct= " << x * y * z;

};

int main()

derive2 a; //object of derived class


a.getdata();

a.readdata();

a.indata();

a.product();

return 0;

Output

Enter value of x= 2

Enter value of y= 3

Enter value of z= 3

Product= 18

Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes from two
or more classes.

class C is derived from two base classes A and B.

Syntax:

class A
{

..........

};

class B

...........

};

class C : acess_specifier A,access_specifier A // derived class from A and B

...........

};

Example 1:

#include <iostream>

using namespace std;

class A

public:

void displayA ()

cout<<"I am Base class A content.\n";

};
class B

public:

void displayB ()

cout<<"I am Base class B content.\n";

};

class C : public A,public B

public:

void displayc ()

cout<<"I am Derived class C content.";

};

int main ()

C obj;

obj.displayA();

obj.displayB();

obj.displayc();

return 0;
}

Output:

I am Base class A content.

I am Base class B content.

I am Derived class C content.

Example 2:

#include

using namespace std;

class A

public:

int x;

void getx()

cout << "enter value of x: "; cin >> x;

};

class B

public:

int y;

void gety()
{

cout << "enter value of y: "; cin >> y;

};

class C : public A, public B //C is derived from class A and class B

public:

void sum()

cout << "Sum = " << x + y;

};

int main()

C obj1; //object of derived class C

obj1.getx();

obj1.gety();

obj1.sum();

return 0;

Output
enter value of x: 5

enter value of y: 4

Sum = 9

Ambiguity in Multiple Inheritance


The most obvious problem with multiple inheritance occurs during function overriding.

Suppose, two base classes have a same function which is not overridden in derived class.

If you try to call the function using the object of the derived class, compiler shows error. It's
because compiler doesn't know which function to call. For example,

class base1 {

public:

void someFunction( ) {....}

};

class base2 {

void someFunction( ) {....}

};

class derived : public base1, public base2 {};

int main() {

derived obj;

obj.someFunction() // Error!

This problem can be solved using the scope resolution function to specify which function to class
either base1or base2

int main() {

obj.base1::someFunction( ); // Function of base1 class is called


obj.base2::someFunction(); // Function of base2 class is called.

C++ Hierarchical Inheritance


● When several classes are derived from common base class it is called hierarchical
inheritance.

● In C++ hierarchical inheritance, the feature of the base class is inherited onto more than
one sub-class.

● Class B, C, D derived from class A.


● In C++ hierarchical inheritance all the derived classes have common base class.
● The base class includes all the features that are common to derived classes.

Syntax of Hierarchical inheritance:

class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D: public A
{
// body of class D.
}

Example:

#include <iostream>
using namespace std;

class A

public:

void displayA ()

cout<<"I am Base class A content.\n";

};

class B: public A

public:

void displayB ()

cout<<"I am Derived class B content.\n";

};

class C : public A

public:

void displayc ()

cout<<"I am Derived class C content.";


}

};

int main ()

B obj;

C obj1;

obj.displayA();

obj.displayB();

obj1.displayA();

obj1.displayc();

return 0;

Output:

I am Base class A content.

I am Derived class B content.

I am Base class A content.

I am Derived class C content.

Example 2:

#include <iostream>

using namespace std;

class A //single base class

public:
int x, y;

void getdata()

cout << "\nEnter value of x and y:\n"; cin >> x >> y;

};

class B : public A //B is derived from class base

public:

void product()

cout << "\nProduct= " << x * y;

};

class C : public A //C is also derived from class base

public:

void sum()

cout << "\nSum= " << x + y;

}
};

int main()

B obj1; //object of derived class B

C obj2; //object of derived class C

obj1.getdata();

obj1.product();

obj2.getdata();

obj2.sum();

return 0;

Output

Enter value of x and y:

Product= 6

Enter value of x and y:

Sum= 5

Explanation
In this example, there is only one base class A from which two class B and C are derived.
Both derived classes have their own members as well as base class members.

The product is calculated in the derived class B, whereas, the sum is calculated in the derived
class C but both use the values of x and y from the base class.

C++ Hybrid Inheritance


● The inheritance in which the derivation of a class involves more than one form of any
inheritance is called hybrid inheritance.

● Basically C++ hybrid inheritance is combination of two or more types of inheritance. It


can also be called multi path inheritance.

● Following block diagram highlights the concept of hybrid inheritance which


involves single and multiple inheritance.

● Above block diagram shows the hybrid combination of single inheritance and multiple
inheritance.

● Hybrid inheritance is used in a situation where we need to apply more than one
inheritance in a program.

Hybrid Inheritance Syntax


class A

.........

};

class B : public A
{

..........

};

class C

...........

};

class D : public B, public C

...........

};

As shown in block diagram class B is derived from class A which is single inheritance and then
Class D is inherited from B and class C which is multiple inheritance. So single inheritance and
multiple inheritance jointly results in hybrid inheritance.

C++ Hybrid Inheritance Example


Here is a simple program to illustrate the concept of hybrid inheritance in C++.

#include <iostream>

using namespace std;

class A

public:

int x;
};

class B : public A

public:

B() //constructor to initialize x in base class A

x = 10;

};

class C

public:

int y;

C() //constructor to initialize y

y = 4;

};

class D : public B, public C //D is derived from class B and class C

{
public:

void sum()

cout << "Sum= " << x + y;

};

int main()

D obj1; //object of derived class D

obj1.sum();

return 0;

} //end of program

Output

Sum= 14

Visibility modes

Visibility modes can be classified into three categories:

o Public: When the member is declared as public, it is accessible to all the functions of the
program.
o Private: When the member is declared as private, it is accessible within the class only.
o Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it.

Visibility of Inherited Members:

Base class visibility Derived class visibility

Public Private Protected

Private Not Inherited Not Inherited Not Inherited

Protected Protected Private Protected

Public Public Private Protected

class Base {

public:

int x;

protected:

int y;

private:

int z;

};

class PublicDerived: public Base {

// x is public

// y is protected

// z is not accessible from PublicDerived


};

class ProtectedDerived: protected Base {

// x is protected

// y is protected

// z is not accessible from ProtectedDerived

};

class PrivateDerived: private Base {

// x is private

// y is private

// z is not accessible from PrivateDerived

};

Example 1: C++ public Inheritance

// C++ program to demonstrate the working of public inheritance

#include <iostream>

using namespace std;

class Base {

private:

int pvt = 1;

protected:

int prot = 2;
public:

int pub = 3;

// function to access private member

int getPVT() {

return pvt;

};

class PublicDerived : public Base {

public:

// function to access protected member from Base

int getProt() {

return prot;

};

int main() {

PublicDerived object1;

cout << "Private = " << object1.getPVT() << endl;

cout << "Protected = " << object1.getProt() << endl;

cout << "Public = " << object1.pub << endl;

return 0;
}

Output

Private = 1

Protected = 2

Public = 3

Here, we have derived PublicDerived from Base in public mode.


As a result, in PublicDerived:
● prot is inherited as protected.
● pub and getPVT() are inherited as public.
● pvt is inaccessible since it is private in Base.
Since private and protected members are not accessible from main(), we need to create public
functions getPVT() and getProt() to access them:
// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;
// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;
Accessibility in protected Inheritance

private protected
Accessibility public members
members members

Base Class Yes Yes Yes

Derived Yes (inherited as protected


No Yes
Class variables)

Virtual Base Class


● Virtual base classes are used in virtual inheritance in a way of preventing multiple
“instances” of a given class appearing in an inheritance hierarchy when using multiple
inheritances.
● Need for Virtual Base Classes: Consider the situation where we have one class A. This
class A is inherited by two other classes B and C. Both these classes are inherited into
another in a new class D as shown in figure below.

● As we can see from the figure that data members/function of class A are inherited twice
to class D.
● One through class B and second through class C. When any data / function member of
class A is accessed by an object of class D, ambiguity arises as to which data/function
member would be called? One inherited through B or the other inherited through C. This
confuses compiler and it displays error.
Example:

#include <iostream>

using namespace std;

class A {

public:

void show()

cout << "Hello form A \n";

}
};

class B : public A {

};

class C : public A {

};

class D : public B, public C {

};

int main()

D object;

object.show();

Output:

Compile Errors:

prog.cpp: In function 'int main()':


prog.cpp:29:9: error: request for member 'show' is ambiguous
object.show();
^
prog.cpp:8:8: note: candidates are: void A::show()
void show()
^
prog.cpp: 8:8: note: void A::show()
How to resolve this issue?
To resolve this ambiguity when class A is inherited in both class B and class C, it is
declared as virtual base class by placing a keyword virtual as :
Syntax for Virtual Base Classes:
Syntax 1:
class B : virtual public A
{
};
Syntax 2:
class C : public virtual A
{
};
Example:
#include <iostream>
using namespace std;
class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
int main()
{
D object; // object creation of class d
cout << "a = " << object.a << endl;

return 0;
}
Output
a = 10
Explanation :
The class A has just one data member a which is public. This class is virtually
inherited in class B and class C. Now class B and class C use the virtual base class A
and no duplication of data member a is done; Classes B and C share a single copy of
the members in the virtual base class A.

Base class and derived class constructors


• Constructor is a member function of a class, whose name is same as the class name.
• Constructor is a special type of member function that is used to initialize the data
members for an object of a class automatically, when an object of the same class is
created.
• Constructor is invoked at the time of object creation.
● It constructs the values i.e. provides data for the object that is why it is known as
constructor.
• Constructor do not return value; hence they do not have a return type.
Characteristics of constructor

• The name of the constructor is same as its class name.


• Constructors are mostly declared in the public section of the class though it can be
declared in the private section of the class.
• Constructors do not return values; hence they do not have a return type.
• A constructor gets called automatically when we create the object of the class.
• Constructors can be overloaded.
• Constructor cannot be declared virtual.

Types of constructors

• Default constructor
• Parameterized constructor
• Overloaded constructor
• Constructor with default value
• Copy constructor
• Inline constructor
The prototype of the constructor looks like
<class-name> (list-of-parameters);
Constructor can be defined inside the class declaration or outside the class declaration
a. Syntax for defining the constructor within the class
<class-name>(list-of-parameters)
{
//constructor definition
}
b. Syntax for defining the constructor outside the class
<class-name>: :<class-name>(list-of-parameters)
{
//constructor definition
}

Base class and derived class constructors


class S: public A1, virtual A2
{
….
};

Here,
A2(): virtual base constructor
A1(): base constructor
S(): derived constructor
Example:
#include<iostream>
using namespace std;
class A1
{
public:
A1()
{
cout << "Constructor of the base class A1 \n";
}

};

class A2
{
public:
A2()
{
cout << "Constructor of the base class A2 \n";
}

};

class S: public A1, virtual A2


{
public:
S(): A1(), A2()
{
cout << "Constructor of the derived class S \n";
}
};

// Driver code
int main()
{
S obj;
return 0;
}
Output
Constructor of the base class A2
Constructor of the base class A1
Constructor of the derived class S

Run-time 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.
• Polymorphism is considered one of the important features of Object-Oriented
Programming.
Types of Polymorphism
● Compile-time Polymorphism
● Runtime Polymorphism

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.
Virtual Function in C++
• A virtual function (also known as virtual methods) is a member function that is
declared within a base class and is re-defined (overridden) by a derived class.
• 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 method.

•Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for the function call.
• They are mainly used to achieve Runtime polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
Rules for Virtual Functions
The rules for the virtual functions in C++ are as follows:

• Virtual functions cannot be static.


• A virtual function can be a friend function of another class.
• Virtual functions should be accessed using a pointer or reference of base class
type to achieve runtime polymorphism.
• The prototype of virtual functions should be the same in the base as well as the
derived class.
• They are always defined in the base class and overridden in a derived class. It is
not mandatory for the derived class to override (or re-define the virtual function),
in that case, the base class version of the function is used.
• A class may have a virtual destructor but it cannot have a virtual constructor.
• Compile time (early binding) VS runtime (late binding) behavior of Virtual
Functions
• Consider the following simple program showing the runtime behavior of virtual
functions.

Limitations of Virtual Functions


• Slower: The function call takes slightly longer due to the virtual mechanism and
makes it more difficult for the compiler to optimize because it does not know
exactly which function is going to be called at compile time.
• Difficult to Debug: In a complex system, virtual functions can make it a little
more difficult to figure out where a function is being called from.
Example:

// concept of Virtual Functions

#include <iostream>
using namespace std;

class base {
public:
virtual void print() { cout << "print base class\n"; }

void show() { cout << "show base class\n"; }


};
class derived : public base {
public:
void print() { cout << "print derived class\n"; }

void show() { cout << "show derived class\n"; }


};

int main()
{
base* bptr;
derived d;
bptr = &d;

// Virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

return 0;
}
Output
print derived class
show base class

Explanation: Runtime polymorphism is achieved only through a pointer (or reference)


of the base class type. Also, a base class pointer can point to the objects of the base class
as well as to the objects of the derived class. In the above code, the base class pointer
‘bptr’ contains the address of object ‘d’ of the derived class.
C++ Function Overriding
• The function in derived class overrides the function in base class.
• As we know, inheritance is a feature of OOP that allows us to create derived
classes from a base class.
• The derived classes inherit features of the base class.
• Suppose, the same function is defined in both the derived class and the based
class.
• Now if we call this function using the object of the derived class, the function of
the derived class is executed.

Example 1: C++ Function Overriding

#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1;
derived1.print();
return 0;
}
Output: Derived Function

• Here, the same function print() is defined in both Base and Derived classes.

• So, when we call print() from the Derived object derived1, the print() from
Derived is executed by overriding the function in Base.

As we can see, the function was overridden because we called the function from an object
of the Derived class.

Had we called the print() function from an object of the Base class, the function would
not have been overridden.

// Call function of Base class


Base base1;
base1.print(); // Output: Base Function

Access Overridden Function in C++


To access the overridden function of the base class, we use the scope resolution operator
::.

We can also access the overridden function by using a pointer of the base class to point to
an object of the derived class and then calling the function from that pointer.

Example 2: C++ Access Overridden Function to the Base Class


Example:
#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1, derived2;
derived1.print();

// access print() function of the Base class


derived2.Base::print();
return 0;
}

Output:

Derived Function
Base Function
Here, this statement

derived2.Base::print();
accesses the print() function of the Base class.

C++ Call Overridden Function Using Pointer

#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1;

// pointer of Base type that points to derived1


Base* ptr = &derived1;

// call function of Base class using ptr


ptr->print();

return 0;
}
Output

Base Function
In this program, we have created a pointer of Base type named ptr. This pointer points to
the Derived object derived1.

// pointer of Base type that points to derived1


Base* ptr = &derived1;
When we call the print() function using ptr, it calls the overridden function from Base.

// call function of Base class using ptr


ptr->print();

C++ Pure Virtual Functions


• If a function doesn't have any use in the base class
• But the function must be implemented by all its derived classes
Characteristics of a pure virtual function

• A pure virtual function is a "do nothing" function. Here "do nothing" means that it
just provides the template, and derived class implements the function.
• It can be considered as an empty function means that the pure virtual function
does not have any definition relative to the base class.
• Programmers need to redefine the pure virtual function in the derived class as it
has no definition in the base class.
• A class having pure virtual function cannot be used to create direct objects of its
own. It means that the class is containing any pure virtual function then we cannot
create the object of that class.
• This type of class is known as an abstract class.
Syntax

There are two ways of creating a virtual function:

• virtual void display() = 0;


or

• virtual void display() {}

Example:
#include <iostream>
using namespace std;

class Base {
public:
// pure virtual function
virtual void show() = 0;
};

class Derived : public Base


{
public:
// implementation of the pure virtual function
void show()
{
cout << "In Derived \n";
}
};

int main(void)
{
// creating a pointer of type Base pointing to an object of type Derived
Base* bp = new Derived();

// calling the show() function using the pointer


bp->show();

return 0;
}
Output: In Derived

Virtual function Pure virtual function

A virtual function is a member function in a A pure virtual function is a member function in a


base class that can be redefined in a derived base class whose declaration is provided in a base
class. class and implemented in a derived class.

The classes which are containing virtual The classes which are containing pure virtual
functions are not abstract classes. function are the abstract classes.

In case of a virtual function, definition of a In case of a pure virtual function, definition of a


function is provided in the base class. function is not provided in the base class.

The base class that contains a virtual The base class that contains a pure virtual function
function can be instantiated. becomes an abstract class, and that cannot be
instantiated.

If the derived class will not redefine the If the derived class does not define the pure virtual
virtual function of the base class, then there function; it will not throw any error but the derived
will be no effect on the compilation. class becomes an abstract class.

All the derived classes may or may not All the derived classes must define the pure virtual
redefine the virtual function. function.

Abstract base class


• A class is abstract if it has at least one pure virtual function.
• An abstract class is a class that can be used only as a base class of some other
class;
• No objects of an abstract class can be created except as subobjects of a class
derived from it.
• That's a virtual function declared by using the pure specifier (= 0) syntax.
Classes derived from the abstract class must implement the pure virtual
function or they, too, are abstract classes.
Restrictions on abstract classes
Abstract classes can't be used for:

• Variables or member data

• Argument types

• Function return types

• Types of explicit conversions

If the constructor for an abstract class calls a pure virtual function, either directly or indirectly,
the result is undefined.
However, constructors and destructors for abstract classes can call other member functions.

Example:
#include <iostream>
using namespace std;
class Class1
{
int a;
public:
virtual void func1() = 0;
void func2()
{
cout << "base class\n";
}
};
class Class2 : public Class1
{
public:
void func1()
{
cout << "func1 in derived class\n";
}
};
int main()
{
//Class1 b; //---------- > this line will cause an error
Class1 *b = new Class2();//pointer can be created, so this is correct
b -> func1();
b->func2();
}
Output:

func1 in derived class

base class

Code Explanation: Here, in the above function, Class1 is the base class, and as it has a pure
virtual function (func1), it has become an abstract class. Class2 is derived from the parent class
Class1. The func1 is defined in the derived class. In the main function, when we try to create an
object of type base class, we will get an error, as objects cannot be created for abstract class.
Whereas when we try to create a pointer of base class type, it will be created successfully, and
we can point it to the derived class. This pointer can be used to call the derived class function.

You might also like