With suitable example, describe use of virtual function in polymorphism.

1 Answer

Answer :

In order to achieve polymorphism, objects belonging to different classes should be able to respond to the same message at different instances which initiates the use of single pointer variable to refer to objects of different classes.  In case of inheritance, base class pointer is used to refer to all derived objects but even when it contains an address of derived class always executes base class function.  The Compiler simply ignores content of pointer & selects member function that matches type of pointer. In order to achieve polymorphism, the concept of virtual functions is used.  When we use same function name in both base & derived classes, function in base class is declared as virtual using keyword virtual preceding its normal declaration.  When function is made virtual C++ determines which function to be executed at runtime based on type of object pointed to by base pointer rather than type of pointer.  Thus by making use of single pointer variable (base pointer) to point to different objects so that different version of virtual function can be executed and hence polymorphism is achieved known as Run Time Polymorphism.

Example:

#include<conio.h>

#include<iostream.h>

class student

{

int roll;

char name[20];

public:

virtual void accept()

{

cout<<"\n Enter Roll id:";

cin>>roll;

cout<<"\n Enter name:";

cin>>name;

}

virtual void display()

{

cout<<"\n Roll id:"<<roll<<endl;

cout<<"\n Name:"<<name<<endl;

}

};

class Test: public student

{

int marks1,marks2;

public:

void accept()

{

student::accept();

cout<<"\n Enter marks 1:";

cin>>marks1;

cout<<"\n Enter marks 2:";

cin>>marks2;

}

void display()

{

student:: display();

cout<<"Marks1="<<marks1<<endl;

cout<<"Marks2="<<marks2<<endl;

}

};

void main()

{

student *p;

Test b;

p=&b;

clrscr();

p->accept();

p->display();

getch();

}

Related questions

Description : Polymorphism is implemented using function overloading. Justify the statement.

Last Answer : Polymorphism is a mechanism that allows a developer to have more than one function with same name but different signature. In function overloading, one can make use of more than one function with different signature as well. Hence ... main()  { int a[10],i,isum; float b[5],fsum; clrscr(); cout

Description : Describe with example importance of virtual base class.

Last Answer : A virtual base class (Grandparent class) is a class that avoids duplication of inherited data in derived class (child class) derived from parent classes (parent1 and parent2) which in turn derived ... duplications class L is declare as virtual base class while defining base classes B1 and B. 

Description : How to define virtual function? Give example.

Last Answer : A virtual function is a member function that is declared within a base class and redefined by a derived class. To define virtual function following syntax is used: Class baseclass { virtual function_name() { } ... } }; Example: #include class Base { public: virtual void show( ) { cout

Description : What is the need of virtual function? Explain with example.

Last Answer : When base class and its derived class both contain same name and prototype member function then derived class function overrides base class function. Base class pointer is used to refer member functions of its ... Program/Example: #include class Base { public: virtual void show( ) { cout

Description : Explain the concept of virtual function with example.

Last Answer : Virtual Function: A virtual function is a member function that is declared within a base class and redefined by its derived class. When base class and its derived class both contain same name member function then ... pointer. Example: #include class Base { public: virtual void show( ) { cout

Description : What is polymorphism? Enlist different types of polymorphism. What are the differences between them?

Last Answer : Polymorphism: It is a feature of object oriented programming which allows a programmer to have a more than one function having same name but different /same parameters but performs different/ ... Compile Time polymorphism o Virtual Function Difference between Types of Polymorphism:

Description : Compare run-time and compile-time polymorphism.

Last Answer : Compare run-time and compile-time polymorphism.

Description : How to achieve compile time polymorphism explain in detail.

Last Answer : The process of linking of function call to function definition at compile time is called as Compile Time Polymorphism. It can be through Function and operator overloading. Function overloading: The process of defining the ... \n Concatenated String is:"; str1.display(); getch(); }  

Description : Define polymorphism. Enlist its types.

Last Answer : Definition: Polymorphism means ability to take more than one form that means a program can have more than one function with same name but different behavior. Types of polymorphism: 1) Compile time polymorphism 2) Runtime polymorphism 

Description : Write any four differences between compile time and run time polymorphism.

Last Answer : Sr. No. Compile-time Polymorphism Run-time Polymorphism 1 Compile time polymorphism means that an object is bound to its function call at compile time. Run time polymorphism means that selection ... binding. 7 E.g. overloaded function call. E.g. virtual function.

Description : Define the following terms: (i) Data abstraction (ii) Class (iii) Dynamic binding (iv) Polymorphism

Last Answer : (i) Data abstraction: Abstraction refers to the act of representing essential features without including the background details or explanation. Data abstraction is the process of defining a data type, often ... more than one form at different instances depending on the type or number of arguments. 

Description : With suitable example, describe use of this pointer.

Last Answer : 1. C++ uses a unique keyword called this to represent an object that invokes a member function. 2. This unique pointer is automatically passed to a member function when it is invoked. 3. this is a pointer that always ... ; public: void setdata(int x) { this ->a=x; } void putdata() { cout

Description : Describe the concept of constructor with default argument with suitable example.

Last Answer : Definition: The constructor where we can assign default values for one or more parameters at the time of function declaration is called as constructor with default argument Example: class complex ... and 3.0 to img., because the actual parameters, when specified overrides the default value.

Description : Explain object as a function argument using following points with suitable example: (i) Pass by value (ii) Pass by reference

Last Answer : (i) Pass by Value: When an object is passed by value to a function, a copy of that object is created and changes are reflected on the copy object not on original object. Example: #include #include class Example { int x; public: Example(int a)  {  x=a;  }  void print()  { cout

Description : Write a program to demonstrate the use of pure virtual function.

Last Answer : Consider the following example where parent class provides an interface to the base class to implement a function called getArea() as pure virtual function: #include // Base class class Shape { protected: int ... ; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout

Description : What is pure virtual functions? Write down the rules used for pure virtual function.

Last Answer : A pure virtual function is a function which is declared in a base class and which does not have definition relative to the base class. In such cases, the compiler requires each derived class to either ... A class containing pure virtual functions cannot be used to declare any objects of its own.  

Description : Describe syntax and use of defining member function outside class. Give one example.

Last Answer : Member function that is declared inside a class has to be defined separately outside the class. These member functions associate a membership identify label in the header. This label tells the ... ) A member function can call another member function directly, without using the dot operator. 

Description : With example, describe use of static member function.

Last Answer : A static member function can have access to only other static members (functions or variables) declared in the same class. A static member function can be called using the class name as follows: class_name:: ... static void showcount()----------------//static member function { cout

Description : Explain the concept of overloaded constructor in a class with suitable example.

Last Answer : Overloaded constructor: When more than one constructor function is defined in a same class then it is called as overloaded constructor. All constructors are defined with the same name as ... first constructor does not accept any argument and the second constructor accepts two integer arguments.

Description : Explain constructor in derived class with suitable example.

Last Answer : When a class is declared, a constructor can be declared inside the class to initialize data members. When a base class contains a constructor with one or more arguments then it is ... one argument. Derived class constructor accepts two values and passes one value to base class constructor.

Description : Explain single inheritance with suitable example.

Last Answer : When a single derived class is derived from only one base class then it is called as single inheritance. In a single inheritance, derived class can inherit some or all members of base class. It is implemented by specifying ... s; clrscr(); s.getdata(); s.putdata(); getch(); }

Description : Explain class with suitable example.

Last Answer : A class is a user defined data type which binds data and its associated functions together. It allows the data and functions to be hidden, if necessary from external use. Generally, a class ... for displaying their values. These functions provide the only access to data members of the class. 

Description : Explain destructor with suitable example. 

Last Answer : Destructor: 1. A destructor is a special member function whose task is to destroy the objects that have been created by constructor. 2. It does not construct the values for the data members of the class. 3. It is invoked ... main() { time t(2,43,56); t.display(); getch(); }

Description : Explain multiple constructors in a class with suitable example. 

Last Answer : Multiple constructors in a class means a class can contain more than one constructor. This is also known as constructor overloading. All constructors are defined with the same name as the class they belong to. All the ... default constructor integer(int a, int b) { m = a; n = b; cout

Description : What do you mean by default argument? Give its suitable example.

Last Answer : Default argument  Initializing an argument with a value while defining a constructor is referred as constructor with default value. When a constructor with default value is declared in a class, it does not require object to pass value ... void main() { ABC obj1(5); ABC obj2(20,30); }

Description : Write a program to implement the concept of virtual base class for following figure. Accept and display information of one employee with his name, code, basic pay, experience and gross salary with the object of employee class.

Last Answer : #include<iostream.h> #include<conio.h> class Master  {  char name[10],code[3];  public:  void acceptM() { cout<<"\nEnter name and code "; cin>>name>>code; ... .displayM(); e.displayA(); e.displayD(); e.displayE(); getch();  }

Description : State and describe use of pointer operator and address operator. Give one example of each. 

Last Answer : Pointer operator:- * It is used to declare a pointer variable. Also used as "value at" operator to read value stored inside the address pointed by pointer. Example: int *ptr; Address operator:-& ... address of a variable can be stored in pointer variable. Example: int a,*ptr; ptr=&a;

Description : Describe with example the use of insertion and extraction operators.

Last Answer : Insertion operator: The operator "" is called as extraction operator or get from extracts the value from keyboard and assigns it to the variable on its right. Extraction operator is used with cin statement to accept input from user (keyboard). Example: cin>>number1; 

Description : With example, describe use of scope resolution operator.

Last Answer : In C, the global version of a variable cannot be accessed from within the inner block. C++ resolves this problem by introducing a new operator:: called scope resolution operator. This can be used to uncover a hidden variable. It takes ... cout << ::m = <<::m; } return 0; }

Description : Describe multiple constructor by giving example.

Last Answer : Multiple constructor: Multiple constructors is a category of constructor in which a class can have more than one constructor. This is also known as constructor overloading. All constructors are defined with the same name as the class ... constructor 1 integer(int a, int b) { m = a; n = b; cout

Description : Describe constructor with syntax and example.

Last Answer : A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever an object of its associated class ... )//constructor declaration { a=0; } }; void main() { ABC x; }

Description : Describe pointer to object with an example. 

Last Answer : When address of an object of a class is stored into the pointer variable of the same class type then it is pointer to object. This pointer can be used to access the data member and member functions of same class. ... (); // Invoking getdata()using pointer to object p1. ptr->display(); }

Description : With example, describe multiple inheritance. 

Last Answer : A derived class with multiple base classes is called as multiple inheritance. A derived class inherits properties of all the base classes. It also can have its own properties. Syntax:- class base _class _name1 { ____ } ... clrscr(); derived d; d.get2(); d.put2(); getch(); }

Description : Explain the need of static member function with example.

Last Answer : A static member function can have access to only other static members (functions or variables) declared in the same class. A static member function can be called using the class name as follows: class_name:: ... test::showcount(); -------------Call to static member function. }

Description : Explain concept of function overriding with example.

Last Answer : Function Overriding:- When derived class defines same name function, as defined in its base class then it is called as function overriding. In this a function in the derived class overrides the inherited function. Example : #include #include class Base { public: void Display() { cout

Description : Explain friend function. Give example.

Last Answer : Friend function: The private members of a class cannot be accessed from outside the class but in some situations two classes may need access of each other s private data. So a common function can be declared which can be made friend of ... xyz x1; a1.get1(); x1.get1(); add(a1,x1); }

Description : List different types of inheritance with suitable diagram.

Last Answer : Types of inheritance: 1) Single inheritance: A derived class is derived from only one base class. Diagram: 2) Multiple inheritance: A derived class is derived from more ... ) Hybrid inheritance: Combination of single, multiple, multilevel and hierarchical inheritance.  Diagram:

Description : Write a program in C++ to implement following inheritance. Assume suitable data.

Last Answer : #include<iostream.h> #include<conio.h> class employee { int empid; char empname[20]; public: void accept() { cout<<"\n enter empid, empname:"<<endl; cin>>empid>> ... m.acc1(); m.dis1(); w.acc2(); w.dis2(); getch(); }

Description : Describe structure of C++ program.

Last Answer : General C++ program has following structure. INCLUDE HEADER FILES DECLARE CLASS DEFINE MEMBER FUNCTIONS DEFINE MAIN FUNCTION Description:-  1. Include header files In this section ... Functions This section the programmer creates object and call various functions writer within various class.

Description : State and describe visibility modes used in inheritance with their effects.

Last Answer : Visibility modes: private protected public Private: o When a base class is privately inherited by a derived class, public members and protected members of the base class become ... class Syntax: class derived: protected base { //Members of derived class; };

Description : State and describe access specifiers used inside class to declare members.

Last Answer : Access specifiers: 1. private 2. protected 3. public Private access specifier: Class members declared as private can be accessed only from within the class. Outside class access is not allowed for private members of ... { private: int a; protected: int b; public: void display() { cout

Description : State and describe types of inheritance.

Last Answer : Types of inheritance: o Single Inheritance o Multiple Inheritance o Multilevel Inheritance. o Hierarchical Inheritance o Hybrid Inheritance Single Inheritance o The mechanism of deriving a new class from existing single ... : public B, public C { // class D body; };

Description : What is constructor? How user can declared constructor in derived class? Explain with example.

Last Answer : Constructor:- Constructor is a special member function which has same name as a class name and is used to initialize object during compile time of program. Declaring constructor in derived class If a base class contains a ... derived d(2,5); d.displaybase(); d.display(); getch(); }

Description : What is structure? How user declare structure? Give example.

Last Answer : Structure is a collection of different data types written under a common name. It is a user defined data type. To Define a structure following Syntax is use:- struct structure_name { data_type variable 1; data_type ... () {  Person p1; cout>p1.name; cout> p1.age; cout> p1.salary; cout

Description : What is copy constructor? Explain with example.

Last Answer : Copy constructor: The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: ... ; // Copy constructor is called here // Let us access values assigned by constructors cout

Description : What is base class? What is derived class? Give example.

Last Answer : Base class: In inheritance a new class is derived from an old class. The old/existing class is referred as base class whose properties can be inherited by its derived class. Derived class: In inheritance a new ... as well as base class. Example: class base { public: void putb() { cout

Description : What is parametrized constructor? Explain with example.

Last Answer : A constructor that can take arguments is known as parameterized constructor. In some applications, it may be necessary to initialize the various data members of different objects with different values when they ... object is created. Member function put displays the value of data member m . 

Description : What is abstract class? Give one example of abstract class.

Last Answer : An abstract class is a class that is designed only to act as base class. It is not used to create objects. An abstract class is used to define an implementation and is intended to be inherited by child classes. Example: ... >a; } void display() { coutb; } void display1()  { display(); cout

Description : Explain structure with syntax and example.

Last Answer : Structure: The Structure is a user defined data supported by object oriented programming. It has almost similar properties that any other user defined data type possess except all members are public by default.  One can ... cout<<"\nThe Value is "<<d.a; getch();  }

Description : Explain hybrid inheritance with example.

Last Answer : Hybrid inheritance is also referred as mixed inheritances. As the name suggests it is a combination of all the kinds of inheritance mechanisms, namely single inheritance, multiple inheritance, multilevel inheritance and hierarchical inheritance. ... { D d; d.getdata(); d.putdata(); }