Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews C Plus Plus Language Interviews:C++ Access ControlC++ COM ActiveXC++ ConstructorsC++ ContainersC++ Exception HandlingC++ FriendC++ InheritanceC++ Inline FunctionC++ New And DeleteC++ Operator OverloadingC++ Pointers & FunctionsC++ ProgrammerC++ ReferencesC++ Static DataC++ SyntaxC++ TemplateC++ Type CheckingC++ Virtual Functions
Copyright © 2018. All Rights Reserved
C++ Inheritance Interview Question:
Give example of pure virtual functions?
Submitted by: MurtazaA pure virtual function is a function which has no definition in the base class. Its definition lies only in the derived class ie it is compulsory for the derived class to provide definition of a pure virtual function. Since there is no definition in the base class, these functions can be equated to zero.
The general form of pure virtual function is:
virtual type func-name(parameter-list) = 0;
Consider following example of base class Shape and classes derived from it viz Circle, Rectangle, Triangle etc.
class Shape
{
int x, y;
public:
virtual void draw() = 0;
};
class Circle: public Shape
{
public:
draw()
{
//Code for drawing a circle
}
};
class Rectangle: public Shape
{
Public:
void draw()
{
//Code for drawing a rectangle
}
};
class Triangle: public Shape
{
Public:
void draw()
{
//Code for drawing a triangle
}
};
Thus, base class Shape has pure virtual function draw(); which is overridden by all the derived classes.
Submitted by: Murtaza
The general form of pure virtual function is:
virtual type func-name(parameter-list) = 0;
Consider following example of base class Shape and classes derived from it viz Circle, Rectangle, Triangle etc.
class Shape
{
int x, y;
public:
virtual void draw() = 0;
};
class Circle: public Shape
{
public:
draw()
{
//Code for drawing a circle
}
};
class Rectangle: public Shape
{
Public:
void draw()
{
//Code for drawing a rectangle
}
};
class Triangle: public Shape
{
Public:
void draw()
{
//Code for drawing a triangle
}
};
Thus, base class Shape has pure virtual function draw(); which is overridden by all the derived classes.
Submitted by: Murtaza
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.