Classes and objects

I need help with this assignment here. The code below is all i have so far and I'm stuck.

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
#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 main() {
	Dog myobj;
	cout << "Enter Birth Year" << endl;
	cin >> myobj.birth_year;




}
Last edited on
Hello cblack618,

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.

* Your class doesn't have a constructor : see the following link for ressource on how to make and use one : http://www.cplusplus.com/doc/tutorial/classes/

* An accessor is a public function which as is said in your instruction, should retrieve (return) data stored in your class variable, which in most case should be made private btw.

You need to do a couple of things:
1_ Make your class variables private
2_ Create a constructor which is going to take 3 arguments: the dog's birth year, whether it was vaccined an its breed. Your constructor should be public.
3_ Create one public accessor for each class variable.

Hope this helps.

EDIT: You've included the following headers: iomanip and time.h, you're not actually using them in your program.
Last edited on
Hello,

I'm just a intro to C++ student in school. So I'm just a beginner and only know the bare basics. I see the changes you recoommended (1-3) but don't know how to do that
This would be strange if you didn't know how to do this. I assume your teacher only gives you an assignment if you have beforehand learned all the tools necessary to complete said assignment.

That being said, I suggest you read the article on classes I linked in my previous post, so that you get an understanding on how classes work - being private and public members or constructors.

For an explanation on accessors (and mutators) see: https://ccm.net/faq/29359-accessors-and-mutators-in-c

This should be enough knowledge for you to complete your assignment. If you still have questions, feel free to ask specific questions about what you don't understand :)
Topic archived. No new replies allowed.