77242

output of program is not same as passed argument

Why I am getting different out put of both the width and height variable in below program

#include <iostream> using namespace std; class my_space { public : void set_data(int width,int height) // taking the same name of the variable as the class member functions { width = width; height = height; } int get_width() { return width; } int get_height() { return height; } private : int width; int height; }; int main() { my_space m; m.set_data(4,5); // setting the name of private members of the class cout<<m.get_width()<<endl; cout<<m.get_height()<<endl; return 0; }

getting below output of the program

sh-4.3$ main 1544825248 32765

Answer1:

The problem here is that int width and int height in function parameter list hide the width and height class member variables since the have the same name. What your function does is assign the passed in values to themselves and then exist. This means the width and height in the class are left uninitialized and they hold some unknown value. What you need to do if you want the names to be the same is use the this pointer to diferentiate the names like

void set_data(int width,int height) // taking the same name of the variable as the class member functions { this->width = width; this->height = height; }

Now the compiler knows which is which. You could also just name the function parameters to something else and then you would not need to use this->.

Also instead of having a set function you could use a constructor and initialize the object when you create it. A constructor like

my_space(int width = 0, int height = 0) : width(width), height(height) {}

<sup>Here we can use the same names as the compiler knows which one is the member and which one is the parameter</sup>

Will always make sure the class is at least default constructed to a known state or you can provide your own values to make it a non-default state.

Recommend

  • How to set a clicking clock in a pygame window
  • How to animate scale and rotate in pygame using rotozoom
  • Bar chart in seaborn
  • Group list of tuples by item
  • Scipy Leastsq Optional Output Variable (Mesg)
  • Trying to get the char code of ENTER key
  • Dynamically switching connect in Modelica
  • SetWindowsHookEx does not react on media keys
  • calculate gradient output for Theta update rule
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Problem while Building a Setup Project for a windows Service?
  • How to attach a node.js readable stream to a Sendgrid email?
  • Functions in global context
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Why value captured by reference in lambda is broken? [duplicate]
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Modifying destination and filename of gulp-svg-sprite
  • Deserializing XML into class C#
  • Javascript + PHP Encryption with pidCrypt
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Why winpcap requires both .lib and .dll to run?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • python draw pie shapes with colour filled
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?