Lecture 3: C++ Primer
Tuesday 02/06, 2007
[Up]
[Previous Lecture]
[Next Lecture]
Assigned Reading: 11
Slides As Shown:
Topics Covered:
- Announcements:
- TA Office Hours Posted
- CMSC 202 Mailing List
- Computer Science Help Center Open
- Project 1 will be posted by noon next Monday
- You're reading the textbook, right?
- Recapped some differences between C and C++ from last time.
- Discussed C++ References.
(Slides posted last week.)
- Note: Don't try to make sense of the & notation.
- Simple test of references: ref1.cpp, ref1.txt
- References used for parameter passing:Parameter Passing by Reference.
Note no dereferencing in swap() function. Also, no address-of in function call.
Important: C programmer would not think calling swap() would change the values of a and b.
- Const reference parameters cannot be modified by the function called
but also are not copied.
- Discussed function overloading. See slides.
Some example programs.
-
Simple resolution with type:
over1.cpp,
over1.txt
-
Simple resolution with number:
over2.cpp,
over2.txt
-
Example of promotion and downcasting:
over3.cpp,
over3.txt
-
Ambiguous call:
over4.cpp,
over4.txt
-
Tricky resolution (don't do this!):
over5.cpp,
over5.txt
-
Do it this way, using typecasting:
over6.cpp,
over6.txt
-
Promoting int to double&:
const1.cpp,
const1.txt
-
Const& and ref:
const2.cpp,
const2.txt
-
Can't resolve pass by value vs pass by const ref:
const3.cpp,
const3.txt
- Discussed scope, lifetime and namespaces. See slides.