c++ compile error
- by Niranjan
Hi,
I am trying to develop abstract design pattern code for one of my project as below.. But, I am not able to compile the code ..giving some compile errors(like "conversion from 'ProductA1 *' to 'ProductA *' exists, but is inaccessible" ).. Can any one please help me out in this...
#include "stdafx.h"
#include <iostream>
using namespace std;
class ProductA {
public:
virtual void Operation1()=0;
virtual void Operation2()=0;
};
class ProductA1 : ProductA {
public:
virtual void Operation1() {cout<<"PD ProductA1 Operation1"<<endl; }
virtual void Operation2() {cout<<"PD ProductA1 Operation2"<<endl; }
};
class ProductA2 : ProductA {
public:
virtual void Operation1()
{cout<<"DT ProductA2 Operation1"<<endl; }
virtual void Operation2()
{cout<<"DT ProductA2 Operation2"<<endl; }
};
//-------------------------------------------------------------
class ProductB {
public:
virtual void Operation3()=0;
virtual void Operation4()=0;
};
class ProductB1 : ProductB {
public:
void Operation3() { cout<<"PD ProductB1 Operation3"<<endl; }
void Operation4() { cout<<"PD ProductB1 Operation4"<<endl; }
};
class ProductB2 : ProductB
{
public:
void Operation3() { cout<<"DT ProductB2 Operation3"<<endl; }
void Operation4() { cout<<"DT ProductB2 Operation4"<<endl; }
};
//--------------- abstrct factory ---------------------------
class Factory
{
public:
virtual ProductA* CreateA () =0;
virtual ProductB* CreateB ()=0;
};
class Factory1 : Factory
{
public:
ProductA* CreateA () { return new ProductA1(); }
ProductB* CreateB () { return new ProductB1(); }
};
class Factory2 : Factory
{
public:
ProductA* CreateA () { return new ProductA2(); }
ProductB* CreateB () { return new ProductB2(); }
};
//--------------------- client --------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
Factory* pf = new Factory1();
ProductA *pa = pf->CreateA();
pa->Operation1();
pa->Operation2();
ProductB *pb = pf->CreateB();
pb->Operation3();
pb->Operation4();
return 0;
}