0% found this document useful (0 votes)
4 views7 pages

Oop Unit 5

The document discusses C++ pointers, explaining their role in simulating call-by-reference and managing dynamic data structures. It covers the concept of the 'this' pointer in member functions, polymorphism in C++, and the distinction between virtual and pure virtual functions. Additionally, it includes example programs and multiple-choice questions to reinforce understanding of these concepts.

Uploaded by

harsh.parmar
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)
4 views7 pages

Oop Unit 5

The document discusses C++ pointers, explaining their role in simulating call-by-reference and managing dynamic data structures. It covers the concept of the 'this' pointer in member functions, polymorphism in C++, and the distinction between virtual and pure virtual functions. Additionally, it includes example programs and multiple-choice questions to reinforce understanding of these concepts.

Uploaded by

harsh.parmar
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/ 7

UNIT-5 Polymorphism

C++ Pointers

Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to
create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the
main use of pointers.

The address of the variable you’re working with is assigned to the pointer variable that points to the same data type
(such as an int or string).

Syntax: datatype *var_name; int *ptr; // ptr can point to an address which

holds int data

How to use a pointer?

• Define a pointer variable

• Assigning the address of a variable to a pointer using the unary operator (&) which returns the address of that
variable.

• Accessing the value stored in the address using unary operator (*) which returns the value of the variable located
at the address specified by its operand.

// C++ program to illustrate Pointers


#include <bits/stdc++.h>

using namespace std;

void pointers()

{ int var = 20;

// declare pointer variable int* ptr;

// note that data type of ptr and var must be same

ptr = &var;

// assign the address of a variable to a pointer cout << "Value at

ptr = " << ptr << "\n"; cout << "Value at var = " << var << "\n";

cout << "Value at *ptr = " << *ptr << "\n";

// Driver program int main()

pointers(); return 0;

OUTPUT :

Value at ptr = 0x7ffe454c08cc

Value at var = 20

Value at *ptr = 20

This pointer :

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an
implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the
invoking object.

Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a
this pointer.
//Program #include
<iostream>
using namespace std; class
employee
{
int id;
string name; int salary;
public:
employee(int e_id,string e_name,int e_salary ){ this-
>id=e_id;
this->name=e_name; this-
>salary=e_salary;
}
void display(){
cout<<id<<name<<salary<<endl;
}
};
main(){
employee e1=employee(10,"john",25000);
e1.display();
}

Output
Polymorphism :

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a
hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.

// Program of runtime polymorphism

#include <iostream>

using namespace std;

class A {

public:

void disp(){

cout<<"Display in base class"<<endl;

};

class B: public A{

public:

void disp(){

cout<<"Display in derived class";

};

int main() {

//Parent class

object A obj;

obj.disp();

//Child class

object B obj2;

obj2.disp();
return 0;

Output

Virtual Function in C++

A virtual function is a member function which is declared within a base class and is
redefined(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 function.

Pure Virtual Functions in C++

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have an
implementation, we only declare it. A pure virtual function is declared by assigning 0 in the
declaration.
MCQs :

1. Which is also called as abstract class?

A. virtual function B. pure virtual function C. derived class D. base class

2. What is meant by pure virtual function?

a. Function which does not have definition of its own

b. Function which does have definition of its own

c. Function which does not have any return type

d. Function which does not have any return type & own definition

3. Where the abstract class does is used?

A. base class only B. derived class C. both derived & base class D. virtual class

4. Which of the following is the correct way to declare a pointer?

A. int *ptr B. int ptr C. int &ptr D. All of the above

5. Which is the pointer which denotes the object calling the member function?

A. Variable pointer B. This pointer C. Null pointer D. Zero pointer

6. The this pointer is accessible __________________

A. Within all the member functions of the class B. Only within functions returning void

C. Only within non-static functions D. Within the member functions with zero arguments

7. What is the use of this pointer?

A. When local variable’s name is same as member’s name, we can access member using this
pointer.

B. To return reference to the calling object

C. Can be used for chained function calls on an object

D. All of the above

8. This pointer can be used directly to ___________

A. To manipulate self-referential data structures

B. To manipulate any reference to pointers to member functions

C. To manipulate class references

D. To manipulate and disable any use of pointers


9. Which members can never be accessed in derived class from the base class?

A. Private

B. Protected

C. Public

D. All except private

10. Which of the following is true for pointer to derived class?

A. We can use pointers not only to a base objects but also to the objects of derived classes.

B. We cannot use pointers in the classes.

C. Using pointer in class is form of inheritance

D. All of above.

11. We can access those members of derived class which are inherited from base class by base

class pointer.

A. True

B. False

12. If same message is passed to objects of several different classes and all of those can respond in
a different way, what is this feature called?

A. Inheritance B. Overloading C. Polymorphism D. Overriding

QUESTIONS :
1. Define the following Keywords :
1) Polymorphism 2) This Pointer 3) Abstract Class 4) Pointer
2. Write a program to demonstrate working of ‘this’ pointer in C++.
3. How does pointer variable differ from simple variable?
4. Differentiate : Virtual Function and Pure Virtual Function
5. What is a polymorphism ? Explain and write a program in C++.
6. Define Pointer and Explain This pointer with Suitable Example.

You might also like