25 Jan 2014

Basics c++ practice inheritance set 1






Question 1 Mahindra Satyam

    #include <iostream>

    using namespace std;

    class Base

    {

        public:

        virtual void print() const = 0;

    };

    class DerivedOne : public Base

    {

        public:    

        void print() const

        {

            cout << "DerivedOne\n";

        }

    };

    class DerivedTwo : public Base

    {

        public:

        void print() const

        {

            cout << "DerivedTwo\n";

        }    

    };

    class Multiple : public DerivedOne, public DerivedTwo

    {

        public:

        void print() const

        {

            DerivedTwo :: print();

        }

    };

    int main()

    {

        int i;

        Multiple both;

        DerivedOne one;

        DerivedTwo two;

        Base *array[ 3 ];

        array[ 0 ] = &both;

        array[ 1 ] = &one;

        array[ 2 ] = &two;

        array[ i ] -> print();

        return 0;

    }
Solutions:In this program, ‘Base’ is an ambiguous base of ‘Multiple’. So it is producing an error. And this program is a virtual base class.




No comments:

Post a Comment