GVSU CIS 263
Week 1 / Day 1
Administrative Stuff
- Course web page: https://cis.gvsu.edu/~kurmasz/Teaching/Courses/F19/CIS263/syllabus.html
- Read the syllabus.
- Office Hours:
- Official office hours are given on my web page. This is when I make a point of being in my office.
- However, I tend to be on campus from 9 to 4 Monday, Wednesday, and Friday.
- Anytime the door is open, it’s office hours. The wider the door is open, the more interruptible I am.
- If it’s all the way open, feel free to come in just to chat.
- I like mouthy students. (More specifically, I like to know what you do and don’t like about the way the course is being run. We may not agree; but, that’s OK.)
- The primary text will be the “Data Structures Essentials” online book from zyBooks https://www.zybooks.com/catalog/data-structures-essentials/
- A Tour of C++ by Bjarne Stroustrup is a good C++ reference book that is free to GVSU students: https://learning.oreilly.com/library/view/a-tour-of/9780134998053/
- I will draw some lecture material out of the Weiss text. It’s expensive, so I’m not suggesting you run out and buy it.
- All submitted code must come with tests.
- You are generally expected to fix and re-submit buggy code. Your grade will be based primarily on timliness (which a penalty for each buggy submission).
Getting started with C++
- Why C++?
- Closer to the hardware
- Languages like Java abstract away a lot of implementation details.
- This is good when doing “ordinary” application development; but,
- it is important for Computer Science majors to know what’s going on “under the hood”.
- You won’t need to look “under the hood” often; but, if you don’t learn it in college, you won’t have the skill when you need it.
- Also an opportunity to get experience with more languages.
- To be marketable after graduation, you need to be able to learn new languages / platforms quickly.
- Skill Level
- You need only learn C++ well enough to complete the assignments. (This isn’t a C++ class, after all.)
- However, the Weiss text goes into a level of detail that will be useful if you anticipate doing some “serious” C++
development after graduation.
- C++ is a compiled language
- To compile (in EOS):
g++ -std=c++17 -Wall myCppFile.cpp -o myExcecutable
- Emphasize need for
-std=c++17
- (Without it some newer language constructs – like initializer lists – won’t compile.)
- Emphasize importance of
-Wall
- To run (in EOS):
./myExecutable
- If you don’t specify the output with
-o
, a file named a.out
is generated.
- You may have at most one
main
function in the referenced code.
- C++ looks a lot like Java with several key differences:
- Namespaces are always explicit. (Not implied by directory structure.) See Stroustrup Section 3.4.
std::cout
(See Strostroup Chapter 10.)
- Separate
.h
and .cpp
files. (See Stroustrup Sections 3.1 and 3.2.)
- See
LawnMower.{h,cpp}
and mowerUser{A,B}.cpp
- Sample code available here: https://github.com/kurmasz-assignments/cis263-sample-code
Week 1 / Day 2
More C++
- CLion
- From JetBrains https://www.jetbrains.com– same company as IntelliJ
- Students have free access
- To get started
- Clone the repo with the starter code.
- File -> New Project.
- Edit path to point to folder for cloned repo.
- Edit
CMakeLists.txt
- Objects are, by default, created on the stack. (No need to use
new
as in Java.)
- Draw stack diagram. (Show both local varaibles and function calls in both C++ and Java.)
- Variables are not initialized. If you declare a variable and don’t give it a value, the value will get a
“random” value.
- Show how this can happen with a stack diagram.
- By default, objects are passed by value (whereas Java objects are passed by reference). See Stroustrup Section 3.6.
- By adding
&
to a type, you can pass an object by reference like Java does.
- See
Point2D.h
and passByValueOrReference.cpp
- Draw stack and memory diagram
- Pointers
- The
&
operator returns an object’s memory address.
- (The operator is also used to declare a reference type. With practice, you can tell the meaning from context.
When used with a type,
&
means reference. When used with a value, it means address.)
- The
*
operator is used to either declare or dereference a pointer. (When a pointer is dereferenced we take access the object being pointed at.)
- See
slidePointUsingPointers
in passByValueOrReference.cpp
.
- Draw stack and memory diagram
- Also used when creating objects dynamically using new (similar to Java). See
MyIntVector.h
- In general, you should use references when you can. Only use poineters when you must.
- Copy / assignment of complex objects
- By default, objects are copied field by field.
- This works for objects containing only simple types like
Point2D
.
- It can cause problems for objects that contain pointers and other shared resources.
missingCopyConstructor.cpp
shows what goes wrong when the default C++ copy constructor is used when a custom one is needed.
- Draw memory diagram
expectedCopyBehavior
demonstrates the expected behavior when a correct copy constructor is provided.
- The assignment (i.e.,
=
operator) must be overloaded for the same reason.
- Warning: Sometimes the compiler will use the copy constructor when you think it might be using the assignment operator – especially when using an assignment in a declaration like
MyVector v1 = v2
.
- See Stroustrup Section 5.2 for a general discussion of copy and move.
- Weiss Section 1.5.6 goes into even more depth.
- There is also a move operator for the special circumstance where an object is copied then destroyed.
- Most common when returning an object from a method.
- See Stoustrup Section 5.2.2 for details on the move operator.
- Weiss Section 1.5.6 goes into even more depth about rvalue references.