(written questions) about virtual functions and polymorphism of C + +

Source: Internet
Author: User

What is the output of the following two-segment program?

Program 1:
#include "stdio.h" class Base {public:     int Bar (char x)     {         return (int) (x);     }     virtual int bar (int x)     {         return (2 * x)}     }; class Derived:public Base {public:     int bar (char x)     { C10/>return (int) (-X);     }     int Bar (int x)     {         return (X/2);     }}; int main (void) {     Derived Obj;     Base *pobj = &Obj;     printf ("%d,", Pobj->bar ((char)));     printf ("%d,", Pobj->bar (100)); }
Analysis:

Answer: 100 50

Derived OBJ; Base *pobj = &obj;printf ("%d,", Pobj->bar ((char))) printf ("%d,", Pobj->bar (100)), and the first Bar (char) is a non-virtual function, Therefore, a static binding is a static binding that refers to the object when the pointer points to the declaration, when the pobj is declared as the base class, so the bar (char) of the base class is called the Second bar (char) is a virtual function, and therefore is dynamic bound, which refers to the pointer to the referenced object. Pobj refers to the derived object, so the derived class's Bar (int) Program 2 is called:
#include <iostream>using namespace Std;class animal{protected:    int age;public:    virtual void Print_age ( void) = 0;}; Class Dog:public Animal{public:    dog () {this->age = 2;}    ~dog () {}    virtual void print_age (void) {cout << "Wang. My age= "<< this->age << Endl; }};class cat:public animal{public:    cat () {this->age = 1;}    ~cat () {}    virtual void print_age (void) {cout << "miao,my age=" << this->age << Endl;}}; int main (void) {    cat kitty;    dog JD;    Animal *PA;    int *p = (int *) (&kitty);    int *q = (int *) (&JD);    cout << p[1] << Endl;    cout << q[1] << Endl;    P[1] = q[1];    PA = &kitty;    Pa->print_age ();    return 0;}
Analysis:

Answer: Miao, my age =2

Int*p = (int*) (&kitty); int*q = (int*) (&AMP;JD);p and Q are the first addresses of two objects, respectively, for Kitty and JD, because both the class cat and the dog contain virtual functions. So Kitty and JD two objects all contain a virtual function table, and through a pointer to it, p[0] and q[0] is the virtual function table pointer, and p[1] and q[1] is the object's data member is the value of age, p[1]=1,q[1]=2 p[1] = q[1]=2; The age of Kitty is modified to 2,animal *PA;PA = &kitty;pa->print_age ();p A pointer declaration when the type is a base class animal, which points to the derived class Kitty object, a typical polymorphic property, The PA static type is animal, and the dynamic type is cat. and Print_age () is a virtual function, so it is dynamic bound, dynamic binding points to a member of the dynamic type, so call the member function of Kitty Print_age (), cout << "Miao,my age=" << this-> Age << Endl; age=2 at this time.

(written questions) about virtual functions and polymorphism of C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.