954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

invoking class functions from user input

Supposing I have a class Dog with attributes age and color. I also have a function print(string) which invokes the accessor functions for the dog attributes. Now when the user enters the name of a Dog object at the command line I would like to call the print function to print these attributes to the screen.
string name;
cout << "Enter the name of a dog";
cin << name;
cout << print(name);

Now, how do I cast the string data type entered by the user into a Dog object so that I can invoke the class functions directly?
Or am I going about it the wrong way?

Please help, this is not homework, I am just trying to learn!

spider_pig
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

The easiest way would be to do an ifcheck on the input and then set a variable to a number then switch-block the number, ie. User input's Labrador which makes dog_id equal 1, then in the switch-block for case 1 call the function for the dog object with the type of dog being Labrador

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You