Channing
Channing

Reputation: 166

Using variables from another class in another file

My question is about using one class's variables in another class in a different file.

I have a class in Mabbs Input.h which looks like this:

class fileParameters{
public:
     static int numImageRows;
     static int imageRowLength;

private:
    int numRows=0;    
};

int numImageRows = 640;
int imageRowLength = 480;

I would like to use the variables numImageRows and imageRowLength in a separate file called Image Centering.cpp. I am aware that I need to put it in the header Image Centering.h, which I have done.

This is the code I have in Image Centering.h:

class imageCenteringParameters{

public:
   static int numImageRows;
   static int imageRowLength;


private:
    int imageArray[numImageRows][imageRowLength];

};

I have 2 questions:

a.) Is this the correct way to ensure I can use the variables from the class fileParameters in Mabbs Input.h in any other file? If so, is there a better/ more efficient way? If not, how would I go about fixing this, and what is good website to learn this from?

b.) It says that the fields in imageArray must have a constant size. I thought they would since they are declared in Mabbs Input.h. How would I go about fixing this, but more importantly, what does this mean?

Upvotes: 1

Views: 1711

Answers (1)

Ziezi
Ziezi

Reputation: 6467

  1. Classes in C++ can be extended, creating new classes which retain characteristics of the base class. This process, known as inheritance, involves a base class and a derived class: The derived class inherits the members of the base class, on top of which it can add its own members.

The inheritance relationship of two classes is declared in the derived class. Derived classes definitions use the following syntax:

class derived_class_name: public base_class_name
{ /*...*/ };
  1. The const qualifier explicitly declares a data object as something that cannot be changed. Its value is set at initialization. You cannot use const data objects in expressions requiring a modifiable lvalue. For example, a const data object cannot appear on the lefthand side of an assignment statement.

Variables definition using const type qualifier use the following syntax:

const type variable_name = initial_and_only_value;

For further reading making a constant array in C++.

Upvotes: 1

Related Questions