C++ Classes Program

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
//Defintion of Dog Class
class Dog {
public:
	Dog(); //Constructor
	~Dog(); //Destructor
	void setName(string name);
	void setAge(int age);
	void setWeight(int weight);
	string getName();
	int getAge();
	int getWeight();
	void speak();
private:
	string name;
	int age;
	int weight;
};
//Implementation of Dog Class menthods
//Constructor
Dog::Dog() {
	age = 0;
	weight = 0;
	cout << "Dog Constructor Called" << endl;
}
//Destructor
Dog::~Dog()
{
	cout << "Dog Destructor Called" << endl;
}
//Setter methods
void Dog::setName(string name)
{
	this->name = name;
}
void Dog::setAge(int age)
{
	this->age = age;
}
void Dog::setWeight(int weight)
{
	this->weight = weight;
}
//Getter methods
string Dog::getName()
{
	return name;
}
int Dog::getAge()
{
	return age;
}
int Dog::getWeight()
{
	return weight;
}
void Dog::speak()
{
	cout << "BARK!!" << endl;
}
int main()
{
	Dog myPet = Dog(); //an object of Dog class is instantiated
	myPet.setName("Rusty");
	myPet.setAge(2);
	myPet.setWeight(14);

	cout << "My Pet Details are ... \n";
	cout << "Name: " << myPet.getName() << endl;
	cout << "Age: " << myPet.getAge() << endl;
	cout << "Weight: " << myPet.getWeight() << endl;
	cout << "My dog can ";
	myPet.speak();
	cout << endl;
	//declare another dog objet names yourPet and assign the following:
	Dog yourPet = Dog();
	yourPet.setName("Topsy");
	yourPet.setAge(3);
	yourPet.setWeight(16);

	cout << "Your Pet Details are ... \n";
	cout << "Name: " << yourPet.getName() << endl;
	cout << "Age: " << yourPet.getAge() << endl;
	cout << "Weight: " << yourPet.getWeight() << endl;
	cout << "My dog can ";
	yourPet.speak();
	cout << endl;

	
	system("PAUSE");
	return 0;
}


the user is prompted to enter name, age and weight of yourPet and myPet from the keyboard. Assign the values to yourPet and myPet object and print the details
Last edited on
Do you know how to use cin? See:

https://www.tutorialspoint.com/Standard-Input-Stream-cin-in-Cplusplus

So you need some local virables and cin to set the variables like you already did. Just replace the fixed values with the variables.
yes i know the cin but what i dont know is what is the variable that should set to the cin and is theres anything in the code that should change in order for the cin code to run
@Jabeh, did you write that code? If you did, then it's hard to see how you don't know how to declare some local variables and use cin.
@lastchance no i did not write the code, it is an exercise
@jabeh, you have not written any code of your own in any of your threads.

Start small: ask the user to input two integers, add them together, write out their sum.

Don't bother with classes until you have done a lot more than that.

You are not going to progress far without learning to declare variables, operate on them and input/output values.
@lastchance okay thank you
Topic archived. No new replies allowed.