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?

// 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;

}

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');

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.

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.

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

Instead of writing this:

string getName();

in the declaration and this as a definition:

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.

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.

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!

// 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;

}

I suppose you could just make printdetails use cout.

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);

I suppose you could just make printdetails use cout.

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);

That worked like a charm. Just learned something new :) Thanks.

Marking as solved.

This question has already been answered. Start a new discussion instead.