CS 225 Notes Chapter 1
Here is the C code to copy one string like this to another
Intro to me
Got my BS and MS from UW-Madison. Ph.D from IIT in 1993. Been teaching
at NCC since 1984, Depaul since 1999. Work at Lucent Tech. It is ok to
call me at work. If busy, I will call back. Email is good. Call me at home
before 10:00. At work or home after 0900. We screen calls so wait for the
tone. Call me anything you want. I like interruptions. All questions are
Several coding assignments. Write the code as if you would use it again.
I don't care what compiler or machine. Must be C++ though. Some aspects
of C++ syntax are confusing so don't spend too much time trying to figure
it out, ASK. I am assuming considerable C knowledge.
The point values are in the syllabus. I
am lenient about interpretation of things. If you are not sure what I mean,
ask. Or at least document it. My tests are not very hard. CS is not about
memorization of algorithms or syntax, but problem solving. Programs are
the best exercise and test.
I will be here after class as needed most days. Other times are available
The university has a trict policy about plagiarism. It is helpful to know
the difference between the real world and academia on this issue. In the
real world, people are encouraged to use other peoples code as this reduces
cost. It is called re-use. However, in the academic world, unique work
is the key. Another way to look at it is copying someone elses work in
this world is like getting your sister to do your piano lessons. It's
fine until you have to do the recital.
The main thrust of the class is programming in C++. We will try to
cover the entire language. Even without the OO extensions, C++ is an improvement
due to the better type checking. C++ has grown a bit too much, in an attempt
to solve all problems.
Object Oriented Programming
This is primarily the grouping of code and data together. Encapsulation
anl inheritance are the main features. This differs from structured programming
mostly in syntax and the inheritance idea.
The other major OO language these days is Java. Time permitting, I will
introduce examples in Java to show how they are related. But this isn't
a Java class.
Overview of OOP
What problem were we trying to solve? Mostly complexity and re-use.
People noticed that code that was grouped together with the data it manipulated
was easier to follow. Also, complex data problems could be simplified by
making a graph of them.
Code is hard to re-use since everyone codes the interfaces slightly
differently. At least differently than you want them to be. C++ classes
allow a developer to present an interface to the user so they can access
the data without messing with the internals.
Control of complexity
Systems with a large number of related but different data objects were
hard to understand. Inheritance and access control makes that easier. Magic
number 7 + or - 2.
People can only hold about 7 thngs in thier head at the same time.
So if you can lump things together, you can hold more of the program in
Similar problem. Break code into smaller modules to ease understanding.
Definitions and principles
Data and code together
Object data can only be used by object code
Attached code called methods
Also known as member functions
Like C structures with code fields
Multiple functions with the same name. Allows more natural function
naming. Most data objects have a print operation. Now all can be called
print(). Also can overload operators like plus(+) so that string concatenation
can look like arithmetic.
There is often a natural hierarchy to data objects. All rooms have
some things in common like walls and doors. But some rooms have windows.
Some rooms have plumbing. Some walls go to the ceiling and some don't.
Rather than describe everything for each kind of room, create a generic
room object and have the special rooms add to that, subtract from that
or overload that.
Overview of C++
Came from desire to have OO language that was familiar to C programmers.
There are gillions of C programmers at Lucent (Was AT&T) But very few
Lisp or Smalltalk or ADA people. Also they wanted to added better type
checking to C.
Differences from C
The base language in C++ uses ANSI standard C. It adds new and delete,
the const definition, asserts, more ways to initialize things. Some are
important like new and delete. Other aren't so important like not needing
the keyword struct in some places.
Identifiers are the same as most languages. Letters, numbers and underscores,
starting with a letter.
These include integers (int), float and character (char). They can be modified
by short and long, signed and unsigned for ints and double for floats.
A pointer is the address of an object. Like the address on a letter, it
is not the address we are interested in, it is the house it represents.
Every object in the program has a memory address. A pointer variable contains
one of these memory addresses. For example, if you declare an integer
it has an address, say 99. Now let's make a pointer variable.
This variable is interesting not for its contents directly, but because
its contents are the address of something else.
This sets the variable p to contain the address (&) of the variable
a; So now the contents of p is 99.
Now we can use the pointer variable to print the value of a. getting
the value of the thing from the pointer is called dereferencing.
To use this in C++,
print(*p); // this is not a real print statement
We can also change the value
The * is very important. Without it, this changes the pointer to contain
the address 6, which is probably not what we want. This kind of mistake
is responsible for many program crashes. It is possible to do arithmetic
on pointers. That is, you can add an integer to a pointer. So p+1 is the
contents of the memory address that comes after p. This is not very useful
here, but it very useful when we talk about arrays.
Arrays are a collection of objects that are all the same size and are right
next to each other in memory. They are defined in C++ like this
This makes an array of 5 integers that are numbered from 0 to 4. You can
make a pointer to an array.
This creates a pointer an initializes it to point to a. To point to a means
to have the memory address of the first element in a. So, the statement
p=a means the same as p=&a. These are the same since in C++, the
name of an array, in this case a, is a pointer to the array. So we can
set all the elements in an array to 0 like this
for(int i=0; i<5;i++)
++ is the C++ increment operator. p++ is equivalent to p=p+1; But we can
combine the statements above to be
This is interpreted as
set the value pointed to by p to 0, like *p=0
then increment p, like p=p+1
The prcedence rules in C++ make this happen.
Array pointers are really interesting when we use arrays of characters.
There is a convention in C++ that strings are represented as arrays of
characters with a NUL character at the end. This character is represented
in ASCII as a 0. So,
We could store the string "zaphod" as
There are two versions of the operators. a++ increments the value
of a after using it. so b=a++ means to copy the value in a to b and then
increment a. The version ++a means increment first. There is also a decrement
operator so you can do a--.