64,682
社区成员
发帖
与我相关
我的任务
分享
第3题:
#include <iostream.h>
int main()
{
int *P;//此处更改为int *p=new int;
*P=9;
cout < <”The value at P:” < <*P;
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Shape
{
public:
virtual void _draw() = 0;
virtual void _erase() = 0;
virtual ~Shape(){cout<<"~Shape()"<<endl;}
};
class ShapeFactory
{
public:
~ShapeFactory(){cout<<"~ShapeFactory()"<<endl;}
friend class ShapeFactoryInitializer;
class BadShapeCreation : public logic_error
{
public:
BadShapeCreation(string _type)
: logic_error("Can not create type:" + _type){}
};
static Shape* _CreateShape(const string& _id)
throw(BadShapeCreation)
{
if (factories.find(_id) != factories.end())
return factories[ _id ]->_Create();
else
throw BadShapeCreation(_id);
}
private:
virtual Shape* _Create() = 0;
static map<string,ShapeFactory*> factories;
};
map<string,ShapeFactory*> ShapeFactory::factories;
//////////////////////////////////////////////////////////////////////////
class Circle : public Shape
{
public:
void _draw() {cout<<"Circle::_draw()" <<endl;}
void _erase(){cout<<"Circle::_erase()"<<endl;}
~Circle(){cout<<"Circle::~Circle()"<<endl;}
private:
Circle(){}
friend class ShapeFactoryInitializer;
class Factory;
friend class Factory;
class Factory : public ShapeFactory
{
public:
Shape* _Create(){return new Circle;}
friend class ShapeFactoryInitializer;
};
};
//////////////////////////////////////////////////////////////////////////
class Square : public Shape
{
public:
void _draw() {cout<<"Square::_draw()" <<endl;}
void _erase(){cout<<"Square::_erase()"<<endl;}
~Square(){cout<<"Square::~Square()"<<endl;}
private:
Square(){}
friend class ShapeFactoryInitializer;
class Factory;
friend class Factory;
class Factory : public ShapeFactory
{
public:
Shape* _Create(){return new Square;}
friend class ShapeFactoryInitializer;
};
};
class ShapeFactoryInitializer
{
private:
static ShapeFactoryInitializer si;
ShapeFactoryInitializer()
{
ShapeFactory::factories[ "Circle" ] = new Circle::Factory;
ShapeFactory::factories[ "Square" ] = new Square::Factory;
}
~ShapeFactoryInitializer()
{
map<string,ShapeFactory*>::iterator it = ShapeFactory::factories.begin();
while (it != ShapeFactory::factories.end())
delete it ++->second;
}
};
ShapeFactoryInitializer ShapeFactoryInitializer::si;
//////////////////////////////////////////////////////////////////////////
char* s[] = {"Circle","Square","Circle","Square","Square","Circle","Square","Circle","Square","Square"};
int _tmain(int argc, char* argv[])
{
vector<Shape*> shapes;
try
{
for (int i = 0;i < sizeof(s) / sizeof(s[ 0 ]);i ++)
shapes.push_back(ShapeFactory::_CreateShape(s[ i ]));
}
catch (ShapeFactory::BadShapeCreation e)
{
cout<<e.what()<<endl;
return EXIT_FAILURE;
}
for (int i = 0;i < shapes.size();i ++)
{
shapes[ i ]->_draw();
shapes[ i ]->_erase();
}
for (vector<Shape*>::iterator _Pos = shapes.begin();_Pos != shapes.end();++ _Pos)
{
delete *_Pos;
*_Pos = NULL;
}
system("PAUSE");
return 0;
}
#include <stdio.h>
void main()
{
char c;
for (int i=32;i<=127;i++)
{
c=i;
printf("%c",c);
}
}