4 Replies - 615 Views - Last Post: 13 May 2013 - 03:23 AM Rate Topic: -----

#1 jibbler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 05-July 12

need help accessing member function of another class

Posted 12 May 2013 - 12:11 PM

The programming challenge is to create a parking ticket simulator with 4 different classes, each has its own set of responsibilities. To keep things simple, I've only written ParkedCar, ParkingMeter and ParkingTicket.

ParkedCar class holds the make, model, color, license number, and the number of minutes parked.

ParkingMeter class holds only the minutes purchased.

ParkingTicket needs to look at the ParkedCar object, and "read" those values, and possibly determine if a fine should be generated.

I've included the 3 header files, and also the main.

Could someone show me the correct syntax so that I need to code so that the ParkingTicket object "talks to" the ParkedCar object. It is this line where I'm confused. I would like the function getCarMake to read the data stored in ParkedCar::setMake function. Hopefully that makes sense. As always, I appreciate any assistance you can offer.

cout << ticket.getCarMake();


#include <iostream>
#include <string>

#ifndef PARKEDCAR
#define PARKEDCAR

using namespace std;

class ParkedCar
{
private: 
	string make;
	string model;
	string color;
	string licNum;
	int numMinParked;

public:

	//constructor
	ParkedCar::ParkedCar()
{
	make = "";
	model = "";
	color = "";
	licNum = "";
	numMinParked = 0;
}

	//mutators
	void ParkedCar::setMake(string m)
	{
		make = m;
	}

	void ParkedCar::setModel(string mod)
	{
		model = mod;
	}

	void ParkedCar::setColor(string c)
	{
		color = c;
	}

	void ParkedCar::setLicNum(string l)
	{
		licNum = l;
	}

	void ParkedCar::setNumMinParked(int nmin)
	{
		numMinParked = nmin;
	}

	//accessors
	string ParkedCar::getMake(string m)
	{
		return make;
	}

	string ParkedCar::getModel()const
	{
		return model;
	}

	string ParkedCar::getColor()const
	{
		return color;
	}

	string ParkedCar::getLicNum()const
	{
		return licNum;
	}

	int ParkedCar::getNumMinParked()const
	{
		return numMinParked;
	}
};
#endif;


#include <iostream>
#include <string>

#ifndef PARKINGMETER
#define PARKINGMETER

using namespace std;

class ParkingMeter
{
private:
	int minPurchased;

public:
	//constructor
	ParkingMeter::ParkingMeter()
	{
		minPurchased = 0;
	}

	//mutators
	void ParkingMeter::setMinPurchased(int m)
	{
		minPurchased = m;
	}

	//accessors
	int ParkingMeter::getMinPurchased()const
	{
		return minPurchased;
	}


};
#endif;


#include <iostream>
#include <string>

#include "ParkingMeter.h"
#include "ParkedCar.h"

#ifndef PARKINGTICKET
#define PARKINGTICKET

using namespace std;

class ParkingTicket
{
private:
	double fine;
	int parkedTime, timePurchased;
	string v;
	ParkedCar car;
public:

	//constructor
	ParkingTicket()
	{
		fine = 0.0;
		parkedTime = 0;
		timePurchased = 0;
		//car.setMake("");
		//car.setModel("");
		//car.setColor("");
		//car.setLicNum("");
		
	}
	
	//mutators
	void setParkedTime(int t)
	{
		parkedTime = t;
	}

	void setFine(double f)
	{
		fine = f;
	}

	void setCarMake(string c)
	{
		car.setMake(c);
	}
	void setCarModel(string d)
	{
		car.setModel(d);
	}

	//accessors

	string getCarMake()
	{
		return car.getMake();
	}
	int ParkingTicket::getParkedTime(int t)
	{
		return parkedTime;
	}
	
};
#endif;


#include <iostream>
#include <string>
#include "ParkingTicket.h"
#include "PoliceOfficer.h"
#include "ParkedCar.h"
#include "ParkingMeter.h"

void myTitle();

int main()
{
	myTitle();
	system("pause");
	system("cls");

	string value;
	int num = 0;

	ParkedCar car;
	ParkingMeter meter;
	ParkingTicket ticket;
	PoliceOfficer donutman;

	cout << "What is the make of the car?\n";
	cin >> value;
	car.setMake(value);
	
	cout << "What is the model of the car?\n";
	cin >> value;
	car.setModel(value);
	
	cout << "What is the color of the car?\n";
	cin >> value;
	car.setColor(value);

	cout << "What is the license plate number?\n";
	cin >> value;
	car.setLicNum(value);

	cout << "How many minutes were purchased for this parking spot?\n";
	cin >> num;
	meter.setMinPurchased(num);

	cout << "How many minutes has this vehicle been parked?\n";
	cin >> value;
	car.setNumMinParked(num);


	cout << ticket.getCarMake();
	


	system("pause");
	return 0;

}

void myTitle()
{
    cout << "*************************************************"<<endl;
    cout << "*             Welcome to Michael's              *"<<endl;
	cout << "*           Parking Ticket Simulator            *"<<endl;
    cout << "*         ============================          *"<<endl;
	cout << "*                                               *"<<endl;
    cout << "*************************************************"<<endl<<endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: need help accessing member function of another class

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: need help accessing member function of another class

Posted 12 May 2013 - 12:54 PM

I would create a member variable of the ParkedCar to hold a (or many) ParkingTickets and add a setter which takes a ParkingTicket as an argument.
Was This Post Helpful? 0
  • +
  • -

#3 jibbler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 05-July 12

Re: need help accessing member function of another class

Posted 12 May 2013 - 03:18 PM

View PostJackOfAllTrades, on 12 May 2013 - 12:54 PM, said:

I would create a member variable of the ParkedCar to hold a (or many) ParkingTickets and add a setter which takes a ParkingTicket as an argument.


I created a ParkedCar object within the ParkingTicket class. I've tried using the following code in main to call it, but this doesn't seem to work either. Is my syntax wrong? Or am I doing something else terribly wrong?

Here I have a function in ParkingTicket class to set the car's make from the ParkedCar object.
void setCarMake(string c)
	{
		car.setMake(c);
	}


Now in main, I want the ParkingTicket class (ticket object) to read the ParkedCar object and access the value stored there.

	cout << ticket.getCarMake(car.getMake);

This post has been edited by jibbler: 12 May 2013 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1370
  • View blog
  • Posts: 4,741
  • Joined: 19-February 09

Re: need help accessing member function of another class

Posted 12 May 2013 - 05:39 PM

As JackOfAllTrades suggests, you can copy the car data to the ticket.

ticket.setCar(car);

cout << ticket.getCarMake() << endl;


Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: need help accessing member function of another class

Posted 13 May 2013 - 03:23 AM

The idea here is to think of the objects as you would in the real world. You're modelling the real world here. So, how would this process work?

A car pulls up to a meter, puts money in for a certain period of time and the driver leaves. If the driver does not return by the time the meter expires, the car is in violation and the parking meter pops up a flag stating so (hey I'm old...that's the way they used to work). A meter maid/police officer on the beat comes across the parking meter in violation and writes a ticket which is placed under the car's windshield.

So this is all about containment. What are the relationships between the objects? You could say that the ParkingMeter has-a (one, and only one) ParkedCar. The PoliceOfficer has-many ParkingMeters (the officer's "beat"). A ParkedCar MAY have a (or perhaps more than one) ParkingTicket.

I see it as something like this:
The PoliceOfficer creates ParkingTickets which are given to the ParkedCar, so a PoliceOfficer must know how to write a ticket (create a ParkingTicket).

A ParkingMeter would have a ParkedCar and a method which determines that the contained ParkedCar is in violation and would return that ParkedCar for the PoliceOfficer to ticket.

A ParkedCar needs a method to receive a ParkingTicket from the PoliceOfficer.

Important takeaway:
The ParkedCar is passed along between the ParkingMeter and the PoliceOfficer.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1