Thread: Polymorphism in C++

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    12

    Polymorphism in C++

    Hi,

    Suppose I have the following base and derived classes:

    Code:
    class A
    {
    public:
    void funcA(...){...}
    };
    
    class B : public A
    {
    public:
    void funcB(...){...}
    };
    and I defined the following objects and pointers in the main function

    Code:
    A ObjA;
    B ObjB;
    
    A *pA;
    B *pB;
    which of the following statements are valid:

    Code:
    pA=&ObjB;
    pB=&ObjA;
    
    pA->funcA(...);
    pA->funcB(...);
    
    pB->funcA(...);
    pB->funcB(...);
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be better for you to make your examples a bit more concrete (e.g., instead of "..." have say, an empty parameter list and a function body that prints a message identifying the function called) such that you can write a program to find out. Of course, due to possible undefined behaviour (e.g., if you tried to use new/delete since your base class does not have virtual destructor) you might still not be sure, but that's when you can present us with your program and your research and ask for confirmation (e.g., this looks right, this is why I think it works, am I correct?) or explanation (why do I get this compile warning/error?).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    12
    Quote Originally Posted by laserlight View Post
    It would be better for you to make your examples a bit more concrete (e.g., instead of "..." have say, an empty parameter list and a function body that prints a message identifying the function called) such that you can write a program to find out. Of course, due to possible undefined behaviour (e.g., if you tried to use new/delete since your base class does not have virtual destructor) you might still not be sure, but that's when you can present us with your program and your research and ask for confirmation (e.g., this looks right, this is why I think it works, am I correct?) or explanation (why do I get this compile warning/error?).
    My question was more abstract. To put it in simple words: if I have a base class and a derived class from it. Then I defined a pointer that points to an object of the base class, could I access the members of the derived class using this pointer? (The answer turned out to be no from the following program I tried).

    With same logic, if I defined a pointer that points to an object of the derived class, can I access the members of the base class using this pointer? (The answer turned out to be yes from the following program I tried).

    The program is:

    Code:
    #include<iostream>
    
    using namespace std;
    
    
    class CPolygon
    {
    protected:
        int height, width;
    public:
        void set_values(int a, int b) { height = a; width = b; }
    };
    
    
    class CRectangle:public CPolygon
    {
    public:
        int area() { return height*width; }
    };
    
    
    class CTriangle:public CPolygon
    {
    public:
        int area() { return height*width / 2; }
    };
    
    
    int main()
    {
        CRectangle rect1, rect2,*rectp;
        CTriangle tri1, tri2,*trip;
    
    
    
    
    
    
        CPolygon *polyp1, *polyp2;
        
        polyp1 = &rect1;
        polyp2 = &tri1;
    
    
        rectp = &rect2;
        trip = &tri2;
    
    
        polyp1->set_values(3, 4);
        polyp2->set_values(3, 4);
    
    
        rectp->set_values(6, 8);
        trip->set_values(6, 8);
    
    
        /*rect.set_values(3, 4);
        tri.set_values(3, 4);*/
    
    
        cout << "The area of the rectangle 1 is: " << rect1.area() << endl;
        cout << "The area of the triangle 1 is: " << tri1.area() << endl;
    
    
        //cout << "The area of the rectangle 1 is: " << polyp1->area() << endl;
        //cout << "The area of the triangle 1 is: " << polyp2->area() << endl;
    
    
        cout << "The area of the rectangle 2 is: " << rectp->area() << endl;
        cout << "The area of the triangle 2 is: " << trip->area() << endl;
    
    
        system("pause");
    
    
        return 0;
    
    
    }
    Any further comment on this is welcome.

    Thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CNewProg View Post
    Then I defined a pointer that points to an object of the base class, could I access the members of the derived class using this pointer? (The answer turned out to be no from the following program I tried).

    With same logic, if I defined a pointer that points to an object of the derived class, can I access the members of the base class using this pointer? (The answer turned out to be yes from the following program I tried).
    The answer to both questions are, yes. But for being able to access a derived member, you need to declare that derived function inside the base class and mark it as virtual. Remember that the point of polymorphism is that the caller does not need to actual type of the object, only that it has a certain interface. So you need to declare this interface in the base class. Otherwise there's no way to do unless you recast the base pointer to a derived pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2016
    Posts
    12
    Quote Originally Posted by Elysia View Post
    ... for being able to access a derived member, you need to declare that derived function inside the base class and mark it as virtual. ...
    That's right, when I declared it as virtual in the base class (as shown below) it worked, i.e., I could access the member functions of the derived classes.

    Code:
    virtual int area(){return 0;}
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Polymorphism
    By tinu73 in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2012, 08:50 AM
  2. Polymorphism
    By Eman in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2010, 06:51 AM
  3. Help with Polymorphism
    By sivapc in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2009, 05:39 AM
  4. polymorphism
    By slaveofthenet in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2003, 11:50 AM
  5. polymorphism
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2001, 12:13 AM

Tags for this Thread