• Tidak ada hasil yang ditemukan

Lecture-28 CSPS103: Object Oriented Programming

N/A
N/A
Protected

Academic year: 2024

Membagikan "Lecture-28 CSPS103: Object Oriented Programming"

Copied!
12
0
0

Teks penuh

(1)

FACULTY OF ENGINEERING & TECHNOLOGY

Preeti Singh

Department of Computer Science & Engineering Rama University, Kanpur

Lecture-28

CSPS103: Object Oriented Programming

[email protected]

(2)

OBJECTIVES

In this lecture, you will learn to:

Virtual Function

Rules of Virtual Function

Virtual function Example

(3)

VIRTUAL FUNCTION

A virtual function is a member function that is declared within a base class and redefined by a derived class.

In order to make a function virtual, you have to add keyword virtual in front of a function definition.

When a class containing a virtual function is inherited, the derived class redefines the virtual function relative to the derived class.

The virtual function within the base class defines the form of the interface to that function.

Each redefinition of the virtual function by a derived class implements its operation as it relates specifically to the derived class.

That is, the redefinition creates a specific method.

When a virtual function is redefined by a derived class, the keyword virtual is not needed.

A virtual function can be called just like any member function.

However, what makes a virtual function interesting, and capable of supporting run-time polymorphism, is what happens when a virtual function is called through a pointer.

When a base pointer points to a derived object that contains a virtual function and that virtual function is called through that pointer, C++ determines which version of that function will be executed based upon the type of object being pointed to by the pointer. And this determination is made at run time.

Therefore, if two or more different classes are derived from a base class that contains a virtual function, then when different objects are pointed to by a base pointer, different versions of the virtual function are executed.

(4)

RULES OF VIRTUAL FUNCTION

Virtual functions must be members of some class.

Virtual functions cannot be static members.

They are accessed through object pointers.

They can be a friend of another class.

A virtual function must be defined in the base class, even though it is not used.

The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two

functions with the same name but different prototypes, C++ will consider them as the overloaded functions.

We cannot have a virtual constructor, but we can have a virtual destructor

Consider the situation when we don't use the virtual keyword.

(5)

VIRTUAL FUNCTION EXAMPLE

#include <iostream.h>

{

public:

virtual void display() {

cout << "Base class is invoked"<<endl;

} };

class B:public A {

public:

void display() {

cout << "Derived Class is invoked"<<endl;

} };

int main() {

A* a; //pointer of base class B b; //object of derived class a = &b;

a->display(); //Late Binding occurs }

(6)

REFERENCES

 Kernighan, Brian W., and Dennis M. Richie. The C Programming Language. Vol. 2. Englewood Cliffs: Prentice- Hall, 1988.

 King, Kim N., and Kim King. C programming: A Modern Approach. Norton, 1996.

 Bjrane Stroustrup, “C++ Programming language”,3rd edition, Pearson education Asia(1997)

 LaforeR.”Object oriented Programming in C++”,4th Ed. Techmedia,New Delhi(2002).

 YashwantKenetkar,”Let us C++”,1stEd.,Oxford University Press(2006)

 B.A. Forouzan and R.F. Gilberg,CompilerScience,”A structured approach using C++” Cengage Learning, New Delhi.

 https://www.javatpoint.com/cpp-tutorial

 https://www.tutorialspoint.com/cplusplus/index.htm

 https://ambedkarcollegevasai.com/wp-content/uploads/2019/03/CPP.pdf

 https://onlinecourses.nptel.ac.in/noc20_cs07/unit?unit=3&lesson=19

(7)

MULTIPLE CHOICE QUESTION

Multiple Choice Question:

Q1. Polymorphism means

a) Many forms b) Only one form c) Hiding data d) None of them

(8)

MULTIPLE CHOICE QUESTION

Multiple Choice Question:

Q2. How many types of polymorphism are there?

a) 1 b) 2 c) 3 d) 4

(9)

MULTIPLE CHOICE QUESTION

Multiple Choice Question:

Q3. What is the other name of compile-time polymorphism?

a) Static polymorphism b) Dynamic polymorphism c) Executing polymorphism d) Non-executing polymorphism

(10)

MULTIPLE CHOICE QUESTION

Multiple Choice Question:

Q4. What is the other name of run-time polymorphism?

a) Static polymorphism b) Dynamic polymorphism c) Executing polymorphism d) Non-executing polymorphism

(11)

MULTIPLE CHOICE QUESTION

Multiple Choice Question:

Q5. Which of the following is the scope resolution operator?

a) . b) * c) ::

d) ~

(12)

Summary

In this lecture, you learned that:

 A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.

 It is used to tell the compiler to perform dynamic linkage or late binding on the function.

Referensi

Dokumen terkait

Because the parameter is a reference to the base class, Output() accepts objects of either class as an argument and the appropriate version of the virtual function Volume() is

oriented language to implement a system using classes or objects, but the system must have all the following properties to be considered a true object-oriented system...

You will notice that the pointer to void named general is assigned an address of an int type in line 12 and the address of a float type in line 16 with no cast and no complaints

This version of the problem seeks to identify both the objective function coefficient vector and the constraint matrix of a linear programming problem that best fits a set of

Although this function looks like it might be called multiple times, a smart compiler can translate the Python code to a loop-based version that uses a fixed amount of memory for

We’re going to talk about a couple of the key language features that are related to functional programming: lambda expressions and method

This can then be used with a piece of scripted code to perform a specific action when that object is clicked, called the event handler..

The ADT Bundle is a downloadable zip file that contains a special version of Eclipse with the Android Development Tools plug-in, the Android Virtual Device Manager, the SDK Manager