This is a follow up post from Instance-level encapsulation with C++.
I've defined a class and created two objects from that class.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
class timeclass {
  private:
  string date;
  time_t gmrawtime, rawtime;
  struct tm * timeinfo;
  char file_date[9];
  void tm_init(int);
public:
  timeclass(int);
  void print_date();
};
void timeclass::tm_init(int y) {
  timeinfo = gmtime(&rawtime);
  timeinfo->tm_year = y - 1900; // timeinfo->tm_year holds number of years since 1900
  timeinfo->tm_mon = 0;
  timeinfo->tm_mday = 1;
  timeinfo->tm_hour = 0;
  timeinfo->tm_min= 0;
  timeinfo->tm_sec= 0;
}
timeclass::timeclass(int y) {
  timeclass::tm_init(y);
  gmrawtime = mktime(timeinfo) - timezone; 
}
void timeclass::print_date() {
  strftime(file_date,9,"%Y%m%d",timeinfo);
  date = string(file_date);
  cout<<date<<endl;
}
/* -----------------------------------------------------------------------*/
int main()
{
  timeclass time1(1991); 
  timeclass time2(1992); 
  time1.print_date(); // Prints 19920101, despite being initialized with 1991
  time2.print_date(); // Prints 19920101, as expected
  return 0;
}
This example is part of a date counter sliced and diced from my main program, but it illustrates my point. I want to have a date counter running for each instance of the class (time1 and time2), but it looks like once I construct the time2 object, the 'timeinfo' variable that I thought was encapsulated in time1 gets overwritten by the time2 constructor.
I am aware that C++ supports only class-level encapsulation, and am wondering if my problem is because members of the same class have access to one another's private members. Is there a way around this, so I can achieve what I want to do? Thank you, Taylor
 
     
     
     
    