Hey,

I allowed to use only one getter and setter per class.
My variables initialization in the calss in need them is correct (I used the Debagger)
But then I can not access the private variables ...
Here is part of the program..

#ifndef _H_SIMULATION
#define  _H_SIMULATION
#include <iostream>

using namespace std;

class Simulation{

private:
    int opp;
    int numtaxi;
    int fromrow;
    int fromcol;
    int torow;
    int tocol;

public:
    Simulation(char *file);


    Simulation(int opp, int numtaxi, int fromcol, int fromrow, int tocol, int torow) :opp(opp), 
        numtaxi(numtaxi), fromcol(fromcol), fromrow(fromrow), tocol(tocol), torow(torow){
        cout << "Ctor of Class Simulation is Called" << endl;
    };

    void setSimulation(int opp,int numtaxi, int fromcol, int fromrow, int tocol, int torow);
    int getSimulation();

    ~Simulation();
};



#endif


#include <string.h>
#include <fstream>
#include "Simulation.h"


void Simulation::setSimulation(int _opp, int _numtaxi, int _fromcol, int _fromrow, int _tocol, int _torow)
{
    opp = _opp;
    numtaxi = _numtaxi;
    fromcol = _fromcol;
    fromrow = _fromrow;
    tocol = _tocol;
    torow = _torow;
}
int Simulation::getSimulation(){
    return opp;
}
Simulation::Simulation(char *file)
{
    ifstream command(file);
    char str[250];
    char* pch;
    string tempfrom;
    string tempto;
    int i = 0;


    if (command.is_open())
    {

        while (command.getline(str, 250)&& *str!=EOF)
        {
            if (str[0] == '#')
                continue;
            pch= strtok(str,",");
            opp = *pch - '0';
            for (i = 0; pch != NULL && i < 3; i++)
            {
                pch = strtok(NULL, ",");
                if (i == 0)
                {
                    numtaxi = *pch - '0';
                }
                if (i == 1)
                {
                    tempfrom = pch;
                }
                if (i == 2)
                {
                    tempto = pch;
                }
            }

            fromcol = tempfrom[0] - 'a';
            fromrow = tempfrom[1] - '0';
            tocol = tempto[0] - 'a';
            torow = tempto[1] - '0';

        }

    }
    else
    {
        cout << "unable to open the file";
        exit(1);
    }
    command.close();

}

Simulation::~Simulation()
{
    cout << "Dtor of Simulation Brain is called" << endl;
}


#ifndef __ORGNIZER_H_
#define __ORGNIZER_H_
#include <iostream>
#include "Taxi.h"
#include "City.h"
#include "Simulation.h"
#include <string.h>
#include <fstream>
using namespace std;

class Orgnizer
{
private:
    Taxi T1;
    Taxi T2;
    Simulation S1;
    City C1;


public:



    Orgnizer(const Taxi &taxi1, const Taxi &taxi2, const City city, Simulation &sim);
    void setOrgnizer(Taxi T1, Taxi T2, Simulation S1, City C1);
    //void getOrgnizer();
    Orgnizer& MovePassenger(const Orgnizer& org);
    //~Orgnizer;
};

#include <iostream>
#include "Orgnizer.h"



Orgnizer::Orgnizer(const Taxi &taxi1, const Taxi &taxi2, const City a, Simulation &sim) :
T1(taxi1), T2(taxi2), C1(a), S1(sim){
    cout << "Ctor of class Orgnizer was called" << endl;
}

Orgnizer& Orgnizer::MovePassenger(const Orgnizer& org)
{

    if (org.S1.setSimulation.opp==1)   //this dosen't work.. how can i use opp???

}

void Orgnizer::setOrgnizer(Taxi T1_, Taxi T2_, Simulation S1_, City C1_)
{
    T1=T1_;
    T2=T2_; 
    S1=S1_;
    C1=C1_;
}

I see that in your setter function, there is no checking or processing; it simply sets the private variables to whatever the parameter says.

In this case, there is no point in having them private; you might as well make them public variables, and then you won't need setters or getters.

Edited 1 Year Ago by Moschops

Your getter needs to be a const member function as in int getSimulation() const;. Other than that, Moschops is correct in that you need to do argument validation before you set member variables otherwise you have effectively made your private members public. In addition, do NOT use leading underscores for variable or argument names since that construct is reserved for the use of the system. You may inadvertently end up with undesirable consequences... :-)

This article has been dead for over six months. Start a new discussion instead.