C++ Polymorphism

This is a discussion on C++ Polymorphism within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get along with C++ and polymorpism. Therefore I created the following code example. When try to compile ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    C++ Polymorphism

    I'm trying to get along with C++ and polymorpism. Therefore I created the following code example. When try to compile i'm getting the following error message. Actually the code sample was Java exam and and now i try to translate it to C++.

    Code:
    $ g++ main.cpp -o main
    main.cpp: In member function `void A::func5(C*)':
    main.cpp:18: Fehler: invalid use of undefined type `struct C'
    main.cpp:6: Fehler: forward declaration of `struct C'
    Can anyone help me what's wrong with the example below. I guess it's related to forward declaration but don't know what's wrong. Any code improvements are appreciated as well.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    class C; // forward declaration
    
    
    class A
    {
        public:
            A()                  { cout << "A::A()"  << endl; }
            virtual ~A()         { cout << "A::~A()" << endl; }
    
    
            virtual void func1() { cout << "A::func1()"  << endl; }
            virtual void func2() { cout << "A::func2()"  << endl; }
            virtual void func3() { cout << "A::func3()"  << endl; }
            void func4(A* pA)    { cout << "A::func4()"  << endl; pA->func1(); }
            void func5(C* pC)    { cout << "A::func5()"  << endl; pC->func1(); }
    };
    
    
    class B : public A
    {
        public:
            B()                 { cout << "B::B()"     << endl; }
            ~B()                { cout << "B::~C()"    << endl; }
            void func1()        { cout << "B::func1()" << endl; }
            void func2(int* pI) { cout << "B::func2"   << endl; }
    };
    
    
    class C : public B
    {
        public:
            C()                  { cout << "C::C()"  << endl; }
            ~C()                 { cout << "C::~B()" << endl; }
    
    
            virtual void func1() { cout << "C::func1()"  << endl; }
            virtual void func2() { cout << "C::func2()"  << endl; }
            virtual void func3() { cout << "C::func3()"  << endl; }
            void func4()         { cout << "C::func4()"  << endl; }
    };
    
    
    int main()
    {
        A* ap = new C();
    
    
        cout << endl;
    
    
        ap->func1();
        ap->func2();
        ap->func3();
        ap->func4(ap);
        ((C*)ap)->func4();
        // static_cast<B*>(ap)->p()
    
    
        cout << endl;
    
    
        delete ap;
    
    
        return 0;
    }
    Commenting out the line "void func5(C* pC) ..." and the program works...

    Code:
    A::A()
    B::B()
    C::C()
    
    
    C::func1()
    C::func2()
    C::func3()
    A::func4()
    C::func1()
    C::func4()
    
    
    C::~B()
    B::~C()
    A::~A()

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Here is a simple example in classes forward declaration
    Forward declarations in C++

    The compiler does know that a class C is going to exist,but it does not know it's members,because it is an incomplete declaration of C,that later becomes complete.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Ordinarily you wouldn't encounter this problem because implementation of the member functions is located after the declaration of the class or in a separate source file.

    Additionally, the B and C destructors display lies about which one is being called. If anything I would fix that.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,502
    It isn't generally a good idea to have a base class which knows about its children. (Here, your A::func5)
    That makes the design somewhat brittle.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    I agree. As mentioned it's orignially form a Java exam, to confusue students if either A::func4 or A::func5 is called

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Thanks for your help! It works now, code is attached.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism in C
    By Avenger625 in forum C Programming
    Replies: 7
    Last Post: 03-16-2012, 07:01 AM
  2. Help with Polymorphism
    By sivapc in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2009, 05:39 AM
  3. Polymorphism Help
    By forseti42 in forum C++ Programming
    Replies: 22
    Last Post: 05-07-2008, 03:12 PM
  4. polymorphism
    By slaveofthenet in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2003, 12:50 PM
  5. polymorphism
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2001, 12:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21