1. 701.
    +1
    # include<iostream.h>

    /* speed recording unit - tachymeter (takometre) */
    class sru {

    int limit;//speed limit
    int* rec; //speed exceeding the limit will be recorded here
    int size; //size of dynamic memory (capacity of the tachymeter)
    int current;//ind. to the next free array element to save speed

    public:
    /* constructor */
    sru(int plimit = 90, int psize = 100) {
    limit = plimit; //set the speed limit,
    size = psize; // and the size of the dynamic memory
    rec = new int[size];//allocate dynamic memory
    current = 0; //initially no speed is recorded
    }

    /* copy constructor */
    sru(const sru& s) {
    limit = s.limit;
    size = s.size;
    rec = new int[size];
    for (int i = 0; i < size; i++)
    rec[i] = s.rec[i];
    current = s.current;
    }

    /* destructor */
    ~sru() {
    delete [] rec;
    }

    /* records the current speed,
    returns : 1 -> recorded
    0 -> couldn't be recorded
    (dyn. mem. not available) */
    int write(int speed) {
    if ( current > size - 1 )
    return 0;
    rec[current++] = speed;
    return 1;
    }


    //friend class srureader;//srureader class can access the internal part


    /* returns the current number of speeds recorded in the array*/
    int getcurrent()
    {
    return current;
    }

    /* returns the speed from the array with the given index
    returns : -1 if the given index is out of range
    */
    int getspeed (int index)
    {
    if ( index < 0 || index > current-1)
    return -1;

    return rec[index];
    }

    };

    /* this class has functions to read sru records.
    it is a friend of sru so that it can access
    internal areas */
    class srureader {

    sru* port;//using this port, connected sru will be read

    public:

    /* constructor */
    srureader() {
    port = null;//initially no sru is connected
    }

    /* connects the given sru
    returns : 1 -> connected
    0 -> couldn't be connected */
    int connect(sru* s) {
    //port not available (another sru connected)
    if ( port != null )
    return 0; //couldn't be connected
    port = s;//connect
    return 1;//connected
    }
    /* disconnects the the previously connected sru
    returns the disconnected sru */
    sru* disconnect() {
    sru* disconnectedport = port;
    port = null;
    return disconnectedport;
    }

    /* returns the maximum recorded speed */
    int maxspeed() {
    int max = 0;

    // if you declared srureader as friend inside the sru class
    /* for ( int i = 0; i < port->current; i++ )
    if ( port->rec[i] > max )
    max = port->rec[i]; */

    // if you access the internal part of sru class with accessor methods
    for ( int i = 0; i < port->getcurrent(); i++ )
    if ( port->getspeed(i) > max )
    max = port->getspeed(i);

    return max;



    }

    };

    int main() {
    sru takometre(100, 200);
    takometre. write(110);
    takometre. write(140);
    takometre. write(120);

    sru copy(takometre);

    srureader reader;
    reader. connect(&copy);
    cout << "max speed : " << reader. maxspeed() << endl;
    reader. disconnect();

    system("pause");
    return (0);
    }
    Tümünü Göster
    ···
   tümünü göster