Use Members of One Class in Another Class (OOP) - c++

I have two classes Instructor and Game.
Instructor.h
class Instructor
{
int instrID;
public:
Instructor();
void showGameStatus();
int createGame();
vector<int> createGames(int numberOfGames);
};
Game.h:
class Game {
private:
int gID;
int instrID;
int pFactID;
public:
Game() { // default constructor
gID = 0;
instrID = 0;
pFactID = 0;
};
These are in Instructor.cpp
void Instructor::showGameStatus()
{
}
int Instructor::createGame()
{
Game g;
}
CreateGame() initializes a game. I want that upon calling showGameStatus() I can print out all properties (eg gId, InstrId) of the Game g that initialized earlier etc.
Is it possible to access the properties of Game g that in another method?

This should do it. Class Instructor should inherit class Game:
class Instructor::public Game{
your code here
}

The short answer is: No.
The longer answer is this: If I understand correctly, what you want to accomplish, the problem is that the object g of type Game is held by a local variable inside the scope of your Instructor::createGame member function. Once that function is "done", i.e. the local scope ends, the object, which has automatic storage will be destroyed. It's gone. I don't know what the int means that you return, but no matter what it does, it doesn't hold an object of type Game.
Now, you probably want your createGame to return some type of handle to an actual Game object. Depending on your specific setting, it is your job to choose how to pass such an object around. For example, one way might be this:
Game Instructor::createGame() const { // 1
Game g;
// do stuff with g, perhaps?
return g;
}
Another might be:
std::unique_ptr<Game> Instructor::createGame() const { // 2
auto gptr = std::make_unique<Game>();
// do stuff with gptr, perhaps?
return gptr;
}
Or yet another:
std::size_t Instructor::createGame() { // 3
// Instructor has a member std::vector<Game> games
games.emplace_back();
// do stuff with games.back()
return games.size()-1;
}
There are countless other ways to pass the object around.
No matter what you choose you have to pass something to identify which Game object you are talking about back into your showGameStatus function, if you plan to have more than one Game object flying around (I assume you do).
auto some_handle = instructor.createGame();
// ... later ...
instructor.showGameStatus(some_handle);
This all holds true, if you want more than one object. Otherwise you might want to just add the object as a member of your Instructor type:
class Instructor {
private:
Game game;
public:
Instructor() : game() {}
// no createGame function, it is superfluous
void showGameStatus() const {
game.some_output_function();
}
};

Just inherit the Instructor Class into the Game Class and do your work...

Related

How to implement selectable namespace-like feature within a class?

I am creating a class for a chess engine. The class contains information about where each piece is, what moves are allowed, etc. The class also allows a move to be simulated without having to create a new object. The current implementation looks like this:
// in header file
class ChessGame{
int base_var1; // base indicates real game value
int test_var1; // test indicates simulated game value
... many other vars of various types
void makeRealMove(int move); // modifies base values
void makeTestMove(int move); // modifies test values
}
// in src file
void ChessGame::makeRealMove(int move){
base_var1 = move; // lots of code in here
}
void ChessGame::makeTestMove(int move){
test_var1 = move; // identical code here
}
This works, but the code for makeRealMove and makeTestMove is exactly the same, just swapping each test_var with appropriate base_var. What I would like to do is have one function makeMove which can dynamically select the right type of variables to change. This would remove essentially redundant code, make debugging easier, and such. If namespaces were allowed within classes and could be conditionally selected, I would do the following:
// in header file
class ChessGame{
namespace base { int var1; } // plus the other vars
namespace test { int var1; } // plus the other vars
void makeMove(int move, bool condition);
}
// in src file
void ChessGame::makeMove(int move, bool real_move){
if(real_move) { using namespace base; }
else { using namespace test; }
var1 = move; // appropriate variable selected
}
Unfortunately, namespaces cannot be nested in a class, and even if they could be, I could not select between two of them in this manner. So is there a way to get this kind of behavior, or am I stuck with my current approach?
You can use a class instead of a namespace:
class ChessGame{
struct Vars {
int var1; // plus the other vars
};
Vars realGame;
Vars testGame;
void makeMove(int move, bool condition);
void makeMoveImpl(int move, Vars &vars);
};
void ChessGame::makeMove(int move, bool real_move) {
if (real_move) makeMoveImpl(move, realGame);
else makeMoveImpl(move, testGame);
}
void ChessGame::makeMoveImpl(int move, Vars &vars) {
vars.var1 = move; // appropriate variable selected
}
Note that based on your design, it might make sense to make Vars a global class instead of a nested one (while still storing two instances of it inside ChessGame). makeMoveImpl could then even become a member function of Vars and ChessGame would serve just as a delegator to one or the other.
class ChessGame{
std::array<int, 2> var; // var[0] == real game value, var[1]== simulated game value
//... many other vars of various types
void makeRealMove(int move){makeMove(move,false);};
void makeTestMove(int move){makeMove(move,true);};
void makeMove(int move, bool test){
var[test]= move;
// lots of code in here
};
};

Passing Vector to function while using Graph

I wrote my code like this inside a class to calculate max distance from source to node. Now I need to take it out of the class and have a similar function to calculate my distances but by using Djikstra. So, I would need to pass this city vector and source being one of my vertex ( this will loop over vertices ) and return a distance from this function back to class for my next computation. I am running out of time so help me.
int distanceToNearCity(int cityIdOfStore, const std::vector<City> & AllCities) const
{
// is there a store in this city ?
if (storeExists || cityId == cityIdOfProposedNewStore)
{
return 0; // 0 distance
}
int distance = TOOFAR; // initialise with more than max allowed
for (int i=0; i<connectingCities.size(); ++i)
{
int id = connectingCities[i];
if (AllCities[id-1].hasStore() || AllCities[id-1].getId() == cityIdOfProposedNewStore)
{
// we have a store (or proposed one) here, but is it's distance more than other stores ?
if (distances[i] < distance)
{
distance = distances[i];
}
}
}
return distance;
}
How can I pass these class objects to function which is public.Thanks!!
Well, accessing internal member variables from main is problably a bad design, breaks encapsulation, is a possible source of problems, thus not a good idea.
Solution 1
Let's say your class is called MyOriginal. Make distanceToNearCity virtual. Create a derived class MyDerived and rewrite distanceToNearCity so that implements Djikstra. In the derived class you can access the original member variables from MyOriginal, as long as they are public or protected. The user (main) does not need to know the implementation details.
Solution 2
Rewrite the original distanceToNearCity method so, that it has no side effects as it ware a static method. This means that it does not need access to any of the member variables. Pass all arguments to the method via parameters. I mentioned 'as it was a static method', since the method will not be the member of the original MyOriginal class. Make MyOriginal a template class, implement distanceToNearCity in an external class and pass this class as a template argument to MyOriginal. As a non member, you can implement any number of distance algorithms and pass them to the original class. This solution has the advantage, that the call to the 'virtual' method is known at the compile time, so it produces faster code.
template<class T> // T will contain distance computation
class MyOriginal
{
public:
void process()
{
.. // your staff
int distance = T::distanceToNearCity(necessary arguments); // call the external implementation
.. // rest of your staff
}
};
class OriginalDistance
{
public:
static int distanceToNearCity(necessary arguments); // your original implementation
};
class DjikstraDistance
{
public:
static int distanceToNearCity(necessary arguments); // another distance computation
};
int main(int argc, char *argv[])
{
MyOriginal<DjikstraDistance> myInstance;
myInstance.process(); // call processing, use DjikstraDistance::distanceToNearCity() inside
}
Solution 3
If you from some reason dislike both previous implementations, you can use a 'c' style solution. Create a type which represents a signature of the distanceToNearCity method.
Write 'distanceToNearCityOriginal' and 'distanceToNearCityDjikstra' functions. Pass a pointer to desired function as a paramter the MyOriginal::process method. C++ developers will dislike you.
typedef int (DistanceAlgo*)(necessary arguments); // pointer to a function which returns int
int distanceToNearCityOriginal(necessary arguments); // first implementation of distance
int distanceToNearCityDjikstra(necessary arguments); // second implementation of distance
class MyOriginal
{
public:
void process(DistanceAlgo algo)
{
.. // your staff
int distance = (*algo)(necessary arguments); // call the external implementation
.. // rest of your staff
}
};
int main(int argc, char *argv[])
{
DistanceAlgo algo = &distanceToNearCityDjikstra; // choose djikstra
MyOriginal myInstance;
myInstance.process(algo); // call processing, use distanceToNearCityDjikstra inside
}

Changing int value from a class function

I have a shape class that I initialize from my main program and give the parameters in the constructor.
Shape *cusomShape = new CustomShape(float radius, float origin)
The shape class has some functions such as rollover and more.
When the rollover function inside the shape class is fired, I want to change a certain int value in the main program. This might similar to firing of an event that changes the value when the rollover function is fired, but I am not sure how to do that in C++. If at all, events is the ideal approach here, it would great to see a short example coming.
If using the event is not the correct, what would the ideal way to go about this?
I think what you need is to pass a value by pointer or reference to the function in Shape and then modify it. If the function is called not from main but from somewhere else passing the pointer is the better option you have. First pass the pointer to the class and store it using another method and then each time rollover is called make use of it.
EDIT: example:
class CustomShape {
void storePointer(int* _value) {
value = _value;
}
void rollover() {
.. do stuff
*value++; // for instance
... do stuff
}
int * value;
}
int main() {
int a;
CustomShape cs;
cs.storePointer(&a);
....
cs.rollover();
....
return 0;
}
Pass a reference to the variable in the constructor and save that reference. Change the value when needed.
I would suggest passing a reference to the variable to the member function that needs to change its value. Storing a reference in a class couples the Shape class to the reference. This means that each time you want to use the Shape, without updating the integer, you cannot, since the Shape constructor will expect a reference/pointer to the int as an argument (the Shape class will store the pointer/reference as an attribute). Passing a reference/pointer to the member function promotes Low Coupling.
#include <iostream>
class Shape
{
double shapeValue_;
public:
Shape (double value)
:
shapeValue_(value)
{}
void fireFunction(int& updateMe)
{
updateMe = 123;
}
};
using namespace std;
int main()
{
int update;
cout << update << endl;
Shape s(4.5);
s.fireFunction(update);
cout << update << endl;
return 0;
};
And in this case, you have an option for a main program that doesn't involve shape object calling on fireFunction:
int main()
{
Shape s(4.5);
// Main program that doesn't use fireFunction.
return 0;
};
In this case, if you have member functions changing input arguments, you should take on a style for defining such functions: e.g. make sure that the variable that gets changed by the member function is always the first input argument in its declaration.
If you want complex objects to communicate updates between each other, you can make use of the Observer Pattern.

Order of Class Definitions in C++

I've got a bit of a problem here. I'm trying to define several classes, of which some are Players and some are Pawns belonging to the players. Coming from Python, I'm used to being able to conveniently access a Pawn's owning Player through the Pawn, as well as accessing a Player's Pawns through the Player. Correct me if I'm wrong, but this seems impossible in C++.
I currently define Player first, and one of its data members m_Pawns is supposed to be a vector<Pawn>. I declare the data member, but I don't assign it any value. I also define a member function that is meant to assign a vector of pawns to m_Pawns, but I don't call it anywhere near the constructor. Since I'm not actually calling the constructor for Pawn in the constructor for Player, it seems I should be fine.
Here's my Player class. The Board class is defined beforehand, whereas the Pawn class is defined afterwards (the Pawn class contains pointers to an owner of the Player class, so switching it around doesn't really help).
class Player
{
public:
Player(sf::Color color, const string& name);
sf::Color GetColor();
string GetName();
void CreatePawns(Board& board, int which);
protected:
vector<Pawn> m_Pawns;
sf::Color m_Color;
string m_Name;
};
Player::Player(sf::Color color, const string& name):
m_Color(color),
m_Name(name)
{}
sf::Color Player::GetColor()
{
return m_Color;
}
string Player::GetName()
{
return m_Name;
}
void Player::CreatePawns(Board& board, int which)
{
switch(which)
{
case 1:
for(int i = 0; i < 4; ++i)
{
m_Pawns.push_back(Pawn((*board).Cluster1[i], this*, m_Color));
}
break;
case 2:
for(int i = 0; i < 4; ++i)
{
m_Pawns.push_back(Pawn((*board).Cluster2[i], this*, m_Color));
}
break;
case 3:
for(int i = 0; i < 4; ++i)
{
m_Pawns.push_back(Pawn((*board).Cluster3[i], this*, m_Color));
}
break;
default:
cout << "Invalid player ID!\n\n";
break;
}
}
If the class Player is coming first and class Pawn coming later then you can only declare pointer or reference to the later class (here Pawn). You cannot have objects of later class, e.g.
class Player {
Pawn* p; // allowed
Pawn& r; // allowed
vector<Pawn*> p; // allowed
vector<Pawn&> vr; // not allowed (reference are not copyable)
vector<Pawn> o; // error !
};
class Pawn {};
There is no way you can overcome this situation, as in C++ for non-template class one need to show full definition to declare objects.
The only way out is to reformat your code or use pointer/reference (with forward declaration).
The class Pawn still has to be defined so compiled can instantiate vector. You can get away with storing references or pointers to Pawn objects in your vector instead of values; vector for example. In that case forward declaration of class Pawn will be enough.
You can switch it around, because you can forward declare Player, and then Pawn can have a pointer to it.
You can take a pointer to an incomplete type. You can't hold values of that type.
You can do something like this:
class Pawn;
class Player {
}
class Pawn {
}

Is it possible to pass a variable out of a class without creating a new object in C++

I have a variable, which is a member of one of my classes, that another is in need of, but I'm not sure how to effectively pass the value between them without using a global variable, which is something I'd like to avoid if at all possible. I know I could create an object, but that would invoke the constructor of the originating class which would execute a number of functions and write the needless results to memory, which would be wasteful of system resources.
Is there an easy way to pass this value between the two functions?
Update: The class that is in need of the variable, called no_of_existing_devices. The purpose of class Initialise is to open up a file and count the number of lines of test it contains, and place that number in the variable int no_of_existing_devices, which is then used by the Device::Device() to create an object for each
class Device
{
public:
void view_attribute_list();
void set_attribute();
Device();
};
Device::Device()
{
for (int count = 0; count < no_of_existing_devices; count ++)
{
// Create an object for each iteration, up to a maximum of no_of_existing_devices
}
}
The class of which this variable is a member
class Initialise
{
public:
int no_of_existing_devices;
bool initialisation;
string existing_device_list[100];
void initialise_existing_devices();
Initialise();
};
Initialise::Initialise()
{
no_of_existing_devices = 0;
}
void Initialise::initialise_existing_devices()
{
string line;
ifstream DeviceList;
DeviceList.open("devices/device_list");
while (true)
{
getline(DeviceList, line, '\n');
if (DeviceList.eof())
{
break;
}
++ no_of_existing_devices;
}
DeviceList.close();
DeviceList.open("devices/device_list");
for (int i = 0; i < no_of_existing_devices; i ++)
{
getline(DeviceList, line, '\n');
existing_device_list[i] = line;
}
Device existing_devices[no_of_existing_devices];
!initialisation; // Existing devices are now initialised
}
Okay, from what I understand:
You don't want to have a global
You don't want to have a static
You don't want to introduce a dependency between Device and Initialise
There is one other option, assuming something owns Device and Initialise, move the no_of_existing_devices up to there, then construct both Device and Initialise with a reference to this variable...
In a similar circumstance I was just passing the pointer to the member --- I had to invoke a member function then, so it was a pointer to the member function, http://www.parashift.com/c++-faq-lite/pointers-to-members.html
It's a bit messy, but it works :-).
If the variable in the originating class can hold a value without an instance of the class I would assume that the variable is static. If not create a public static member of the class. And use it in the target class.
Something like:
// .h file
class A
{
public:
static int a;
}
// .cpp file
int A::a = 123;
// .cpp file of class B
void B::foo()
{
cout << A::a;
}
If it is a class attribute (internal variable), then you can obtain a reference through a get method. Otherwise, you can use the friend keyword on the class you want to access the attribtue from the other For example, if you declare friend class B; on class A, the attributes of the class B will be accessible on the class A.
I suggest you use the first method in order to maintain your code OO pure ;)
Edit: of course, if you access through a reference there are no resources wasted :)
Edit 2: use a static method on Initialise class that returns the no_of_existing_devices and call Initialise::NoOfExistingDevices() on the Device class. If you want to resources use a pointer like this:
public static int* Initialise::NoOfExistingDevices() {
return &no_of_existing_devices;
}
By the way, I advise you to turn the variable private.