虚函数的简单应用

/*虚函数的简单应用*/

 

/*******************************Shape.h***********************************/

#if !defined(AFX_SHAPE_H__0DFF4B12_4075_4594_84F1_CD0039366CE1__INCLUDED_)
#define AFX_SHAPE_H__0DFF4B12_4075_4594_84F1_CD0039366CE1__INCLUDED_

#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_84F1_CD0039366CE1__INCLUDED_)

 

/******************************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_478F_AE85_5300CB4EB50A__INCLUDED_)
#define AFX_LINE_H__F6440DD8_8342_478F_AE85_5300CB4EB50A__INCLUDED_

#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_478F_AE85_5300CB4EB50A__INCLUDED_)

 

 

/*******************************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_4A75_B810_243B090B264C__INCLUDED_)
#define AFX_RECT_H__6CCC5CB0_6AE6_4A75_B810_243B090B264C__INCLUDED_

#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_4A75_B810_243B090B264C__INCLUDED_)

 

/*******************************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_97C9_4D40_A3B9_C8B2A8B67AE5__INCLUDED_)
#define AFX_OUTDEVICE_H__2FCF39EB_97C9_4D40_A3B9_C8B2A8B67AE5__INCLUDED_

#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_97C9_4D40_A3B9_C8B2A8B67AE5__INCLUDED_)

 

/***************************OutDevice.cpp*********************************/

#include "stdafx.h"
#include "OutDevice.h"



OutDevice::OutDevice()
{
   
}

OutDevice::~OutDevice()
{
   
}

void OutDevice::OutPut( Shape* pShape)
{
   
}

 

/*********************************OutFile.h*******************************/

#if !defined(AFX_OUTFILE_H__719F4419_EF0D_476F_A45C_A8D1E757257A__INCLUDED_)
#define AFX_OUTFILE_H__719F4419_EF0D_476F_A45C_A8D1E757257A__INCLUDED_

#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__719F4419_EF0D_476F_A45C_A8D1E757257A__INCLUDED_)

 

/****************************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__DBF9025C_C6E2_41A3_8C36_273A88EB516B__INCLUDED_)
#define AFX_SCREEN_H__DBF9025C_C6E2_41A3_8C36_273A88EB516B__INCLUDED_

#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__DBF9025C_C6E2_41A3_8C36_273A88EB516B__INCLUDED_)

 

/******************************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;
}

原文地址:https://www.cnblogs.com/w413133157/p/1659281.html