What are the different forms of polymorphism??

Answer Posted / amit

There are two types of polymorphism:-
1.Compile time polymorphism
This is achieved by:
- Function overloading
- Operator overloading
2.Run time polymorphism
This is achieved through inheritance and virtual functions.
In this, base class has one or more virtual functions which
are overridden in the derived class. And then base class
pointer is used to access base or derived class virtual
function.

Example:-

class base {
public:
virtual void func() {
cout << "In base class" << endl;
}
};

class derived {
public:
void func() {
cout << "In derived class" << endl;
}
};

int main(){
base *bp, b;
derived d;

bp = &b;
bp->func(); // base class func() will be called

bp = &d;
bp->func(); // derived class func() will be called
return 0;
}

Here, the decision to call base or derived class func() is
taken at run time.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a null tree?

632


What does I oop mean?

618


#include #include #include #include void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

3254


What is the main feature of oop?

621


What are the 3 principles of oop?

619






Is html an oop?

582


What is data binding in oops?

588


what is the 3 types of system development life cycle

2435


Can we override main method?

607


What is a class and object?

600


write knight tour problem which is present in datastructure

2166


Why do we use oop?

605


write a code for this. serial_number contained in the header of the file will be read , if this serial number is less than a previous serial number within a successfully processed file, or is the same as another serial number within a successfully processed file, or if the field contains anything other than 7 digits, then the file must error with the reason ‘Invalid SERIAL_NUMBER’.

1780


What is object and example?

604


What are oops methods?

568