Small problem

0 nikhil kumar · May 10, 2015
Guys i have written a code similar to bucky's 13 video but it wont show any display help plsss

#include <iostream>
using namespace std;


class numberdisp
{

public:
    numberdisp(int z)
{
    setnum(z);
}
    void setnum( int x)
    {
         y=x;
    }

    int getnum()
    {
        return y;
    }
    private:
  int y;

};
int main()
{
numberdisp object(6) ;


return 0;
}



Post a Reply

Replies

Oldest  Newest  Rating
0 Lé Pig · May 10, 2015
I modified his code a bit and it seems like it would be spot on with the tutorial but still, no number output. Howcome?


#include

using namespace std;

class numberdisp
{

public:

 int setnum(int z)
{
number = z;
}

  int getnum()
{
      return number;
}

  private:
     int number;

};

int main()
{

numberdisp numObject;
numObject.setnum(6);

}
0 Lé Pig · May 10, 2015
Care to elaborate?
0 Lé Pig · May 10, 2015
class name looks good, now without using strings and instead using ints like the OP wanted to I changed a few things.  the public function setnum and getnum seem to be good, the private declares the variable number to be an int. We accessed the class with an object, we used a 6 in the parameter as if it were a string like in the video. I'm not sure what we're doing wrong. Although, if you're not interested in helping you don't have to string us along you know.
0 Michael Bradford · May 11, 2015
I think what Krootushas is trying to get at is this, what exactly in the main() function would display anything from the class numberDisp? 












You create an object and set the private y variable to 6.. but you never call getnum() [which will return y] OR call out the returned bit from getnum() so ofcourse there's no output you never ASKED for an output.
0 Michael Bradford · May 11, 2015
Also just to clarify a statement like this somewhere in main would probably get you your output


cout 
0 Michael Bradford · May 11, 2015
Ack Code box thing interpretted what I wrote, sorry should look like this

cout << numObject.getNum();
0 Lé Pig · May 11, 2015
So then the main should look like this?

numberdisp numObject;
numObject.setnum(7);
cout
0 Lé Pig · May 11, 2015
alright... sorry it should look like this

numberdisp numObject;
numObject.setnum(7);
cout << numObject.getnum();


so in bucky's tutorial I have to admit I was being a dumdum and I didn't wait til the end to see the final result.
+1 Lé Pig · May 11, 2015
so to the nikhil, I think the code you're trying to write should look like this more or less. What you want to do is use less variables

#include <iostream> 

using namespace std;

class numberdisp    // created your class which is used to store functions. Think of your program as if it were a book. The class is a book, the functions (group of statements) are a chapter and the statements (anything ending with a ' ; ') are the sentences.
{

public:

 int setnum(int z) 
// only need the variables 'setnum', 'z' and 'number' at this point
{
number = z;
}

  int getnum() 
//now you create a function named 'getnum' to RETURN the number you will input in int main()
{
      return number;
}

  private:     
// now you declare the variable  'number' (though I'm not entirely sure why now...i should watch the vid again)
     int number;

};

int main()
{

numberdisp numObject;
// you create an "object" that acts as a key to access the class 'numberdisp'



numObject.setnum(7);  // you use that object to now access the function 'setnum()' and change the 'z' and/or 'number' to any  number you like


cout << numObject.getnum();  //now you output a message which is the function 'getnum()' but you need to use your "object" again as a key to access that function inside your class 'numberdisp'

}


I hope that I'm not too wrong about any of this and I hope it made sense...
0 Maciej Gozdek · May 11, 2015
To display the 6 you need to put cout << object.getnum(); in main. Might as well end line if you want to. I know I would.
  • 1

C++

107,070 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator