Variable in class holds wrong value

instance variable
how to use variable of one class in another in java
assigning values to variables in java
how to get the value from one class to another class in java
how to call variable from one method to another method in java
how to pass value from one method to another in java
class variable in java
how to call a variable from another class in java

I've just started learning c++, and have some earlier experience in java and c. I decided to experiment a bit with classes in order to learn more about them.

Currently I have 2 classes, Book and Shelf. Shelf consists of one Book, a string and an int. Book consists of a string and an int called page.

All variables contain expected values, except for my int page in Book. It contains some arbitrary value that seems to have no correlation to anything at all. My guess is that it has something to do with my code somehow messing up some important pointer to the page.

I've tried to change page to an *int instead of int, hoping that I would hold the pointer to the value, rather than the value itself. But the output is stil "wrong" as the dereferenced pointer still contains the wrong value.

My main.cpp:

int main(int argc, char** argv) {

 Book harrypotter("Harry Potter and the Chamber of Secrets");
 Shelf fantasy(harrypotter, "fantasy", 1);

 std::cout << fantasy.getSUBJECT() << std::endl;
 std::cout << fantasy.getSHELFNUMBER() << std::endl;
 std::cout << fantasy.getBOOK().getNAME() << std::endl;
 std::cout << fantasy.getBOOK().getPAGE(); //this line failes

return 0;


class Shelf {
 Shelf(Book book, std::string subject, int shelfnr);

 std::string getSUBJECT(){return this->subject;}
 Book getBOOK(){return this->book;} //container with the faulty int
 int getSHELFNUMBER(){return this->shelfnr;}

 Book book;
 std::string subject;
 int shelfnr;



class Book {
 Book(std::string name);

 std::string getNAME(){return this->name;}
 void setPAGE(int page){this->page = page;}
 int getPAGE(){return this->page;} //this returns wrong value!

 std::string name;
 int page;//this contains wrong value!

Current output:

Harry Potter and the Chamber of Secrets
-2145318336 (or some other arbitrary number)

Output that i expect:

Harry Potter and the Chamber of Secrets

One reason why you do not see changes is this:

Book getBOOK(){return this->book;}

This returns a copy of the Book object. Thus you are changing a copy, not the original Book.

If you want to change the actual Book object declared in Shelf, return a reference:

Book& getBOOK(){return this->book;}

4. Methods Use Instance Variables: How Objects Behave, And if the Dog class has a method makeNoise(), well, don't you think a 70-pound dog barks a bit deeper than the little 9-pounder? In other words, methods use instance variable values. Like, “if dog is The compiler won't let you return the wrong type of thing. Declare and create a Dog array, to hold 7 Dog references. Java Variables. Variables are containers for storing data values. In Java, there are different types of variables, for example: String - stores text, such as "Hello". String values are surrounded by double quotes; int - stores integers (whole numbers), without decimals, such as 123 or -123

The member function Shelf::getBOOK() returns the data member by value. This means that a copy is made and returned as a temporary object. This line


mutates the temporary copy, not the object that is owned by the Shelf instance. The page variable that you access later on is hence uninitialized, and reading from it is undefined behavior. That's why it would be advantageous to set a meaningful default value, e.g.

class Book {
  // ...
  int page = 0;

Fixing your original issue can be achieved by

Book harrypotter("Harry Potter and the Chamber of Secrets");


// Now, the above book has the desired state, so pass it the Shelf instance:
Shelf fantasy(harrypotter, "fantasy", 1);

or by changing the Shelf::getBOOK() signature as suggested in @PaulMcKenzie's answer.

Why the Instance Variable of the Super Class Is Not Overridden in , When we create a variable in both parent and child classes with the the variable value that belongs to the Parent class, even if it is holding In the subclass, these methods will try to return a value of a field of the wrong type! In the previous code under the x variable, create a new variable called 'a' and give it the value 10. Then, print it out using the Console.WriteLine command but this time write (x + a): int x = 5; int a = 10; Console.WriteLine(x + a); Console.ReadLine(); Variables can be changed; you can change the variable x to 20 and you will get the result 30.

Book getBOOK(){return this->book;}

You return a copy of book, and call setPAGE on that copy. The original book never changes.

If there is supposed to be only one book in shelf(which is very odd) you could expose setPAGE() from shelf itself which will call book.setPAGE().

If you plan to have many book's on a shelf, getBOOK() doesn't make any sense.

TEXTBOOK OF COMPUTER SCIENCE : FOR CLASS XII, A pointer variable holding incorrect address value is most disastrous for any program. 7. Write a C++ statement to display the contents of the memory address​  Here the <data_type> is a type of data in which the variable can hold the types they are an integer, Sting, float and so on. <variable_name> is the name of a variable that holds the value in our application and <value> is assigning a specific value to the variable and finally <access_specifier> is used to give access permission for the variable

How to use the variable of one class into another class using java?, Note: If i put String value variable as public then it shows an error.Any ideas? First of all ur program was wrong. Basically U can When you use aaa obj =new an area of memory is created big enough to hold the class variables. You can  Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Local variables − Local variables are declared in methods, constructors, or blocks.

What are the differences between class variables and instance , Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. A class variable (  Value objects are always associated with one workspace or temporary variable. Value objects go out of scope when their variable goes out of scope or is cleared. There are no references to value objects, only copies that are independent objects. Value Object Behavior. Here is a value class that stores a value in its Number property.

Microsoft Excel VBA Professional Projects, Creating a custom collection class with Private instance variables and custom methods ensures that objects of the wrong type cannot be added to collection class holds a collection of objects instantiated from the clscell class . NOTE VBA collections are based on the value one , and there is no option for changing this . It specifies the type parameters (also called type variables) T1, T2, , and Tn. To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class. With this change, the Box class becomes:

  • You might want to think a little bit about your design, having a "shelf" with only a single book doesn't make much sense (unless it's a very small shelf... ;) )
  • Well the whole story was to make a game in c++ until I ran into this problem. I just made this code that replicates the problem, so that you guys wouldn't have to read through the mess that is the "real" project.