Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp_introduction
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cp1_ws19
cpp_introduction
Commits
515e228a
Commit
515e228a
authored
Oct 28, 2019
by
Kevin Höllring
Browse files
Options
Downloads
Patches
Plain Diff
Add explanatory comments to sample 3
parent
d3dd5829
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/sample_3.cpp
+161
-158
161 additions, 158 deletions
src/sample_3.cpp
with
161 additions
and
158 deletions
src/sample_3.cpp
+
161
−
158
View file @
515e228a
#include
<iostream>
#include
"sample_3.h"
#include
<iostream>
// This function is passed a copy of the original house (no &)
// Then the copy is painted black but the original still has its original color
void
city
::
copy_repaint
(
city
::
House
torepaint
)
{
torepaint
.
color
=
black
;
}
void
city
::
copy_repaint
(
city
::
House
torepaint
)
{
torepaint
.
color
=
black
;
}
// This function is passed a reference to the original house (&)
// Then this house is painted black
void
city
::
copy_free_repaint
(
city
::
House
&
torepaint
)
{
torepaint
.
color
=
black
;
}
...
...
@@ -13,8 +14,7 @@ void city::copy_free_repaint(city::House &torepaint) {
void
city
::
describe_house
(
city
::
House
&
house
)
{
std
::
cout
<<
"The resident "
<<
house
.
owner
;
std
::
cout
<<
" is currently living in their "
;
switch
(
house
.
color
)
{
switch
(
house
.
color
)
{
case
red
:
std
::
cout
<<
"red"
;
break
;
...
...
@@ -38,10 +38,10 @@ void city::describe_house(city::House &house) {
}
int
main
(
int
argc
,
char
**
argv
)
{
using
std
::
string
;
using
std
::
cout
;
using
std
::
cin
;
using
std
::
cout
;
using
std
::
endl
;
using
std
::
string
;
using
namespace
city
;
// Declare a variable to hold the name of the user
...
...
@@ -58,12 +58,14 @@ int main(int argc, char** argv) {
// Check if the name has at least 3 characters
if
(
owner
.
length
()
<
3
)
{
// Print out hint
cout
<<
"Your name is suspiciously short. Please enter at least 3 characters"
<<
endl
;
cout
<<
"Your name is suspiciously short. Please enter at least 3 "
"characters"
<<
endl
;
// Skip to next run of loop
continue
;
}
else
{
// Write out a mix of a static string and the string previously read
in
}
else
{
// Write out a mix of a static string and the string previously read
//
in
cout
<<
"So your name is "
<<
owner
<<
"."
<<
endl
;
// Let us break free of this infinite loop
...
...
@@ -71,7 +73,6 @@ int main(int argc, char** argv) {
}
}
// Let's ask the user for a color of the house
cout
<<
"Which color should your house have? (r|g|b|w)"
<<
endl
;
...
...
@@ -107,12 +108,14 @@ int main(int argc, char** argv) {
// Deal with an unknown color
if
(
unknown_color
)
{
cout
<<
"The color you chose was not known. Please choose one out of r,g,b,w"
<<
endl
;
cout
<<
"The color you chose was not known. Please choose one out "
"of r,g,b,w"
<<
endl
;
// Retry
continue
;
}
else
{
cout
<<
"The color you chose was "
<<
color
<<
". Your house will be painted in this color."
<<
endl
;
}
else
{
cout
<<
"The color you chose was "
<<
color
<<
". Your house will be painted in this color."
<<
endl
;
// End loop
break
;
}
...
...
@@ -129,18 +132,19 @@ int main(int argc, char** argv) {
cin
>>
house_number
;
if
(
house_number
<=
0
)
{
cout
<<
"Oh no! Your favorite house number should certainly be positive. Please consider this again."
<<
endl
;
cout
<<
"Oh no! Your favorite house number should certainly be "
"positive. Please consider this again."
<<
endl
;
// Retry
continue
;
}
else
{
cout
<<
"Oh, so your house will have the number "
<<
house_number
<<
" just for you."
<<
endl
;
}
else
{
cout
<<
"Oh, so your house will have the number "
<<
house_number
<<
" just for you."
<<
endl
;
// Stop the trying loop
break
;
}
}
// Declare a house of custom type and assign its member fields.
House
designed_house
;
designed_house
.
color
=
houseColor
;
...
...
@@ -150,17 +154,16 @@ int main(int argc, char** argv) {
std
::
cout
<<
"Let us summarize your registration:"
<<
endl
;
describe_house
(
designed_house
);
std
::
cout
<<
"Let us summarize your registration:"
<<
endl
;
describe_house
(
designed_house
);
std
::
cout
<<
"We will now try to repaint your house black:"
<<
endl
;
// Demonstrate that passing the copy of a house will not change the original
copy_repaint
(
designed_house
);
describe_house
(
designed_house
);
std
::
cout
<<
"Let us try it again a little differently:"
<<
endl
;
// Demonstrate that passing a reference to the original will cause its color
// to change
copy_free_repaint
(
designed_house
);
describe_house
(
designed_house
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment