En C++, voici ce que j'ai codé :
Les interface (classes virtuelles pures) C++ :
// Fruit.h: interface for the Fruit class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_FRUIT_H__8E01E81D_85AC_4033_97B0_D8E96E3ACD77__INCLUDED_)
#define AFX_FRUIT_H__8E01E81D_85AC_4033_97B0_D8E96E3ACD77__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Infos.h"
class Fruit
{
public:
Fruit() ;
virtual ~Fruit()= 0 ;
virtual Infos& getInfos()= 0 ;
};
#endif // !defined(AFX_FRUIT_H__8E01E81D_85AC_4033_97B0_D8E96E3ACD77__INCLUDED_)
// Infos.h: interface for the Infos class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_INFOS_H__D255DD66_031F_4F9A_BD12_10387F5DDD05__INCLUDED_)
#define AFX_INFOS_H__D255DD66_031F_4F9A_BD12_10387F5DDD05__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class Infos
{
public:
Infos();
virtual ~Infos()= 0;
virtual void print()= 0 ;
};
#endif // !defined(AFX_INFOS_H__D255DD66_031F_4F9A_BD12_10387F5DDD05__INCLUDED_)
L'implémentation C++ :
// Pomme.h: interface for the Pomme class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_POMME_H__DDE1E38C_7ABE_4593_AC41_BF3DE71C99E7__INCLUDED_)
#define AFX_POMME_H__DDE1E38C_7ABE_4593_AC41_BF3DE71C99E7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Fruit.h"
class Pomme : public Fruit
{
public:
Pomme();
virtual ~Pomme();
Infos& getInfos() ;
};
#endif // !defined(AFX_POMME_H__DDE1E38C_7ABE_4593_AC41_BF3DE71C99E7__INCLUDED_)
// Pomme.cpp: implementation of the Pomme class.
//
//////////////////////////////////////////////////////////////////////
#include "Pomme.h"
#include "InfosPomme.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Pomme::Pomme()
{
}
Pomme::~Pomme()
{
}
Infos& Pomme::getInfos()
{
Infos* i= new InfosPomme ;
return *i ;
}
// InfosPomme.h: interface for the InfosPomme class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_INFOSPOMME_H__8683B825_FD15_4520_99A3_2517DAF3D87E__INCLUDED_)
#define AFX_INFOSPOMME_H__8683B825_FD15_4520_99A3_2517DAF3D87E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Infos.h"
class InfosPomme : public Infos
{
public:
InfosPomme();
virtual ~InfosPomme();
void print() ;
};
#endif // !defined(AFX_INFOSPOMME_H__8683B825_FD15_4520_99A3_2517DAF3D87E__INCLUDED_)
// InfosPomme.cpp: implementation of the InfosPomme class.
//
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include "InfosPomme.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
InfosPomme::InfosPomme()
{
}
InfosPomme::~InfosPomme()
{
}
void InfosPomme::print()
{
std::cout << "infosPomme" << std::endl ;
}
Et le main C++ :
#include "Pomme.h"
#include "InfosPomme.h"
int main( int argc, char* argv[] )
{
Fruit* p= new Pomme ;
Infos i= p->getInfos() ;
i.print() ;
return 0 ;
}
mais ça ne fonctionne pas
Voici les erreurs de compilation :
Deleting intermediate files and output files for project 'TestCase - Win32 Debug'.
--------------------Configuration: TestCase - Win32 Debug--------------------
Compiling...
Fruit.cpp
Pomme.cpp
Infos.cpp
InfosPomme.cpp
main.cpp
c:\3dsmax6\maxsdk\samples\testcase\main.cpp(10) : error C2259: 'Infos' : cannot instantiate abstract class due to following members:
c:\3dsmax6\maxsdk\samples\testcase\infos.h(13) : see declaration of 'Infos'
c:\3dsmax6\maxsdk\samples\testcase\main.cpp(10) : warning C4259: '__thiscall Infos::~Infos(void)' : pure virtual function was not defined
c:\3dsmax6\maxsdk\samples\testcase\infos.h(16) : see declaration of 'Infos::~Infos'
c:\3dsmax6\maxsdk\samples\testcase\main.cpp(10) : warning C4259: 'void __thiscall Infos::print(void)' : pure virtual function was not defined
c:\3dsmax6\maxsdk\samples\testcase\infos.h(17) : see declaration of 'print'
c:\3dsmax6\maxsdk\samples\testcase\main.cpp(10) : error C2259: 'Infos' : cannot instantiate abstract class due to following members:
c:\3dsmax6\maxsdk\samples\testcase\infos.h(13) : see declaration of 'Infos'
c:\3dsmax6\maxsdk\samples\testcase\main.cpp(10) : warning C4259: '__thiscall Infos::~Infos(void)' : pure virtual function was not defined
c:\3dsmax6\maxsdk\samples\testcase\infos.h(16) : see declaration of 'Infos::~Infos'
c:\3dsmax6\maxsdk\samples\testcase\main.cpp(10) : warning C4259: 'void __thiscall Infos::print(void)' : pure virtual function was not defined
c:\3dsmax6\maxsdk\samples\testcase\infos.h(17) : see declaration of 'print'
Error executing cl.exe.
Creating browse info file...
TestCase.exe - 2 error(s), 4 warning(s)