-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassMultipleObjects.cpp
More file actions
67 lines (55 loc) · 1.86 KB
/
classMultipleObjects.cpp
File metadata and controls
67 lines (55 loc) · 1.86 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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
class Dog {
//private data members by default
int age;
int weight;
std::string color;
//access modifier(public, private, protected)
public:
//accessor functions
int getAge() {
return age;
}
int getWeight() {
return weight;
}
std::string getColor() {
return color;
}
//setter prototype
void setValues(int, int, std::string);
//method of class Dog
void bark() {
std::cout << "Woof!" << std::endl;
}
};
//setter definition for prototype (convienent to combine all accessors in one function)
//this-> refers to the class member variable
//this-> is not needed if the argument and class member variables are different
//:: refers to the scope operator for the class where it is defined
void Dog::setValues(int age, int weight, std::string color) {
this -> age = age;
this -> weight = weight;
this -> color = color;
}
int main() {
//object of Dog class(instance)
Dog fido;
fido.setValues(3, 15, "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;
//object of Dog class(instance)
Dog pooch;
pooch.setValues(1, 10, "white");
//access data members of object using accessor functions
std::cout << "Pooch is a " << pooch.getColor() << " dog" << std::endl;
std::cout << "Pooch is " << pooch.getAge() << " years old" << std::endl;
std::cout << "Pooch weighs " << pooch.getWeight() << " pounds" << std::endl;
//call bark method
fido.bark();
pooch.bark();
return 0;
}