Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

using a member variable as a return value of class


  • Please log in to reply

#1
BysmuthMage

BysmuthMage

    New Member

  • Member
  • Pip
  • 2 posts
Is there a way to use a member variable of a class as the value of an instance of the class?

eg:
...
class number
{
int originalNumber,doubleOfNumber;
number(int theNumber)
{
originalNumber = theNumber;
doubleOfNumber = 2 * theNumber;
}
};

int main()
{
number twentySeven(27);
cout << twentySeven;
}

Is there a way to make this code (or something like it with changes) would write to the screen "27" without the need for 'twentySeven.getNumber()'?
  • 0

Advertisements


#2
kool808

kool808

    Visiting Staff

  • Member
  • PipPipPipPip
  • 1,690 posts
You created a class with the classname number, then you created a function with a function name number(int theNumber), wouldn't it be conflicting?

On the main() program, you declared to make an object twentySeven of class number, therefore it should be able to access only the function twentySeven.number() and NOT twentySeven.getNumber() <-- this function was not declared.

also that code would have been better if you used
int number(int theNumber){
<statements>;
return <var>;
}

then in the main() it should have been

cout << twentSeven.number();

Edited by kool808, 13 July 2005 - 05:34 PM.

  • 0

#3
BysmuthMage

BysmuthMage

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
My question was how you could get the value of 'originalNumber' without using 'twentySeven.getNumber()' or a similar function and without 'twentySeven.originalNumber.' And BTW, the 'function' number(int theNumber) was the constructor.
...
number(int theNumber) //constructor
...

probably would have led to less confusion.

Edited by BysmuthMage, 14 July 2005 - 07:19 AM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP