Well, currently trying to make a program to which the ControlUnit creates all objects, and then passes the objects down as arguments through constructors.
I'm a bit unsure if i understand it correctly, have written what i think it does in comments.
//ControlUnit.h
#pragma once
#include "Lala.h"
#include "Pulse.h"
class ControlUnit
{
private:
Lala lalaobj; //Make a Lala object.
Pulse pulseobj; //Make a Pulse class object.
public:
ControlUnit();
void run(void);
};
1 2 3 4 5 6 7 8 9 10 11 12 13
//ControlUnit.cpp
#include "ControlUnit.h"
ControlUnit::ControlUnit() : pulseobj(&lalaobj) // Use the pulse object made in ControlUnit class - private, give it parameters and initialize it at the same time as ControlUnit's object.
{
}
void ControlUnit::run(void)
{
pulseobj.read();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//Pulse.h
#pragma once
#include "Lala.h"
class Pulse
{
private:
Lala*cptr; // cptr points to Lala class
public:
Pulse(Lala*); //Function which have a type of class Lala.
void read(void);
};
//Pulse.cpp
#include "Pulse.h"
#include <iostream>
usingnamespace std;
Pulse::Pulse(Lala * laptr) //Function which has a type of Lala class, with a pointer laptr (which is our variable to work with in function).
{
cptr = laptr; //Our laptr pointer which points to class Lala
}
void Pulse::read(void)
{
cptr->tell();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//Lala.h
#pragma once
class Lala
{
private:
public:
Lala();
void tell(void);
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//Lala.cpp
#include "Lala.h"
#include <iostream>
usingnamespace std;
Lala::Lala()
{
}
void Lala::tell(void) //Need to reach this through a global object.
{
while(1)
{
cout << "hej";
}
}
What i'm most uncertain about is, why define the function in class pulse with the "Lala*" and why in private you make Lala*cptr...
class ControlUnit
{
private:
Lala lalaobj;
Pulse pulseobj;
That's a declaration. You don't create anything in a declaration.
You just say: "I invent something. It is called ControlUnit, and it posses a Lala object (called lalaobj) and a Pulse object (called pulseobj). That is what constitute its state.
It can perform several actions (methods) that depends from its state"
void Lala::tell(void) //Need to reach this through a global object. I'm not sure what you mean with this. Any Lala object can perform that method, and because is public you can use it outside the class.
why define the function in class pulse with the "Lala*" and why in private you make Lala*cptr...
You made the class, you should know that. Every Pulse object (since the moment it was born) has a relationship with a Lala object, the reference points to that object