Explain virtual functions, C/C++ Programming

Assignment Help:

Virtual Functions

The keyword virtual was previously used to resolve ambiguity for a class derived from two classes, both having a common ancestor. These classes are known as virtual base classes. This time it helps in implementing the idea of polymorphism with class inheritance. The function of the base class can be declared with the keyword virtual. The program with this change and its output is given below.

                class Shape

                 {

                  public :

virtual void print()

 {

                cout << " I am a Shape " << endl;

 }

                 };

                class Triangle : public Shape

                 {

                  public :

void print()

 {

                cout << " I am a Triangle " << endl;

 }

                 };

                class  Circle : public Shape

                 {

                  public :

void print()

 {

                cout << " I am a  Circle " << endl;

 }

                 };

 

void main()

 {

  Shape S;

  Triangle T;

  Circle C;

                S.print();

                T.print();

                C.print();

 

                  Shape *ptr;

 

                                ptr = &S;

                                ptr -> print();

 

                                ptr = &T;

                                ptr -> print();

 

ptr = &C;

                                ptr -> print();

}

 


Related Discussions:- Explain virtual functions

What are the differences among a struct in c & in c++?, A: In C++ a struct ...

A: In C++ a struct is alike to a class except for the default access specifier( refer to other question in the document). In C we ought to include the struct keyword while declarin

C program for function of merge , C Program for FUNCTION OF MERGE #inc...

C Program for FUNCTION OF MERGE #include conio.h> #include stdio.h> char cot(char a[],char b[]); void main() {           char a[50],b[50];           clrscr()

Explain nested classes, Nested Classes Many a times, it becomes essenti...

Nested Classes Many a times, it becomes essential to have a class contain properties of two other classes. One way is to explain a class within another - that is a class with m

C language, in a program for what purpose print f and scan f is used

in a program for what purpose print f and scan f is used

Arithmetic operation, a program that declares and prompts the user to enter...

a program that declares and prompts the user to enter two integer and performs the five arithmetic operations

Explanation of break statement, Problem 1. Explain Break and continue s...

Problem 1. Explain Break and continue statements in C++. Explanation of Break statement Explanation of Continue statement 2. Explain concepts of constructors and

Recursive procedure to computes the number of digits, (a) Write a recursive...

(a) Write a recursive procedure (digits n) that computes the number of digits in the integer n using a linear recursive process. For example, (digits 42) should return 2 and (digit

Program is to read the characters from keyboard, Program is to read the cha...

Program is to read the characters from keyboard: Program is to read the characters from keyboard one by one stored in different files as upper constants, lower constants, othe

Computes the area of a circle of radius r, (a) Write a procedure called (ar...

(a) Write a procedure called (area-of-rectangle h w) that computes the area of a rectangle of height h and width w. (b) Write a procedure called (area-of-circle r) that computes

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd