Classes - Class that inits another class

Okay so i'm in the making of a project and i got a bit confused.

This is what i'm trying to make: http://bildr.no/view/767774

What i'm having trouble understanding is how to connect all these classes.

As Controlunit makes all the object. But i cant access these objects from other classes. Do i need to make multiple objects?

I will include the headers of the classes.

1
2
3
4
5
6
7
8
9
class Buzzer
{
public:
	void silence(void);
	void setFrequency(int);
	int getFrequency(void);
private:
	int frequency;
};

1
2
3
4
5
6
7
8
9
10
11
class ControlUnit
{
public:
	void run(void);
private:
	void printMenu(void);
	void menuChoice(char);
	float readNewValue(float,float);
	float minTemp, maxTemp;
	int minPulse, maxPulse;
};

1
2
3
4
5
6
7
class PulseSensor
{
public:
	int readPulse(void);
private:

};

1
2
3
4
5
class TemperatureSensor
{
public:
	float readTemperature(void);
};
Here's my interpretation:

The ControlUnit class is the main center of activity, it sets up everything.
1
2
3
4
5
6
7
8
9
10
11
class ControlUnit {
public:
    ControlUnit(); //no constructor args
    {
        SerialCom* datastream = new SerialCom(data source);
        Buzzer* buffer = new Buffer(datastream); //make a buzzer, which uses the SerialCom
        //same for the PulseSensor and TemperatureSensor
        //store the references to these somehow in the ControlUnit, as members for instance
    }
    [...]
};


The SerialCom class is what everything uses to communicate with the external data source
1
2
3
4
5
class SerialCom {
public:
    SerialCom(info on where to get data);
    [...]
};


The Buffer class listens for requests to do stuff and performs those requests over it's SerialCom
1
2
3
4
5
6
7
class Buffer {
public:
    Buffer(SerialCom* datastream);
    silence() {
       //send some command over the stream
    }
};


Same for the other sensors, pass them a datastream to use via constructor, and they use that stream to fulfill the ControlUnit's requests.
I see your point, but since i haventh learned about the "new" command where you create a class it seems.

I have a class Temperature, are there no way to connect it to the object created by ControlUnit for SerialCom?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Temperature
{
public:
	Temperature();
	int readValue(void);
	void setMaximum(int);
	void setMinimum(int);
	void setOnOff(bool);
	bool alarm(void);
private:
	bool onOff;
	int maximumTemperature, minimumTemperature, currentTemperature;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Temperature.h"

using namespace std;

Temperature::Temperature()
{
	onOff = 1;
	maximumTemperature = 0;
	minimumTemperature = 0;
	currentTemperature = 0;
}

int Temperature::readValue(void)
{

//code to reach into SerialCom class functions.

return 0;
}
Last edited on
Unless you make all of the classes static you can't make them "communicate" without passing around pointers / references to instances of the classes.

I don't know how you're supposed to do this if you haven't learned about allocating instances of classes.

Do you know how to create a stack instance of a class and/or use pass by reference?
Last edited on
Topic archived. No new replies allowed.