Virtual function

Discussion in 'C++' started by bintom, Apr 14, 2012.

  1. bintom

    bintom Guest

    In the first program, given below, I have to make "father" and
    "mother" virtual. However, in the second program, even without
    declaring the classes virtual, the program works. Why?

    //
    ***********************************************************************************
    // 1st Program

    class person
    { char name[21]; int age;
    public : void init(char *n, int a) { strcpy(name, n); age = a; }
    char* get_name() { return name; }
    int get_age() { return age; }
    };

    class father : virtual public person
    { char sport[11];
    public : void init(char *n, int a, char *sp)
    { person :: init(n, a);
    strcpy(sport, sp);
    }
    char* get_sport() { return sport; }
    };

    class mother : virtual public person
    { char pasttime[16];
    public : void init(char *n, int a, char *p)
    { person :: init(n, a);
    strcpy(pasttime, p);
    }
    char* get_pasttime() { return pasttime; }
    };

    class child : public father, public mother
    { char school[21];
    public : void init(char *n, int a, char *sp, char *p, char *sc)
    { father :: init(n, a, sp);
    mother :: init(n, a, p);
    strcpy(school, sc);
    }
    char* get_school()
    { return school; }
    };

    int main()
    { person p; p.init("Jack", 23);
    cout << p.get_name() << " " << p.get_age() << endl;

    father f; f.init("Papa", 45, "Cricket");
    cout << f.get_name() << " " << f.get_age() << " " << f.get_sport()
    << endl;

    mother m; m.init("Mama", 42, "Gardening");
    cout << m.get_name() << " " << m.get_age() << " " <<
    m.get_pasttime() << endl;

    child c; c.init("Kiddo", 12, "Kabaddi", "Bird watching", "BCRS");
    cout << c.get_name() << " " << c.get_age() << " " << c.get_sport()
    << " "
    << c.get_pasttime() << " " << c.get_school() << endl;
    }




    //
    ***********************************************************************************
    // 2nd Program

    class Point
    { private: int x, y;
    public : void setX(int X) { x = X; }
    int getX() { return x; }
    void setY(int Y) { y = Y; }
    int getY() { return y; }
    void moveX(int i) { setX(getX() + i); }
    };

    class Circle : public Point
    { private: float radius;
    public : void setR(float R) { radius = R; }
    float getR() { return radius; }
    };

    class Square : public Point
    { private: float side;
    public : void setSide(float S) { side = S; }
    float getSide() { return side; }
    };

    class Shape : public Circle, public Square
    { private: int color;
    public : void setColor(int C) { color = C; }
    int getColor() { return color; }
    };

    void shiftY(Point &P, int i)
    { P.setY(P.getY() + i); }

    int main()
    { Square S1; S1.setX(9); S1.setY(4); S1.setSide(5.85);
    cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() <<
    "\n";
    S1.moveX(11);
    cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() <<
    "\n";
    shiftY(S1, 3);
    cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() << "\n
    \n";
    }
    bintom, Apr 14, 2012
    #1
    1. Advertising

  2. bintom <> writes:

    > In the first program, given below, I have to make "father" and
    > "mother" virtual. However, in the second program, even without
    > declaring the classes virtual, the program works. Why?


    The second program does not declare any object of type Shape.
    If you changed "Square S1" to "Shape S1", then the S1.setX(9)
    call would become ambiguous, as would many others.
    Kalle Olavi Niemitalo, Apr 14, 2012
    #2
    1. Advertising

  3. bintom

    bintom Guest

    Hi Kalle Olavi Niemitalo,

    Thanks a ton!!

    BinTom
    bintom, Apr 15, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    2
    Views:
    404
    Jack Klein
    Jun 21, 2004
  2. IK
    Replies:
    2
    Views:
    591
    hemraj
    Jul 23, 2004
  3. Ashwin
    Replies:
    2
    Views:
    336
    Pierre Barbier de Reuille
    Aug 1, 2006
  4. Replies:
    11
    Views:
    680
    James Kanze
    Sep 10, 2006
  5. Replies:
    7
    Views:
    577
    James Kanze
    May 2, 2007
Loading...

Share This Page