jeudi 8 novembre 2018

C++ Class Object Counter As Random with Static Member

First of all I do my researches but because I couldn't learn it in lesson well, other answers weren't usable for me. Now my teacher asks from this code that my counter must be initialized by a static member. And when I create another object in the class it must be count by 1000, 1001, 1002. Also first object I created must have a random number which mustn't be global, after that random number will be count 1 by 1. And I must use pointer for all of that. You know it is really complicated for me but that's what he wants as he said. Here's my code which isn't working.

#ifndef POINT_H
#define POINT_H
#include <cstdlib> 
#include <ctime> 

class Point
{
private:
int x;
int y;
static int count;

public:
Point(int, int);
void objectCount()
{
    srand((unsigned)time(0));
    Point::count = rand();
    Point::
    count++;
}

 void print();

};
#endif 

and my .cpp

#include "Point.h"
#include <iostream>
using namespace std;

Point::Point(int x, int y):x(x), y(y){}


void Point::print()
{
 cout << "(" << x << "," << y << ")" << " " << count << endl;
}




Aucun commentaire:

Enregistrer un commentaire