Home work guidance

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 130
Reputation: StandardsDT is an unknown quantity at this point 
Solved Threads: 0
StandardsDT's Avatar
StandardsDT StandardsDT is offline Offline
Junior Poster

Home work guidance

 
0
  #1
Feb 13th, 2009
I have a few assignments to get done, but I figured I'll post one to see if I'm the right track. If so then what I did will greatly help me.

Here are the requirements

Create a simple Dog class that has the following private fields (age (int), gender(char), name(string), hungry(boolean) and the following public functions(speak(), eat(), getName(), getAge(), getGender(), setAge(int), and printDetails() )
This is what I've written so far. I know there are errors, but this is mainly because I'm stuck at how to do the char. I also commented out the printDetails as I'm confused on how to go about doing that, so for now I resorted to cout.

Did I do this thing entirely wrong or am I doing it a harder way then I have to?

  1. // dogassignment.cpp : Defines the entry point for the console application.
  2. // David Tarantula
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. class dog{
  11.  
  12. private:
  13.  
  14.  
  15. int age;
  16. int hungry;
  17. char gender;
  18. string name;
  19.  
  20.  
  21. public:
  22.  
  23. string speak();
  24. string eat();
  25. string getName();
  26. char getGender();
  27. int getAge();
  28. void setAge(int a);
  29. void setName(string n);
  30. void setGender(char g);
  31. // void printDetails();
  32. };
  33.  
  34. int dog::getAge(){
  35.  
  36. return age;
  37.  
  38. }
  39.  
  40. void dog::setAge(int a){
  41.  
  42. age = a;
  43. }
  44.  
  45. char dog::getGender(){
  46.  
  47. return gender;
  48.  
  49. }
  50.  
  51. void dog::setGender(char g){
  52.  
  53. gender = g;
  54.  
  55. }
  56.  
  57. string dog::getName(){
  58.  
  59. return name;
  60.  
  61. }
  62.  
  63. void dog::setName(string n){
  64.  
  65. name = n;
  66.  
  67. }
  68.  
  69. int main(){
  70.  
  71.  
  72. dog davesDog;
  73.  
  74. davesDog.setAge(1);
  75. davesDog.setName("Cody");
  76. davesDog.setGender("M");
  77. cout << "The dog is " << davesDog.getAge() << " years old." << endl << endl;
  78. cout << "The dog's gender is " << davesDog.getGender() << endl << endl;
  79. cout << "His name is " << davesDog.getName() << "." << endl;
  80.  
  81. system("pause");
  82.  
  83. return 0;
  84.  
  85. }
Last edited by Ancient Dragon; Feb 15th, 2009 at 7:52 am. Reason: replaced inlinecode tags with code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Home work guidance

 
0
  #2
Feb 13th, 2009
Your code looks okay other than the lack of indentation. After 113 posts to the board I would have hoped you'd have learned how to preserve those details when posting code to this board by enclosing code in code tags as disscussed in the announcements and in the watermarks of the Message box.

My preference would be to avoid use of System call to pause, and you don't need stdaf.x to write standard code, but otherwise the concept of using accessor and mutator functions seems appropriate. For methods with one or two line bodies you can inline them if you want. Some people think that looks more "clean" than otherwise, and it is certainly less typing. Also, eventhough not needed in this class, I'd encourage you to get into the habit of writing your own default constructor, copy constructor, assignment constructor, and destructor-----at least until you know when the compilers default versions of those methods is adequate.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #3
Feb 13th, 2009
The problem with the char is this line:
davesDog.setGender("M"); "M" is a character array,
you are passing the letter M and a NULL character ('\0') to the setGender function (the NULL character ends a character array, and it is done automatically when using double quotes). You should use single quotes, meaning you are passing a char, and not a char*.

The code compiles and executes fine if I change the line to this:
davesDog.setGender('M');
Last edited by winrawr; Feb 13th, 2009 at 5:06 pm.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 130
Reputation: StandardsDT is an unknown quantity at this point 
Solved Threads: 0
StandardsDT's Avatar
StandardsDT StandardsDT is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #4
Feb 13th, 2009
Originally Posted by Lerner View Post
Your code looks okay other than the lack of indentation. After 113 posts to the board I would have hoped you'd have learned how to preserve those details when posting code to this board by enclosing code in code tags as disscussed in the announcements and in the watermarks of the Message box.

My preference would be to avoid use of System call to pause, and you don't need stdaf.x to write standard code, but otherwise the concept of using accessor and mutator functions seems appropriate. For methods with one or two line bodies you can inline them if you want. Some people think that looks more "clean" than otherwise, and it is certainly less typing. Also, eventhough not needed in this class, I'd encourage you to get into the habit of writing your own default constructor, copy constructor, assignment constructor, and destructor-----at least until you know when the compilers default versions of those methods is adequate.
I used the [ inlinecode ] tag as described in the announcements but I guess it didn't work, or I used the wrong code. Sorry about that! Thank You for your advise, when you say inline could you give me an example? I honestly haven't programmed since last Spring (Java Class) so everything is very rusty to me.


Originally Posted by winrawr View Post
The problem with the char is this line:
davesDog.setGender("M"); "M" is a character array,
you are passing the letter M and a NULL character ('\0') to the setGender function (the NULL character ends a character array, and it is done automatically when using double quotes). You should use single quotes, meaning you are passing a char, and not a char*.

The code compiles and executes fine if I change the line to this:
davesDog.setGender('M');
Thank You! That makes a lot of sense, I kept looking through my text book "Absolute C++ 3rd Edition" for a better explanation of char but I didn't see anywhere that explained the difference between double quote and single quote. This makes much more sense now.
Last edited by StandardsDT; Feb 13th, 2009 at 5:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #5
Feb 13th, 2009
if it's working, you should mark the thread as solved, it's my first one
also, use [ icode] just for inline code like this , but if you're pasting C++ code, use [ code=cpp] ... [ /code] and it will preserve indentation and syntax highlighting
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Home work guidance

 
0
  #6
Feb 13th, 2009
Instead of writing this:

string getName();

in the declaration and this as a definition:
  1. string dog::getName()
  2. {
  3. return name;
  4. }

you could do this in the declaration:

string getName() {return name;}

and leave out the explicit definition. I would only recommend that for methods which have a single line or two of simple, straightforward code. The formal approach as you have demonstrated is perfectly acceptable, too.

I use [ code ] and [ /code ] without the spaces between the brackets and letters when posting code. There is another version to get line numbers in front of the code but I don't use it and don't remember what it is.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 130
Reputation: StandardsDT is an unknown quantity at this point 
Solved Threads: 0
StandardsDT's Avatar
StandardsDT StandardsDT is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #7
Feb 13th, 2009
Originally Posted by winrawr View Post
if it's working, you should mark the thread as solved, it's my first one
also, use [ icode] just for inline code like this , but if you're pasting C++ code, use [ code=cpp] ... [ /code] and it will preserve indentation and syntax highlighting
Ah ok! Thanks. I didn't want to mark it as solved just yet. I'm finishing up the code at the moment and wanted to paste it in here to give it one final look over for suggestions.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 130
Reputation: StandardsDT is an unknown quantity at this point 
Solved Threads: 0
StandardsDT's Avatar
StandardsDT StandardsDT is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #8
Feb 13th, 2009
So I decided to do away with the hungry, eat and speak. I'm not exactly sure what my professor is looking for me to do with those. I'm not sure if he want's it to be that if the dog speaks he is hungry or what. I figured I'll email him and ask shortly. So with that said how do I get the "printDetails();" to display the out put of everything instead of cout? I've only used cout so far up to this point to get anything to display. Guidance or tips on this is appreciated.

Oh and I will be sure to mark this as solved, promise!

  1. // dogassignment.cpp : Defines the entry point for the console application.
  2. // David Tarantula
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class dog{
  10.  
  11. private:
  12.  
  13.  
  14. int age;
  15. char gender;
  16. string name;
  17.  
  18.  
  19. public:
  20.  
  21. string getName();
  22. char getGender();
  23. int getAge();
  24. void setAge(int a);
  25. void setName(string n);
  26. void setGender(char g);
  27. // void printDetails();
  28. };
  29.  
  30. int dog::getAge(){
  31.  
  32. return age;
  33.  
  34. }
  35.  
  36. void dog::setAge(int a){
  37.  
  38. age = a;
  39. }
  40.  
  41. char dog::getGender(){
  42.  
  43. return gender;
  44.  
  45. }
  46.  
  47. void dog::setGender(char g){
  48.  
  49. gender = g;
  50.  
  51. }
  52.  
  53. string dog::getName(){
  54.  
  55. return name;
  56.  
  57. }
  58.  
  59. void dog::setName(string n){
  60.  
  61. name = n;
  62.  
  63. }
  64.  
  65. int main(){
  66.  
  67.  
  68. dog davesDog;
  69.  
  70. davesDog.setAge(1);
  71. davesDog.setName("Cody");
  72. davesDog.setGender('M');
  73.  
  74.  
  75. cout << "The dog is " << davesDog.getAge() << " years old." << endl << endl;
  76. cout << "Dog's Gender?: " << davesDog.getGender() << endl << endl;
  77. cout << "His name is " << davesDog.getName() << "." << endl;
  78.  
  79. system("pause");
  80.  
  81. return 0;
  82.  
  83. }
Last edited by StandardsDT; Feb 13th, 2009 at 6:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #9
Feb 13th, 2009
I suppose you could just make printdetails use cout.

  1. void printDetails(dog dogObj){
  2.  
  3. cout << "The dog is " << dogObj.getAge() << " years old." << endl << endl;
  4. cout << "Dog's Gender?: " << dogObj.getGender() << endl << endl;
  5. cout << "His name is " << dogObj.getName() << "." << endl;
  6.  
  7. }

and then use printDetails(davesDog) in the main function.

Note that you'd have to change the definition of printDetails at the top of the code to void printDetails(dog);
Last edited by winrawr; Feb 13th, 2009 at 6:26 pm.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 130
Reputation: StandardsDT is an unknown quantity at this point 
Solved Threads: 0
StandardsDT's Avatar
StandardsDT StandardsDT is offline Offline
Junior Poster

Re: Home work guidance

 
0
  #10
Feb 13th, 2009
Originally Posted by winrawr View Post
I suppose you could just make printdetails use cout.

  1. void printDetails(dog dogObj){
  2.  
  3. cout << "The dog is " << dogObj.getAge() << " years old." << endl << endl;
  4. cout << "Dog's Gender?: " << dogObj.getGender() << endl << endl;
  5. cout << "His name is " << dogObj.getName() << "." << endl;
  6.  
  7. }

and then use printDetails(davesDog) in the main function.

Note that you'd have to change the definition of printDetails at the top of the code to void printDetails(dog);
That worked like a charm. Just learned something new Thanks.

Marking as solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC