PUE Solution
PUE Solution
Section A
Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other
words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality. Object is a runtime entity, it is created at runtime
• The sequence diagram represents the UML, which is used to visualize the sequence of
calls in a system that is used to perform a specific functionality. The collaboration
diagram also comes under the UML representation which is used to visualize the
organization of the objects and their interaction.
• The sequence diagram are used to represent the sequence of messages that are flowing
from one object to another. The collaboration diagram are used to represent the
structural organization of the system and the messages that are sent and received.
• The sequence diagram is used when time sequence is main focus. The collaboration
diagram is used when object organization is main focus.
• The sequence diagrams are better suited of analysis activities. The collaboration
diagrams are better suited for depicting simpler interactions of the smaller number of
objects.
5. How abstraction is different from encapsulation? Distinguish
6. What are three models that are needs to combine to perform the operations on
classes? Discuss.
After analysis, we have the object, dynamic, and functional models, but the object
model is the main framework around which the design is constructed. The object
model from the analysis may not show operations. The designer must convert the
actions and activities of the dynamic model and the processes of the functional model
into operations attached to classes in the object model. In making this conversion, we
begin the process of mapping the logical structure of the analysis model into a
physical organization of a program.
7. Determine the C++ syntax and write a program to execute enumerated data type.
#include <iostream>
using namespace std;
int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
{
Test()
{
cout<< "Test of OOSD";
} };
int main() {
Test t;
return 0; }
There are three access specifiers: public - members are accessible from outside the
class. private - members cannot be accessed (or viewed) from outside the class.
protected - members cannot be accessed from outside the class, however, they can be
accessed in inherited classes.
Section B
Modeling is a central part of all the activities that lead up to the deployment of good
software. We build models to communicate the desired structure and behavior of our system.
We build models to visualize and control the system's architecture. We build models to better
understand the system we are building, often exposing opportunities for simplification and
reuse. And we build models to manage risk.
Importance of Modeling
Modeling is a proven & well accepted engineering techniques. In building architecture, we
develop architectural models of houses & high rises to help visualize the final products. In
Unified Modeling Language (UML), a model may be structural, emphasizing the
organization of the system or it may be behavioral, emphasizing the dynamics of the system.
A model is a simplification of reality, providing blueprints of a system. UML, in specific:
• Permits you to specify the structure or behavior of a system.
• Helps you visualize a system.
• Provides template that guides you in constructing a system.
• Helps to understand complex system part by part.
• Document the decisions that you have made.
We build model so that we can better understand the system we are developing. A model may
encompass an overview of the system under consideration, as well as a detailed planning for
system design, implementation and testing.
Principles of UML Modeling
1. The choice of model is important
The choice of what models to create has a profound influence on how a problem is attacked
and how a solution is shaped. We need to choose your models well.
• The right models will highlight the most critical development problems.
• Wrong models will mislead you, causing you to focus on irrelevant issues.
For Example: We can use different types of diagrams for different phases in software
development.
OR
In procedural programming, the program is divided into small parts called functions. In
object-oriented programming, the program is divided into small parts called objects.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way of hiding data so it is less secure.
Object-oriented programming provides data hiding so it is more secure.
In procedural programming, the function is more important than the data. In object-oriented
programming, data is more important than function.
Examples: C, FORTRAN, Pascal, Basic, etc.Examples: C++, Java, Python, C#, etc.
stem.
OR
Sketch the sequence diagram of food order sys
tem.
4. Explain the steps that the designer must perform during object design.
Object Design
After the hierarchy of subsystems has been developed, the objects in the system are
identified and their details are designed. Here, the designer details out the strategy
chosen during the system design. The emphasis shifts from application domain
concepts toward computer concepts. The objects identified during analysis are
etched out for implementation with an aim to minimize execution time, memory
consumption, and overall cost.
Object design includes the following phases −
• Object identification
• Object representation, i.e., construction of design models
• Classification of operations
• Algorithm design
• Design of relationships
• Implementation of control for external interactions
• Package classes and associations into modules
Object Identification
The first step of object design is object identification. The objects identified in the
object–oriented analysis phases are grouped into classes and refined so that they are
suitable for actual implementation.
The functions of this stage are −
• Identifying and refining the classes in each subsystem or package
• Defining the links and associations between the classes
• Designing the hierarchical associations among the classes, i.e., the
generalization/specialization and inheritances
• Designing aggregations
Object Representation
Once the classes are identified, they need to be represented using object modelling
techniques. This stage essentially involves constructing UML diagrams.
There are two types of design models that need to be produced −
• Static Models − To describe the static structure of a system using class
diagrams and object diagrams.
• Dynamic Models − To describe the dynamic structure of a system and
show the interaction between classes using interaction diagrams and
state–chart diagrams.
Classification of Operations
In this step, the operation to be performed on objects are defined by combining the
three models developed in the OOA phase, namely, object model, dynamic model,
and functional model. An operation specifies what is to be done and not how it should
be done.
The following tasks are performed regarding operations −
• The state transition diagram of each object in the system is developed.
• Operations are defined for the events received by the objects.
• Cases in which one event triggers other events in same or different
objects are identified.
• The sub–operations within the actions are identified.
• The main actions are expanded to data flow diagrams.
Algorithm Design
The operations in the objects are defined using algorithms. An algorithm is a stepwise
procedure that solves the problem laid down in an operation. Algorithms focus on
how it is to be done.
There may be more than one algorithm corresponding to a given operation. Once the
alternative algorithms are identified, the optimal algorithm is selected for the given
problem domain. The metrics for choosing the optimal algorithm are −
• Computational Complexity − Complexity determines the efficiency of
an algorithm in terms of computation time and memory requirements.
• Flexibility − Flexibility determines whether the chosen algorithm can be
implemented suitably, without loss of appropriateness in various
environments.
• Understandability − This determines whether the chosen algorithm is
easy to understand and implement.
Design of Relationships
The strategy to implement the relationships needs to be chalked out during the object
design phase. The main relationships that are addressed comprise of associations,
aggregations, and inheritances.
The designer should do the following regarding associations −
• Identify whether an association is unidirectional or bidirectional.
• Analyze the path of associations and update them if necessary.
• Implement the associations as a distinct object, in case of many–to-
many relationships; or as a link to other object in case of one–to-one or
one–to-many relationships.
Regarding inheritances, the designer should do the following −
• Adjust the classes and their associations.
• Identify abstract classes.
• Make provisions so that behaviors are shared when needed.
Implementation of Control
The object designer may incorporate refinements in the strategy of the state–chart
model. In system design, a basic strategy for realizing the dynamic model is made.
During object design, this strategy is aptly embellished for appropriate
implementation.
The approaches for implementation of the dynamic model are −
• Represent State as a Location within a Program − This is the
traditional procedure-driven approach whereby the location of control
defines the program state. A finite state machine can be implemented
as a program. A transition forms an input statement, the main control
path forms the sequence of instructions, the branches form the
conditions, and the backward paths form the loops or iterations.
• State Machine Engine − This approach directly represents a state
machine through a state machine engine class. This class executes the
state machine through a set of transitions and actions provided by the
application.
• Control as Concurrent Tasks − In this approach, an object is
implemented as a task in the programming language or the operating
system. Here, an event is implemented as an inter-task call. It preserves
inherent concurrency of real objects.
Packaging Classes
In any large project, meticulous partitioning of an implementation into modules or
packages is important. During object design, classes and objects are grouped into
packages to enable multiple groups to work cooperatively on a project.
The different aspects of packaging are −
• Hiding Internal Information from Outside View − It allows a class to
be viewed as a “black box” and permits class implementation to be
changed without requiring any clients of the class to modify code.
• Coherence of Elements − An element, such as a class, an operation, or
a module, is coherent if it is organized on a consistent plan and all its
parts are intrinsically related so that they serve a common goal.
• Construction of Physical Modules − The following guidelines help
However, this may pose update anomalies, i.e., a change in the values
of base attributes with no corresponding change in the values of the
derived attributes. while constructing physical modules −
o Classes in a module should represent similar things or
components in the same composite object.
o Closely connected classes should be in the same module.
o Unconnected or weakly connected classes should be
placed in separate modules.
o Modules should have good cohesion, i.e., high cooperation
among its components.
o A module should have low coupling with other modules,
i.e., interaction or interdependence between modules
should be minimum.
Design Optimization
The analysis model captures the logical information about the system, while the
design model adds details to support efficient information access. Before a design is
implemented, it should be optimized so as to make the implementation more efficient.
The aim of optimization is to minimize the cost in terms of time, space, and other
metrics.
However, design optimization should not be excess, as ease of implementation,
maintainability, and extensibility are also important concerns. It is often seen that a
perfectly optimized design is more efficient but less readable and reusable. So the
designer must strike a balance between the two.
The various things that may be done for design optimization are −
Optimization of Algorithms
In object-oriented systems, optimization of data structure and algorithms are done in
a collaborative manner. Once the class design is in place, the operations and the
algorithms need to be optimized.
Optimization of algorithms is obtained by −
Design Documentation
Documentation is an essential part of any software development process that
records the procedure of making the software. The design decisions need to be
documented for any non–trivial software system for transmitting the design to
others.
Usage Areas
Though a secondary product, a good documentation is indispensable, particularly in
the following areas −
OR
Describe the different features of object-oriented programming.
• Objects
• Abstraction
• Polymorphism
• Inheritance
• Encapsulation
In the upcoming sections, let’s see each object-oriented programming feature in detail.
Classes and Objects in OOPs
A class is a template that consists of the data members or variables and functions and defines
the properties and methods for a group of objects.
The compiler does not allocate memory whenever you define a class.
Example:
You can define a class called Vehicle. Its data fields can be vehicle_name, model_number,
color, date_of_manufacture, etc.
An object is nothing but an instance of a class. Each object has its values for the different
properties present in its class. The compiler allocates memory for each object.
Example:
The different objects of the class Vehicle can be Car, Bike, Bicycle, etc. Each of them will
have its values for the fields like color, model_number, etc.
Abstraction
The literal meaning of abstraction is to remove some characteristics from something to
reduce it to a smaller set. Similarly, Object Oriented Programming abstraction exposes only
the essential information of an object to the user and hides the other details.
In real life, like when you toggle a switch, it simply turns on or off the lights. Here, we only
know the functionality of the switch, but we don’t know its internal implementation, like how
it works.
How to implement abstraction?
You can implement abstraction using classes that group the data members and function
together. Inside classes, you can choose the access specifiers for its members to control how
they are visible to the outside world. We can also create header files containing the
implementations of all the necessary functions. So, you can include the header file and call
these functions without getting into their implementation.
Advantages of Abstraction
The advantages of abstraction are as follows:
• It enables code reuse by avoiding code duplication.
• Compile-time polymorphism
Example:
You can have some private variables in a class that you can't access outside the class for
security reasons. Now, to read or change the value of this variable, you can define public
functions in the class which will perform the read or writes operations.
OR
Compare and contrast the differences between Inline function and Macros
with examples.
1. Inline :
An inline function is a normal function that is defined by the inline keyword.
An inline function is a short function that is expanded by the compiler. And its
arguments are evaluated only once. An inline functions are the short length
functions that are automatically made the inline functions without using
the inline keyword inside the class.
Syntax of an Inline function:
inline return_type function_name ( parameters )
{
// inline function code
}
Example of an Inline function:
#include <iostream>
using namespace std;
// Inline function
inline int Maximum(int a, int b)
{
return (a > b) ? a : b;
}
return 0;
}
Output:
Max (100, 1000): 1000
Max (20, 0): 20
2. Macro :
It is also called preprocessors directive. The macros are defined by
the #define keyword. Before the program compilation, the preprocessor
examines the program whenever the preprocessor detects the macros then
preprocessor replaces the macro by the macro definition.
Syntax of Macro:
#define MACRO_NAME Macro_definition
Example of Macro:
#include <iostream>
using namespace std;
return 0;
}
Output:
Max (100, 1000):1000
Max (20, 0):20
6. Distinguish among different types of inheritance and write the C++ code to
execute multiple inheritance.
nheritance is a method through which one class inherits the properties from its
parent class. Inheritance is a feature in which one new class is derived from
the existing ones. The new class derived is termed a derived class, and the
current class is termed a Parent or base class. Inheritance is one of the most
essential features of Object Oriented Programming. Rather than defining new
data functions while creating a class, you can inherit the existing class's data
functions. The derived class takes over all the properties of the parent class
and adds some new features to itself. For example, using inheritance, you can
add the members' names and descriptions to a new class.
Child class: The class that inherits the characteristics of another class is known
as the child class or derived class. The number of child classes that can be
inherited from a single parent class is based upon the type of inheritance. A
child class will access the data members of the parent class according to the
visibility mode specified during the declaration of the child class.
Parent class: The class from which the child class inherits its properties is
called the parent class or base class. A single parent class can derive multiple
child classes (Hierarchical Inheritance) or multiple parent classes can inherit a
single base class (Multiple Inheritance). This depends on the different types of
inheritance in C++.
The syntax for defining the child class and parent class in all types of
Inheritance in C++ is given below:
class parent_class
};
Syntax Description
To create a parent class that is derived from the base class below is a syntax
that you should follow:
class
<derived_class_name> : <access-specifier>
<base_class_name>
{
/ /body
Public Inheritance: The public members of the base class remain public even
in the derived class; the same applies to the protected members.
Private Inheritance: This makes public members and protected members from
the base class to be protected in the derived class. The derived class has no
access to the base class's private members.
Protected Inheritance protects the Public and protected members from the
derived class's base class.
#include<iostream>
using namespace std;
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
int main()
{
C c;
return 0;
}
Output:
B's con
OR
Examine the output when implementing the friend function in C++ code.
Write the program.
If a function is defined as a friend function in C++, then the protected and private data of a
class can be accessed using the function.
By using the keyword friend compiler knows the given function is a friend function.
For accessing the data, the declaration of a friend function should be done inside the body of
a class starting with the keyword friend.
In the above declaration, the friend function is preceded by the keyword friend. The function
can be defined anywhere in the program like a normal C++ function. The function definition
does not use either the keyword friend or scope resolution operator.
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. private:
6. int length;
7. public:
8. Box(): length(0) { }
9. friend int printLength(Box); //friend function
10. };
11. int printLength(Box b)
12. {
13. b.length += 10;
14. return b.length;
15. }
16. int main()
17. {
18. Box b;
19. cout<<"Length of box: "<< printLength(b)<<endl;
20. return 0;
21. }
Section C
7. Sketch the conceptual model of the UML and also interpret with suitable
notations for each building blocks.
UML is composed of three main building blocks, i.e., things, relationships,
and diagrams. Building blocks generate one complete UML model diagram by
rotating around several different blocks. It plays an essential role in
developing UML diagrams. The basic UML building blocks are enlisted
below:
Things
Relationships
Diagrams
Things
Structural things
Behavioral things
Grouping things
Annotational things
Structural things
Nouns that depicts the static behavior of a model is termed as structural things.
They display the physical and conceptual components. They include class,
object, interface, node, collaboration, component, and a use case.
Class: A Class is a set of identical things that outlines the functionality and
properties of an object. It also represents the abstract class whose
functionalities are not defined. Its notation is as follows;
Pause
Next
Unmute
Current TimeÂ
0:00
DurationÂ
-:-
Loaded: 0%
Fullscreen
Play Video
UML-Building Blocks
UML-Building Blocks
UML-Building Blocks
Collaboration: It represents the interaction between things that is done to meet
the goal. It is symbolized as a dotted ellipse with its name written inside it.
UML-Building Blocks
Use case: Use case is the core concept of object-oriented modeling. It portrays
a set of actions executed by a system to achieve the goal.
UML-Building Blocks
Actor: It comes under the use case diagrams. It is an object that interacts with
the system, for example, a user.
UML-Building Blocks
UML-Building Blocks
UML-Building Blocks
Behavioral Things
They are the verbs that encompass the dynamic parts of a model. It depicts the
behavior of a system. They involve state machine, activity diagram,
interaction diagram, grouping things, annotation things
UML-Building Blocks
Activity Diagram: It portrays all the activities accomplished by different
entities of a system. It is represented the same as that of a state machine
diagram. It consists of an initial state, final state, a decision box, and an action
notation.
UML-Building Blocks
UML-Building Blocks
Grouping Things
It is a method that together binds the elements of the UML model. In UML,
the package is the only thing, which is used for grouping.
Package: Package is the only thing that is available for grouping behavioral
and structural things.
UML-Building Blocks
Annotation Things
Note: It is used to attach the constraints, comments, and rules to the elements
of the model. It is a kind of yellow sticky note.
UML-Building Blocks
Relationships
It illustrates the meaningful connections between things. It shows the
association between the entities and defines the functionality of an application.
There are four types of relationships given below:
UML-Building Blocks
Association: A set of links that associates the entities to the UML model. It
tells how many elements are actually taking part in forming that relationship.
UML-Building Blocks
UML-Building Blocks
UML-Building Blocks
Diagrams
The diagrams are the graphical implementation of the models that incorporate
symbols and text. Each symbol has a different meaning in the context of the
UML diagram. There are thirteen different types of UML diagrams that are
available in UML 2.0, such that each diagram has its own set of a symbol.
And each diagram manifests a different dimension, perspective, and view of
the system.
UML diagrams are classified into three categories that are given below:
Structural Diagram
Behavioral Diagram
Interaction Diagram
Class diagram
Object diagram
Package diagram
Component diagram
Deployment diagram
Timing diagram
Sequence diagram
Collaboration diagram
OR
Choose your models well. The right models will highlight the most nasty
development problems. Wrong models will mislead you, causing you to focus
on irrelevant issues.
Second principle of modeling:
In any case, the best kinds of models are those that let you choose your degree
of detail, depending on who is viewing it. An analyst or an end user will want
to focus on issues of what and a developer will want to focus on issues of how.
In the case of a building, you can study electrical plans in isolation, but you
can also see their mapping to the floor plan and perhaps even their interaction
with the routing of pipes in the plumbing plan.
Determine and discuss use case diagram and defend UML use case withan
example.
A use case diagram is used to represent the dynamic behavior of a system. It encapsulates the
system's functionality by incorporating use cases, actors, and their relationships. It models the
tasks, services, and functions required by a system/subsystem of an application. It depicts the
high-level functionality of a system and also tells how the user handles a system.
PauseNext
Unmute
Duration -:-
Loaded: 0%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
After that, we will enlist the actors that will interact with the system. The actors are the
person or a thing that invokes the functionality of a system. It may be a system or a private
entity, such that it requires an entity to be pertinent to the functionalities of the system to
which it is going to interact.
Once both the actors and use cases are enlisted, the relation between the actor and use case/
system is inspected. It identifies the no of times an actor communicates with the system.
Basically, an actor can interact multiple times with a use case or system at a particular
instance of time.
Following are some rules that must be followed while drawing a use case diagram:
1. A pertinent and meaningful name should be assigned to the actor or a use case of a
system.
2. The communication of an actor with a use case must be defined in an understandable
way.
3. Specified notations to be used as and when required.
4. The most significant interactions should be represented among the multiple no of
interactions between the use case and actors.
Here the Web Customer actor makes use of any online shopping website to purchase online.
The top-level uses are as follows; View Items, Make Purchase, Checkout, Client Register.
The View Items use case is utilized by the customer who searches and view products.
The Client Register use case allows the customer to register itself with the website for
availing gift vouchers, coupons, or getting a private sale invitation. It is to be noted that
the Checkout is an included use case, which is part of Making Purchase, and it is not
available by itself.
The View Items is further extended by several use cases such as; Search Items, Browse
Items, View Recommended Items, Add to Shopping Cart, Add to Wish list. All of these
extended use cases provide some functions to customers, which allows them to search for an
item. The View Items is further extended by several use cases such as; Search Items, Browse
Items, View Recommended Items, Add to Shopping Cart, Add to Wish list. All of these
extended use cases provide some functions to customers, which allows them to search for an
item.
Both View Recommended Item and Add to Wish List include the Customer
Authentication use case, as they necessitate authenticated customers, and simultaneously item
can be added to the shopping cart without any user authentication.
Similarly, the Checkout use case also includes the following use cases, as shown below. It
requires an authenticated Web Customer, which can be done by login page, user
authentication cookie ("Remember me"), or Single Sign-On (SSO). SSO needs an external
identity provider's participation, while Web site authentication service is utilized in all these
use cases.
The Checkout use case involves Payment use case that can be done either by the credit card
and external credit payment services or with PayPal.
Important tips for drawing a Use Case diagram
Following are some important tips that are to be kept in mind while drawing a use case
diagram:
Reusablity of components
Reusability of components across projects is
7 across projects is more as
less as compared to in OMT.
compared to SA/AD.
Used when functions are more important than Used when data is more
9
data. important than functions.
OR
1. System
2. Process
3. Technology
Phases of JSD:
JSD Steps:
Initially there were six steps when it was originally presented by Jackson, they
were as below:
Entity/action step
Modelling Step
Network Step
Implementation Step
Merits of JSD:
Demerits of JSD:
OR
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
Types of 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 that 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:
C++
// 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
Explanation: In the above example, a single function named
function func() acts differently in three different situations, which is a property
of polymorphism. To know more about this, you can refer to the article
– Function Overloading in C++.
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:
CPP
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
Output
12 + i9
Explanation: In the above example, the operator ‘+’ is overloaded. Usually,
this operator is used to add two numbers (integers or floating point numbers),
but here the operator is made to perform the addition of two imaginary or
complex numbers. To know more about this one, refer to the article – Operator
Overloading.
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.
Function overriding Explanation
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
// Driver code
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Output
print derived class
show base class
Runtime Polymorphism with Data Members
Runtime Polymorphism can be achieved by data members in C++. Let’s see an
example where we are accessing the field by reference variable which refers to
the instance of the derived class.
C++
// Driver code
int main(void)
{
Animal d = Dog(); // accessing the field by reference
// variable which refers to derived
cout << d.color;
}
Output
Black
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++
public:
// virtual function
virtual void display()
{
cout << "Called virtual Base Class function"
<< "\n\n";
}
void print()
{
cout << "Called GFG_Base print function"
<< "\n\n";
}
};
public:
void display()
{
cout << "Called GFG_Child Display Function"
<< "\n\n";
}
void print()
{
cout << "Called GFG_Child print Function"
<< "\n\n";
}
};
// Driver code
int main()
{
// Create a reference of class GFG_Base
GFG_Base* base;
GFG_Child child;
base = &child;
Output
Called virtual Base Class function