forked from berea-college-csc236/dogclass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdogclass-main.cpp
More file actions
82 lines (73 loc) · 2.29 KB
/
dogclass-main.cpp
File metadata and controls
82 lines (73 loc) · 2.29 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
/**
Dog class example is written by Dr. Jan Pearce to show simple use of a C++ class
with
- constructors (aka initializers),
- accessors (aka getters),
- and mutator (aka setter) methods
*/
#include <iostream>
#include <string>
using namespace std;
class Dog // begin declaration of the Dog class
{
public: // begin public section
Dog() { // default constructor
dogName = "dog"; //set to generic dog name
}
Dog(string whatName) { // constructor
dogName = whatName; //set to dog name parameter
}
string getName() const { // accessor aka getter method
// returns value of private member variable
return dogName;
}
void setName(string name) { // mutator aka setter method
// set member variable dogName to
// value passed in by parameter name
dogName = name;
}
void bark() const {
cout << "Woof!\n";
}
void show() const {
/** shows dog pic and display's dogName
Art modified from https://www.asciiart.eu/animals/dogs
original art by by Joan Stark */
cout << " .-\"-." << endl;
cout << " /|6 6|\\" << endl;
cout << "{/(_0_)\\}" << endl;
cout << " _/ ^ \\_" << endl;
cout << "(/ /^\\ \\)-'" << endl;
cout << " \"\"' '\"\" " << endl;
cout << " " << dogName << "\n" << endl;
return; //optional return
}
friend ostream& operator << (ostream& stream, const Dog& dogg);
private: // begin private section
string dogName; // private member
}; //don't forget semi-colon with C++ classes!!
ostream &operator << (ostream &stream, const Dog &dogg) {
stream << " .-\"-.";
stream << " /|6 6|\\";
stream << "{/(_0_)\\}";
stream << " _/ ^ \\_";
stream << "(/ /^\\ \\)-";
stream << " \"\"' '\"\" ";
stream << dogg.dogName << "\n" << endl;
return stream;
}
int main() {
char stopme;
Dog averagedog; //uses default constructor
averagedog.show();
averagedog.setName("Doggie"); //use of setter setName()
cout << "\nI call my dog: " << averagedog.getName() << endl; //use of getter getName()
cout << "My dog likes to say: ";
averagedog.bark(); //use of method bark()
Dog lassiedog("Lassie"); //uses constructor
cout << endl;
lassiedog.show();
cout << averagedog; //use of overloaded << operator
cin >> stopme; //holds console open in some cases
return 0;
}