⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prg12_7.cpp

📁 Data Structures with C++附代码
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

#include "random.h"         // include random number generator
#include "houses.h"         // including the painting hierarchy

void main(void)
{
    // dynamic list of House addresses
    House *contractorList[5];
    RandomNumber rnd;
    
    // construct the list of 5 houses to be painted
    for (int i=0;i < 5;i++)
        // randomly choose house type 0, 1, 2, create the object,
        // and assign its address in contractorList.
        switch(rnd.Random(3))
        {
            case 0: contractorList[i] = new WoodFrameHouse;
                    break;
            case 1: contractorList[i] = new StuccoHouse;
                    break;
            case 2: contractorList[i] = new VinylSidedHouse;
                    break;
        }
    
    // paint houses using method Paint. since Paint is virtual,
    // dynamic binding is used, and the correct method is called.
    for (i=0;i < 5;i++)
    {
        contractorList[i]->Paint();
        cout << endl;
    }
}

/*
<Run of Program 12.7>

Painting a Stucco House
Painting a Vinyl Sided House
Painting a Wood Frame House
Painting a Vinyl Sided House
Painting a Stucco House
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -