I have a difficulty I need help to overcome. I am developing a program that uses more than 1 class. Variables defined in one class are used in other classes. For example:

class X1 {
private:
   int a;
   int b;
public;
   X1() {
      a = 10;
      b = 20;
   }
};

class X2 {
private:
   int c;
   int d;
public;
   X2() {
      c = 5 + 2*a;
      d = 10 - 4*b;
   }
};

int main() {
   X2 Test;
   int e = 40 + 10*d;
   cout << e << endl;

   keep_window_open();
   return 0;
}

I get a compile error that variable a and b in class X2 are not declared and initialized. I tried making X2 a sub-class of X1, hoping that X2 would inherit the variables a and b, but IO got even more compile errors.

My question: Is there a way to link the classes so that the variables of a prior class are known and usable in the subsequent classes? Or will I have to make one HUGE class?

Thanks in advance.

Recommended Answers

All 5 Replies

you have to make x1 the base class of x2. Study up on ingerentence. In your example X2 will be unable to access the private variables of X1 -- you will have to make those variables either protected or public.

class X2 : public X1
{
   // blabla
};

Before resorting to inheritance, can you state your actual problem with some actual context? Maybe you have been going about this problem wrong?

Stroustrup has an exercise in which the reader makes the "Hunt the Wumpus" game. My first step was to create the starting room. This is that code:

#include "../../std_lib_facilities.h"

class Initialize {
private:
	double rand_wumpus;
	double rand_pit;
	double rand_bat;

public:
	Initialize() {
	double srand((unsigned)time(0));
        rand_wumpus = 100.0 * rand()/RAND_MAX;
        rand_pit = 100.0 * rand()/RAND_MAX;
        rand_bat = 100.0 * rand()/RAND_MAX;
	}
};


class Current_room {
private:
	int room_num;
	bool wumpus_in_room;
	bool pit_in_room;
	bool bat_in_room;

public:
	Current_room() {
        if (rand_wumpus > 10.5 && rand_wumpus < 12.2) {
		wumpus_in_room = true;
		cout << "The wumpus is in the room.  You are eaten!" << endl;
	} else {
		wumpus_in_room = false;
	}

	if (rand_pit > 30.1 && rand_pit < 41.8) {
		pit_in_room = true;
		cout << "You have fallen into a pit and died!" << endl;
	} else {
		pit_in_room = false;
	}

    if (rand_bat > 62.4 && rand_bat < 75.0) {
		bat_in_room = true;
		cout << "A bat has taken you to another room!" << endl;
	} else {
		bat_in_room = false;
	}
	if (wumpus_in_room == false && pit_in_room == false && bat_in_room == false) {
		cout << "You are safe!" << endl;
	}
	}
};


int main()
{
Current_room Test;

keep_window_open();
return 0;
}

The Current_room class is not accessing the rand_wumpus, rand_pit, and rand_bat variables form the Initialize class. The suggestions are to make Initialize public, use get() functions for those variables, or make Current_room a sub-class of Initialize. I don't which is best.

There is not point for Initialize class to have the member private. You would be better of making them priviate. Also Initialize is a bad name. Call it something more meaningful and clear.

This thread has been continued in "Wumpus Hunt".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.