-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassConstructorDestructor.cpp
More file actions
89 lines (68 loc) · 2.25 KB
/
classConstructorDestructor.cpp
File metadata and controls
89 lines (68 loc) · 2.25 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
class Dog {
//private data members by default
int age;
int weight;
std::string color;
//access modifier(public, private, protected)
public:
//constructor prototype
Dog(int, int, std::string);
//destructor prototype
~Dog();
//accessor/mutator functions
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;
}
};
//constructor 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
Dog::Dog(int age, int weight, std::string color) {
this -> age = age;
this -> weight = weight;
this -> color = color;
}
//destructor definition for prototype
Dog::~Dog() {
std::cout << "Dog is destroyed" << std::endl;
}
int main() {
//object of Dog class(passing values to constructor method)
Dog fido(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(passing values to constructor method)
Dog pooch(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;
}