-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassInstance.cpp
More file actions
54 lines (44 loc) · 1.23 KB
/
classInstance.cpp
File metadata and controls
54 lines (44 loc) · 1.23 KB
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
#include <iostream>
#include <string>
class Dog {
//private data members by default
int age;
int weight;
std::string color;
//accessor and mutator functions
public:
int getAge() {
return age;
}
void setAge(int years) {
age = years;
}
int getWeight() {
return weight;
}
void setWeight(int lbs) {
weight = lbs;
}
std::string getColor() {
return color;
}
void setColor(std::string hue) {
color = hue;
}
//method of class Dog
void bark() {
std::cout << "Woof!" << std::endl;
}
};
int main() {
Dog fido; //create object of Dog class(instance)
fido.setAge(3); //set age to 3
fido.setWeight(20); //set weight to 20
fido.setColor("brown"); //set color to brown
//access data members of object using accessor functions
std::cout << "Fido is a " << fido.getColor() << " dog" << std::endl;
std::cout << "Fido is " << fido.getAge() << " years old" << std::endl;
std::cout << "Fido weighs " << fido.getWeight() << " pounds" << std::endl;
fido.bark(); //call bark method
return 0;
}