| | |
Home work guidance
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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?
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() )
Did I do this thing entirely wrong or am I doing it a harder way then I have to?
C++ Syntax (Toggle Plain Text)
// dogassignment.cpp : Defines the entry point for the console application. // David Tarantula #include "stdafx.h" #include <iostream> #include <string> using namespace std; class dog{ private: int age; int hungry; char gender; string name; public: string speak(); string eat(); string getName(); char getGender(); int getAge(); void setAge(int a); void setName(string n); void setGender(char g); // void printDetails(); }; int dog::getAge(){ return age; } void dog::setAge(int a){ age = a; } char dog::getGender(){ return gender; } void dog::setGender(char g){ gender = g; } string dog::getName(){ return name; } void dog::setName(string n){ name = n; } int main(){ dog davesDog; davesDog.setAge(1); davesDog.setName("Cody"); davesDog.setGender("M"); cout << "The dog is " << davesDog.getAge() << " years old." << endl << endl; cout << "The dog's gender is " << davesDog.getGender() << endl << endl; cout << "His name is " << davesDog.getName() << "." << endl; system("pause"); return 0; }
Last edited by Ancient Dragon; Feb 15th, 2009 at 7:52 am. Reason: replaced inlinecode tags with code tags
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
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.
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
The problem with the char is this line:
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"); "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
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
•
•
•
•
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.
•
•
•
•
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 StandardsDT; Feb 13th, 2009 at 5:23 pm.
if it's working, you should mark the thread as solved, it's my first one 
also, use [ icode] just for inline code

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
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
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
Instead of writing this:
string getName();
in the declaration and this as a definition:
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.
string getName();
in the declaration and this as a definition:
C++ Syntax (Toggle Plain Text)
string dog::getName() { return name; }
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
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.
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!
Oh and I will be sure to mark this as solved, promise!
cpp Syntax (Toggle Plain Text)
// dogassignment.cpp : Defines the entry point for the console application. // David Tarantula #include <iostream> #include <string> using namespace std; class dog{ private: int age; char gender; string name; public: string getName(); char getGender(); int getAge(); void setAge(int a); void setName(string n); void setGender(char g); // void printDetails(); }; int dog::getAge(){ return age; } void dog::setAge(int a){ age = a; } char dog::getGender(){ return gender; } void dog::setGender(char g){ gender = g; } string dog::getName(){ return name; } void dog::setName(string n){ name = n; } int main(){ dog davesDog; davesDog.setAge(1); davesDog.setName("Cody"); davesDog.setGender('M'); cout << "The dog is " << davesDog.getAge() << " years old." << endl << endl; cout << "Dog's Gender?: " << davesDog.getGender() << endl << endl; cout << "His name is " << davesDog.getName() << "." << endl; system("pause"); return 0; }
Last edited by StandardsDT; Feb 13th, 2009 at 6:06 pm.
I suppose you could just make printdetails use cout.
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
cpp Syntax (Toggle Plain Text)
void printDetails(dog dogObj){ cout << "The dog is " << dogObj.getAge() << " years old." << endl << endl; cout << "Dog's Gender?: " << dogObj.getGender() << endl << endl; cout << "His name is " << dogObj.getName() << "." << endl; }
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
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
•
•
•
•
I suppose you could just make printdetails use cout.
cpp Syntax (Toggle Plain Text)
void printDetails(dog dogObj){ cout << "The dog is " << dogObj.getAge() << " years old." << endl << endl; cout << "Dog's Gender?: " << dogObj.getGender() << endl << endl; cout << "His name is " << dogObj.getName() << "." << endl; }
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 tovoid printDetails(dog);
Thanks.Marking as solved.
![]() |
Similar Threads
- Unable to detect network computers (Networking Hardware Configuration)
- Work during your spare time and make money!! (eCommerce)
- Need guidance as to where to post question (Windows Software)
- How to re-organized XML file and call XML file from my software (RSS, Web Services and SOAP)
- Compression/Decompression Wave File to MP3 and Vice Versa (C++)
Other Threads in the C++ Forum
- Previous Thread: Error -> Help
- Next Thread: custom made queue/stack - having problems
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






