Beginner looking for help
-
Problem seems simple, hours later and no documentation, I see I am licked. I am doing an exercise with a Win32 Console app, and I am attempting to write 2 overload functions to pass 2 similiar class objects to the function and recieve different results based on what I am passing. The parameters of the exercise are quite clear and as I have written thus far. Certainly there are better ways to code the end result, but I am not in control of the requirements. :( I am passing 2 char class objects and trying to get it to do 2 different things. What I can figure out is how to beat ambiguity. What do I have to pass it in order make this work. Any help for the idiot league of programming would be appreciateed. My code so far, notes attached Using Visual C++ compiler #ifndef _CLASSCAT121212121212112_ // 2 classes declared a cat and a dog #define _CLASSCAT121212121212112_ class CCat { public: char* catname; }; #endif #ifndef _CLASSDOG5456454654656546_ #define _CLASSDOG5456454654656546_ class CDog { public: char* dogname; }; #endif #include #include "Cat.h" #include "Dog.h" void speak(char* n); void speak(char* &n); void main() { CCat cat1; CDog dog1; cat1.catname = "Felix"; dog1.dogname = "Fido"; speak("Felix"); speak(); <--- // how do I pass the dog argument to the function? How do I differentiate the two } void speak(char* n) { cout << n << " says meow " << endl; } void speak() // must make the dog go woof, but how do I pass it a different argument? // how do I overload to beat ambiguity { cout << d << " says woof " << endl; } Much Thanks, Jason K. Dove
-
Problem seems simple, hours later and no documentation, I see I am licked. I am doing an exercise with a Win32 Console app, and I am attempting to write 2 overload functions to pass 2 similiar class objects to the function and recieve different results based on what I am passing. The parameters of the exercise are quite clear and as I have written thus far. Certainly there are better ways to code the end result, but I am not in control of the requirements. :( I am passing 2 char class objects and trying to get it to do 2 different things. What I can figure out is how to beat ambiguity. What do I have to pass it in order make this work. Any help for the idiot league of programming would be appreciateed. My code so far, notes attached Using Visual C++ compiler #ifndef _CLASSCAT121212121212112_ // 2 classes declared a cat and a dog #define _CLASSCAT121212121212112_ class CCat { public: char* catname; }; #endif #ifndef _CLASSDOG5456454654656546_ #define _CLASSDOG5456454654656546_ class CDog { public: char* dogname; }; #endif #include #include "Cat.h" #include "Dog.h" void speak(char* n); void speak(char* &n); void main() { CCat cat1; CDog dog1; cat1.catname = "Felix"; dog1.dogname = "Fido"; speak("Felix"); speak(); <--- // how do I pass the dog argument to the function? How do I differentiate the two } void speak(char* n) { cout << n << " says meow " << endl; } void speak() // must make the dog go woof, but how do I pass it a different argument? // how do I overload to beat ambiguity { cout << d << " says woof " << endl; } Much Thanks, Jason K. Dove
Just a few thoughts: void speak(char* n); void speak(char* &n); This is never going to work. The whole idea of a class should mean that it knows how to take care of it's own behaviour. Consider what you're trying to do here, and how this would work/look/be easy to read in a real world application. Each of your classes should have it's own 'speak' function, in the class, which 'speaks' woof or meow. The common way to do this is to have them derive from an abstract base class called Animal, then you can make an Animal * into a Dog or a Cat, and it will know how to speak. If you don't need this behaviour, then just put the speak() methods in the classes and call them from there. void main() { CCat cat1; // 2 instantiations one of cat one of dog CDog dog1; As an aside, this comment is unnecessary. A good comment would explain *why* you've created this variables/what they are for/etc., not repeat the obvious. I hope that helps. If not, just ask again and I'll be happy to further clarify :) Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.
-
Just a few thoughts: void speak(char* n); void speak(char* &n); This is never going to work. The whole idea of a class should mean that it knows how to take care of it's own behaviour. Consider what you're trying to do here, and how this would work/look/be easy to read in a real world application. Each of your classes should have it's own 'speak' function, in the class, which 'speaks' woof or meow. The common way to do this is to have them derive from an abstract base class called Animal, then you can make an Animal * into a Dog or a Cat, and it will know how to speak. If you don't need this behaviour, then just put the speak() methods in the classes and call them from there. void main() { CCat cat1; // 2 instantiations one of cat one of dog CDog dog1; As an aside, this comment is unnecessary. A good comment would explain *why* you've created this variables/what they are for/etc., not repeat the obvious. I hope that helps. If not, just ask again and I'll be happy to further clarify :) Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.
thank you so much for the swift reply, I'm glad I'm not the only one up browsing the boards. I agree with all the points you made, and it certainly is quite easier to handle with separate, 1 class called functions. Unfortunately for me, the book I'm using seems to think it is a simple exercise, and the parameters are quiet clear. I must perform it as I posted it to the board. I played with the pointer to the memory location, like you first suggested, though only briefly. I will go back and try it again. The one question that keeps popping into my mind isn't how to overload the functions. It is how to pass the different arguments. If I can figure out how to differentiate which class object I am passing, then it can work. *deep, deeeeeep sigh* Much Appreciatively, Jason K. Dove
-
thank you so much for the swift reply, I'm glad I'm not the only one up browsing the boards. I agree with all the points you made, and it certainly is quite easier to handle with separate, 1 class called functions. Unfortunately for me, the book I'm using seems to think it is a simple exercise, and the parameters are quiet clear. I must perform it as I posted it to the board. I played with the pointer to the memory location, like you first suggested, though only briefly. I will go back and try it again. The one question that keeps popping into my mind isn't how to overload the functions. It is how to pass the different arguments. If I can figure out how to differentiate which class object I am passing, then it can work. *deep, deeeeeep sigh* Much Appreciatively, Jason K. Dove
You *could* put a function in to the cat/dog classes that tells you what type of animal it is, and ( again ) derive from a base class. You could also use Run Time Type Identification. This is almost *always* a terrible idea, and I'm wondering why this book wants you to do things in such an ugly manner. It's not that it *can't* be done, it's that there is no reason why it *should*. If you're passing in the name of the animal and the cat/dog classes are visible to the function, then it is also easy - just write one function and check the name passed in for a known list of cats and dogs. Again, not extensible or practical, but it works in the scope of the example you're working with. Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.
-
thank you so much for the swift reply, I'm glad I'm not the only one up browsing the boards. I agree with all the points you made, and it certainly is quite easier to handle with separate, 1 class called functions. Unfortunately for me, the book I'm using seems to think it is a simple exercise, and the parameters are quiet clear. I must perform it as I posted it to the board. I played with the pointer to the memory location, like you first suggested, though only briefly. I will go back and try it again. The one question that keeps popping into my mind isn't how to overload the functions. It is how to pass the different arguments. If I can figure out how to differentiate which class object I am passing, then it can work. *deep, deeeeeep sigh* Much Appreciatively, Jason K. Dove
I got it. Thank you for all your help.;) It help to have perspective Dove #include #include "Cat.h" #include "Dog.h" void speak(CCat c); void speak(CDog d); void main() { CCat cat1; CDog dog1; cat1.catname = "Felix"; dog1.dogname = "Fido"; speak(cat1); speak(dog1); } void speak(CCat c) { cout << c.catname << " says meow " << endl; } void speak(CDog d) { cout << d.dogname << " says woof " << endl; }
-
thank you so much for the swift reply, I'm glad I'm not the only one up browsing the boards. I agree with all the points you made, and it certainly is quite easier to handle with separate, 1 class called functions. Unfortunately for me, the book I'm using seems to think it is a simple exercise, and the parameters are quiet clear. I must perform it as I posted it to the board. I played with the pointer to the memory location, like you first suggested, though only briefly. I will go back and try it again. The one question that keeps popping into my mind isn't how to overload the functions. It is how to pass the different arguments. If I can figure out how to differentiate which class object I am passing, then it can work. *deep, deeeeeep sigh* Much Appreciatively, Jason K. Dove
Why don't you use a dummy argument in the second speak function i.e. to overload it like it is used for pre/post increment/decrement operators void speak(char *str); //For cat object void speak(char *str, int dummy);//For dog object Really speaking an elegant solution is as Christian suggested using inheritance / virtual functions... :cool: --------------------------- Atul
-
Problem seems simple, hours later and no documentation, I see I am licked. I am doing an exercise with a Win32 Console app, and I am attempting to write 2 overload functions to pass 2 similiar class objects to the function and recieve different results based on what I am passing. The parameters of the exercise are quite clear and as I have written thus far. Certainly there are better ways to code the end result, but I am not in control of the requirements. :( I am passing 2 char class objects and trying to get it to do 2 different things. What I can figure out is how to beat ambiguity. What do I have to pass it in order make this work. Any help for the idiot league of programming would be appreciateed. My code so far, notes attached Using Visual C++ compiler #ifndef _CLASSCAT121212121212112_ // 2 classes declared a cat and a dog #define _CLASSCAT121212121212112_ class CCat { public: char* catname; }; #endif #ifndef _CLASSDOG5456454654656546_ #define _CLASSDOG5456454654656546_ class CDog { public: char* dogname; }; #endif #include #include "Cat.h" #include "Dog.h" void speak(char* n); void speak(char* &n); void main() { CCat cat1; CDog dog1; cat1.catname = "Felix"; dog1.dogname = "Fido"; speak("Felix"); speak(); <--- // how do I pass the dog argument to the function? How do I differentiate the two } void speak(char* n) { cout << n << " says meow " << endl; } void speak() // must make the dog go woof, but how do I pass it a different argument? // how do I overload to beat ambiguity { cout << d << " says woof " << endl; } Much Thanks, Jason K. Dove
Hmm.. I think this is what your exercise want you to do! void speak (CCat s) { cout << "Shut the fuck up! I'm trying to get a moment of rest here!!\n"; } void speak (CDog s) { cout << "Woof!\n"; } // ^ You overload the "speak"-function with different acts // // The compiler will automatically choose the "speak" matching // your arguments. int main [..blabla..] { CCat cat1; CDog dog2; cout << "The dog says.. "; speak (dog1); cout << "And the cat goes.."; speak (cat1); } Hope this will do the trick! Gustav Tresselt
-
Hmm.. I think this is what your exercise want you to do! void speak (CCat s) { cout << "Shut the fuck up! I'm trying to get a moment of rest here!!\n"; } void speak (CDog s) { cout << "Woof!\n"; } // ^ You overload the "speak"-function with different acts // // The compiler will automatically choose the "speak" matching // your arguments. int main [..blabla..] { CCat cat1; CDog dog2; cout << "The dog says.. "; speak (dog1); cout << "And the cat goes.."; speak (cat1); } Hope this will do the trick! Gustav Tresselt
-
Why don't you use a dummy argument in the second speak function i.e. to overload it like it is used for pre/post increment/decrement operators void speak(char *str); //For cat object void speak(char *str, int dummy);//For dog object Really speaking an elegant solution is as Christian suggested using inheritance / virtual functions... :cool: --------------------------- Atul
I understand you're suggesting this as a last resort to try and get the desired behaviour, but I just *have* to say for the record that I thought this to be fairly obvious, but too ugly to suggest :eek: I realise you're not suggesting it as a real world solution, so please don't be offended :rose: Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.
-
I understand you're suggesting this as a last resort to try and get the desired behaviour, but I just *have* to say for the record that I thought this to be fairly obvious, but too ugly to suggest :eek: I realise you're not suggesting it as a real world solution, so please don't be offended :rose: Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.
No offence taken as the code is indeed ugly and does not even count as the last resort in real world probs. But as he suggested he had to do it according to the book so the suggestion... Should include a disclaimer...:-D