pointerdemo.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 39 行

CPP
39
字号
#include <iostream>
using namespace std;
#include "tvector.h"
#include "date.h"
#include "dice.h"

// basic pointer demo

int main()
{
    Date today;
    Date * nextDay = new Date(today+1);
    Date * prevDay = new Date(today-1);
    
    cout << "today\t\t tomorrow\t\t yesterday" << endl;
    cout << today << "\t" << nextDay << "\t" << prevDay << endl;
    cout << today << "\t" << *nextDay << "\t" << *prevDay << endl; 
      
    nextDay = prevDay;
    cout << today << "\t" << *nextDay << "\t" << *prevDay << endl;   
    *prevDay += 2;
    cout << today << "\t" << *nextDay << "\t" << *prevDay << endl; 
    cout << today << "\t" << nextDay << "\t" << prevDay << endl;
       
    cout << endl << "k\tsides\troll\tcount" << endl <<endl;
    const int DICE_COUNT = 6;
    tvector<Dice *> dice(DICE_COUNT);
    int k;
    for(k=0; k < DICE_COUNT; k++)
    {   dice[k] = new Dice(2*k+1);
    }
    for(k=0; k < DICE_COUNT; k++)
    {   cout << k << "\t" << dice[k]->NumSides() << "\t"
             << dice[k]->Roll() << "\t";
        cout << dice[k]->NumRolls() << endl;
    }
    return 0;
}

⌨️ 快捷键说明

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