/*虚函数的简单应用*/
/*******************************Shape.h***********************************/
#if !defined(AFX_SHAPE_H__0DFF4B12_4075_4594_
#define AFX_SHAPE_H__0DFF4B12_4075_4594_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class Shape
{
public:
Shape();
virtual ~Shape();
virtual void Draw();
virtual const char* GetName();
};
#endif // !defined(AFX_SHAPE_H__0DFF4B12_4075_4594_
/******************************Shape.cpp**********************************/
#include "stdafx.h"
#include "Shape.h"
Shape::Shape()
{
}
Shape::~Shape()
{
}
void Shape::Draw()
{
}
const char* Shape::GetName()
{
return "图形";
}
/********************************Line.h***********************************/
#if !defined(AFX_LINE_H__F6440DD8_8342_
#define AFX_LINE_H__F6440DD8_8342_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Shape.h"
class Line : public Shape
{
public:
Line();
virtual ~Line();
virtual void Draw();
virtual const char* GetName();
};
#endif // !defined(AFX_LINE_H__F6440DD8_8342_
/*******************************Line.cpp**********************************/
#include "stdafx.h"
#include "Line.h"
#include <iostream.h>
Line::Line()
{
}
Line::~Line()
{
}
void Line::Draw()
{
cout << "Line" << endl;
}
const char* Line::GetName()
{
return "线条";
}
/*****************************Rect.h**************************************/
#if !defined(AFX_RECT_H__6CCC5CB0_6AE6_
#define AFX_RECT_H__6CCC5CB0_6AE6_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Shape.h"
class Rect : public Shape
{
public:
Rect();
virtual ~Rect();
virtual void Draw();
virtual const char* GetName();
};
#endif // !defined(AFX_RECT_H__6CCC5CB0_6AE6_
/*******************************Rect.cpp**********************************/
#include "stdafx.h"
#include "Rect.h"
#include <iostream.h>
Rect::Rect()
{
}
Rect::~Rect()
{
}
void Rect::Draw()
{
cout << "Rect" << endl;
}
const char* Rect::GetName()
{
return "矩形";
}
/*******************************OutDevice.h*******************************/
#if !defined(AFX_OUTDEVICE_H__2FCF39EB_
#define AFX_OUTDEVICE_H__2FCF39EB_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Shape.h"
//输出设备类
class OutDevice
{
public:
OutDevice();
virtual ~OutDevice();
public:
//输出对象
virtual void OutPut( Shape* pShape);
};
#endif // !defined(AFX_OUTDEVICE_H__2FCF39EB_
/***************************OutDevice.cpp*********************************/
#include "stdafx.h"
#include "OutDevice.h"
OutDevice::OutDevice()
{
}
OutDevice::~OutDevice()
{
}
void OutDevice::OutPut( Shape* pShape)
{
}
/*********************************OutFile.h*******************************/
#if !defined(AFX_OUTFILE_H__
#define AFX_OUTFILE_H__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "OutDevice.h"
class OutFile : public OutDevice
{
public:
OutFile();
virtual ~OutFile();
virtual void OutPut( Shape* pShape );
};
#endif // !defined(AFX_OUTFILE_H__
/****************************OutFile.cpp**********************************/
#include "stdafx.h"
#include "OutFile.h"
#include <string.h>
OutFile::OutFile()
{
}
OutFile::~OutFile()
{
}
void OutFile::OutPut( Shape* pShape )
{
const char* pStr = pShape->GetName();
FILE *fp = fopen("ShapeInfo.txt","a+");
if ( fp )
{
fwrite(pStr,strlen(pStr),1,fp);
fclose(fp);
fp = NULL;
}
}
/*********************************Screen.h********************************/
#if !defined(AFX_SCREEN_H__DBF
#define AFX_SCREEN_H__DBF
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "OutDevice.h"
class Screen : public OutDevice
{
public:
Screen();
virtual ~Screen();
virtual void OutPut( Shape* pShape);
};
#endif // !defined(AFX_SCREEN_H__DBF
/******************************Screen.cpp*********************************/
#include "stdafx.h"
#include "Screen.h"
#include <iostream.h>
Screen::Screen()
{
}
Screen::~Screen()
{
}
void Screen::OutPut( Shape* pShape )
{
cout << pShape->GetName() << endl;
}
/*******************************程序主文件*********************************/
#include "stdafx.h"
#include "Line.h"
#include "Rect.h"
#include "OutDevice.h"
#include "OutFile.h"
#include "Screen.h"
/*
需求:
请设计形状(类),完成(绘制方法),并且(输出图形到输出设备上)
输出的地方 :派生类:1屏幕类, 2文件类 ,基类:输出设备类
*/
void ShowAllShape( Shape* pShape,OutDevice* pDevice )
{
//输出 什么图形 到 什么设备上
//输出 图形 到 输出设备上
pDevice->OutPut(pShape);
}
int main(int argc, char* argv[])
{
Screen theCreen;
OutFile theFile;
Line theline;
Rect theRect;
//输出到屏幕
ShowAllShape(&theline,&thCreen);
//输出到文件
ShowAllShape(&theRect,&theFile);
return 0;
}