Thread: multy inheritence

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    multy inheritence

    hi

    I cant find a way to initialize or call the constructors of 2 base classes from a another one using inheritance

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    //Base class
    class Base{
    	public:
    	      virtual void print() = 0;
    };
    
    //derived 1
    class DerivadaUno:virtual public Base{
    	public:
    	    DerivadaUno(int n1){numeroUno = n1;}
    	   void imprimir(){cout << "DerivadaUno\n"
                                                       << numeroUno;}
    	private:
    	   int numeroUno;
    };
    
    //derived 2
    class DerivadaDos:virtual public Base{
    	public:
    	   DerivadaDos(int n2){numeroDos = n2;}
    	   void imprimir(){cout << "DerivadaDos\n" << 
                                                       << numeroDos;}
    	private:
    	   int numeroDos;
    };
    
    //class multy derived from derived 1 and 2
    class Multiple:public DerivadaUno, public DerivadaDos{
    	public:
    	    //cant initialize DerivadaUno and DerivaDaDos
                        //why and how?
                        Multiple(int nu1, int nu2)
    		:DerivadaUno(nu1), DerivadaDos(nu2){}
    		
    	   void imprimir(){DerivadaDos::imprimir();}
    };
    
    int main()
    {
    	Multiple ambas(25,40);
    	DerivadaUno uno;
    	DerivadaDos dos;
    
    	Base *arreglo[3];
    	arreglo[0] = &ambas; 
    	arreglo[1] = &uno; 
    	arreglo[2] = &dos; 
    	
    	for(int i = 0; i < 3; i++)
    		arreglo[i]->imprimir();
    
    	cout << endl;
    	return 0;
    }
    ok if you see i'm trying to call the explicit constructors but the compiler generates 3 error messages:

    thanks for any help and please excuse my poor english
    ----------------------------------------------------------------------------------
    main.cpp

    C:\programas c++\ejer22_14\main.cpp(47) : error C2664: '__thiscall Multiple::Multiple(const class Multiple &)' : cannot convert parameter 1 from 'const int' to 'const class Multiple &'
    Reason: cannot convert from 'const int' to 'const class Multiple'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\programas c++\ejer22_14\main.cpp(48) : error C2512: 'DerivadaUno' : no appropriate default constructor available
    C:\programas c++\ejer22_14\main.cpp(49) : error C2512: 'DerivadaDos' : no appropriate default constructor available
    Error executing cl.exe.

    ejer22_14.exe - 3 error(s), 0 warning(s)
    ----------------------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i think you must define print in all children of the Base class because it is a pure virtual class...

    does imprimir mean 'to print'?...if so, change it to 'print' or remove the = 0 in print() = 0 and define it to {cout << "Base class"; } or something....


    on a side note,
    constructor errors are tricky...whenever you get them tyr not to focus too much on the constructors themselves and think outside the box
    Last edited by misplaced; 10-19-2004 at 11:16 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You are calling the constructors for the base classes correctly.

    misplaced is right, your print function is called print in the base class and imprimir in the derived classes. Change it to imprimir in the base class.

    Also, you have an extra "<<" in DerivadaDos's imprimir function. Remove it.

    And finally, in main, you try to instantiate a new DerivadaUno and DerivadaDos but the only constructors for those require an integer. Either provide an integer for those objects, or add a new constructor for those two classes that doesn't require an integer. For any class, once you write a constructor that takes one or more arguments, you can no longer use the default constructor that takes no arguments.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    Smile

    thanks for reply misplaced and jlou

    and please sorry for posting my own post

    i must overload both constructors in derivedUno and derivedDos
    ok?

    Code:
    //derived1
    DerivadaUno(int n1):numeroUno(n1){}
    DerivadaUno():numeroUno(0){}
    
    //derived2
    DerivadaDos(int n2):numeroDos(n2){}
    DerivadaDos():numeroDos(0){}
    and yes imprimir = print

    sorry obut the print problem was my mistake not copying te code just retyping

    know the code works great
    Last edited by terracota; 10-19-2004 at 11:31 AM.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Another way to do it:
    Code:
    //derived1
    DerivadaUno(int n1 = 0):numeroUno(n1){}
    
    //derived2
    DerivadaDos(int n2 = 0):numeroDos(n2){}

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    thanks al lot
    looks better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes, inheritence, newbie questions
    By grasshopa in forum Windows Programming
    Replies: 0
    Last Post: 02-28-2006, 06:17 AM
  2. do i understand inheritence properly
    By Anddos in forum C++ Programming
    Replies: 8
    Last Post: 11-16-2005, 04:36 PM
  3. class inheritence with constructor parameters
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 06-20-2005, 06:49 PM
  4. Operator overloading and inheritence
    By ventolin in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 11:35 AM
  5. Multiple Inheritence
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 12-11-2002, 04:20 PM