Classes and Objects Assignment

Hello,

I need help with this assignement. What I have below is all i know. I'm a beginner

For this assignment you will write a class called Dog that has the following member variables:

birthyear. An int that holds the dog’s birth year.
breed. A string that holds the breed of dog.
vaccines. A Boolean holding a yes/no value indicating whether the dog is currently on vaccinations.
In addition, the class should have the following member functions:

Constructor. The constructor should accept the dog’s birthyear, breed and vaccines as arguments and assign these values to the object’s birthyear, breed and vaccines member variables.
Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object’s birthyear, breed and vaccines member variables.
Demonstrate the class in a program that creates a Dog object. The user should enter all input. Be sure to include comments throughout your code where appropriate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <iostream>
#include <string>
#include <time.h>
#include <iomanip>
using namespace std;

class Dog {
private:
	int birth_year;
	string breed;
	bool vaccines;
public:
	Dog() {
		cout << "Please enter birth year " << endl;
		cin >> birth_year;
		cout << "Please enter breed" << endl;
		cin >> breed;
		cout << "Please enter if vaccinated" << endl;
		cin >> vaccines;
	}

};

int main() {
	Dog myobj;
	cout << "Enter Birth Year" << endl;
	cin >> myobj.birth_year;

.
that is a good start.
good design dictates that you split input and output away from everything else. For now, memorize this concept, later, you will come to grasp why. A class should usually not do cin/cout statments inside it, but return data to the caller that can then be piped to cin/cout, or a disk file, or a network, or a graphical window, or .... (this is the why, you can't assume how the class's user will want to output the data).
also the assignment asks for you to do that :)

The constructor should accept the dog’s birthyear, breed and vaccines as arguments

Dog(int by, string brd, bool vac)
{ birth_year = by; breed = breed; vaccines = vac;}

eg
Dog bowser(2017, "mutt", false); //you would read these from the user and maybe update them via the user after to show the getter/setter working.

accessor ... means getter and setter, eg
int getby() {return birth_year;}
or a setter
void setby(int by) {birth_year = by;}
you need a set of those for each variable in the class that the user wants to have access to.
Last edited on
Hi

Thanks for your help. However, I'm a beginner. I don't understand the changes you are recommending. can you clarify or break it down
This is what I tried but it still doesnt work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <time.h>
#include <iomanip>
using namespace std;

class Dog {
private:
	int birth_year;
	string breed;
	bool vaccines;
public:
	Dog(int birth_year, string breed, bool vaccines) 
	{
		birth_year = by; breed = breed; vaccines = vac;
	}
public:
	int getBirth_year()
	{
		return birth_year;
	}
	string getBreed()
	{
		return breed;
	}
	bool getVaccines()
	{
		return vaccines;
	}

int main() {
	cout << "Enter Birth Year" << endl;
	cin >> myobj.birth_year;
	cout << "Enter Dog Breed" << endl;
	cin >> myobj.breed;
	cout >> "Does your dog has vaccines?" << endl;
	cin >> myobj.vaccines;

}
If you use the same name as a class variable in the class methods you have to use an extra, ugly, and unnecessary syntax:
void bad(string breed) {this->breed=breed;} //it needs to know the class variable vs the parameter with clarifications added
if you change the name to anything else, you do not:
void better(string brd) {this->breed=brd;}

I have fixed the code to a point where you now have a working program, but there are many things left to do for your assignment, everything you DO need to do for the assignment is now in there as an example I believe. See what you can do now.

Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
//#include <time.h>
#include<ctime> //time.h is the C header file, use ctime for c++ 
#include <iomanip>
using namespace std;

class Dog 
{
//private:  default is private.  not needed. 
	int birth_year;
	string breed;
	bool vaccines;
public:
	Dog(int by, string brd, bool vac) 
	{
		birth_year = by; breed = brd; vaccines = vac;
	}
//public:  these are like flags once on, its public until you say private again.  its still public here.
	int getBirth_year()
	{
		return birth_year;
	}
	string getBreed()
	{
		return breed;
	}
	bool getVaccines()
	{
		return vaccines;
	}
	void setBirth_year(int by)
	{
		birth_year = by;
	}
};  //this line was missing and its hard to see with misaligned bracket style coding.  
//I prefer all brackets align for this reason, but you can do what you like. 

int main() 
{
	Dog fido(2015, "mutt", true);
	cout << "fido was born in "<< fido.getBirth_year() << endl;
	
	cout << "Enter new Birth Year" << endl;
	int b;
	cin >> b;
	fido.setBirth_year(b);
	cout << "fido was now born in "<< fido.getBirth_year() << endl;
	//cin >> myobj.birth_year;  //myobj does not exist.  
	//cout << "Enter Dog Breed" << endl;
	//cin >> myobj.breed;
	//cout >> "Does your dog has vaccines?" << endl;
	//cin >> myobj.vaccines;

}


there are ways to allow you to cin to the object but that is too much to go into at this point in your studies. You should just get the data and set it with the setter for now, as I did above.
Last edited on
Hi.

Thanks so much. Like I said, I'm a beginner learning C++ so i don't know much. So what other things do I need to do like you mentioned ?
read the assignment *carefully* and check off each part of it. It has nothing to do with c++, I just didnt do all the redundant tasks in your homework. I only did enough to show you the way.

Just reading it, you would see these gems:

Be sure to include comments throughout your code where appropriate. did we do this? I suggest removal of commented out junk, as well.

The user should enter all input. Did we do this? (I hard-coded the object's values... so no, we didnt).

Double check it all, make sure you don't lose points for not doing what was asked.

not asked for, but to stir your creative juices and thinking a bit... normally, one would have an identifier in an object. What if you wanted a list/vector/array of dogs? How can you tell spot from fido and so on? You can't! I would not bother to do that in this program, but keep this in mind: when storing data of this type (effectively, user-data), you usually want a 'key' that you can search on to get the data back. You will see this idea later, but think about it a bit in the back of your mind as you work these early problems, take note of whether you could actually USE the thing you made easily, in a program... and if not, what is lacking...
Last edited on
I honestly don’t know what else to do or what I’m missing. That’s why I’m kindly asking. If I knew what to do, I wouldn’t be bothering with asking for help.

You mentioned the user input. Where does the input go ? And what specifically am I asking
Carefully look at this and how gets and sets work.

The point jonnin is making is to keep the output processing, cin's and cout's, out of the class. That way your dog can be used on other platforms rather than just the console. In other words a Dog tells you what it's breed is (as a string via getBreed() ) but doesn't print it out for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>

using namespace std;

class Dog {
private:
    // SOME SORT OF DEFAULTS PLAN
    int birth_year = 1980;
    string breed = "Mystery dog";
    bool vaccines = false;
    
public:
    Dog()
    {
        // LIFELESS AND PAPERLESS DOG
    }
    
    Dog(int aBYear, string aBreed, bool anyVax)
    {
        birth_year = aBYear;
        breed = aBreed;
        vaccines = anyVax;
    }
    
public:
    void setBreed( string aBreed) // NEED SET's FOR ALL MEMBERS
    {
        breed = aBreed;
    }
    
    int getBirth_year()
    {
        return birth_year;
    }
    
    string getBreed()
    {
        return breed;
    }
    
    bool getVaccines()
    {
        return vaccines;
    }
}; // <-- END OF CLASS

int main() {
    // MAKE A DOG
    Dog my_lifeless_dog; // THIS IS A GENERAL DOG - ie no breed, nothing!
    cout << my_lifeless_dog.getBreed() << '\n';
    
    Dog my_fullblown_dog(1954, "Retriever", true);
    cout << my_fullblown_dog.getBreed() << '\n';
    
    // LET'S UPDATE my_lifeless_dog
    string new_breed;
    cout << "What is this dog's real breeed? ";
    getline( cin, new_breed);
    my_lifeless_dog.setBreed(new_breed);
    cout << "Whoopee! " << my_lifeless_dog.getBreed() << '\n';
    
    return 0;
}
1
2
3
4
5
6
    Dog( int aBYear, string aBreed, bool anyVax )
    {
        birth_year = aBYear;
        breed = aBreed;
        vaccines = anyVax;
    }

That constructor assigns values to members.

Constant and reference members and base classes (that some classes have) cannot be assigned to.
They have to be initialized with correct values.

There is a member initializer list syntax for that.
It is recommended to use that syntax:
1
2
3
4
5
6
    Dog( int aBYear, string aBreed, bool anyVax )
    :   birth_year {aBYear},
        breed {aBreed},
        vaccines {anyVax}
    {
    }
aunty Gladys keskiverto strikes again with irrelevant nit-picking :)
Registered users can post here. Sign in or register to post.