How do I send this number to a function and print it to screen.:
Code:cout << "The number: " ; MyNumber.print(300);
How do I send this number to a function and print it to screen.:
Code:cout << "The number: " ; MyNumber.print(300);
Why would you need to send it to a function to print ?
..but the function could be like..
but a simple statementCode:void MyNumberClass::print(int x) { std::cout<<x; }
would be enough...Code:cout<<"The Number: "<<x;
I just got use to doing it with pointers and and such a few days ago. It's no fun taking the easy way out with cout all the time. Now I can add this to my experence. It's the easy stuff that seems the hardest and I think I did this once before. Anyway, once I get it to work, I never forget it... Thank you
If you want to try out extra features, doing them on what does not require them warrant later problems. Instead find some problems to solve which does need them.
I learn a lot though trial and error even when I might not solve the original problem, like now, but what I did learn will be re-tested doing my next adventure. I been at the finish line for hours, it's hard not to Press "F9" to continue.
It's really scary how we can create bugs in our project months ago and still might not realize it today.
Printing a class - C / C++ answers
I think I'm getting close. This got to be the preferred method because I can set a value like (300) where before it would not even compile. Problem now is it don't show the output. Could you tell me what do I need to add or do to make the result print to screen. My virtual print function is in the base class for my two derived classes, so this is my only issue.
Thanks in advance
Code:class NumberA : public NUMBER { public: int valueA; NumberA() { valueA = 0; } virtual int print(int x) { return valueA; } };
Code:int main() { NumberA numberA; cout << "Number-A: " ; numberA.print(300);
Last edited by sharris; 04-16-2011 at 03:19 PM.
You mean to tell me that nobody got a clue or even interested in one of the greatest C++ questions EVER!!! I needed no help up until now. This is my creation from scratch with this additional new function I'm trying to make work. Those who know me knows I always add something fancy to my projects, every-week and that is what usually, ALWAYS what my questions related to.
I might not be an expert but I have since enough to know, that what I am missing, is so minor it's a pitty that these expert members here don't have a clue or don't want to share or simply want to know why sharris is always trying new or silly ways of doing things, but they will tell me the why not's, but they never will explore. That a pitty too.
sharris, sharris, it seems that you are embarking on some new and unknown technology again???
I got it! I got IT!!!
Code:#include <iostream> using namespace std; class NumberA { public: int valueA; NumberA() { valueA; } virtual int print(int valueA) { cout << valueA; } }; int main() { NumberA numberA; cout << "Number-A: " ; numberA.print(300); cout << endl; cout << endl; cin.get(); return 0; }