Project 2 is due in.
You can resubmit Project 1 if the previous grade was B or less.
Topics
Assignment
Friend functions
Comparison operators and <utility>
Unary operators
Simple
Prefix
Postfix
Indexing[]
Function call()
Demo a class with const, friend, operators
Complete a given simple efficient 8 character buffer class for secure input/output.
Previously you met a very insecure 8 character buffer in [ lab01.html ] because it allowed stupid programmers to do stupid things ... like over-running the end of the buffer.
One way to fix this is to hide the dangerous data in a class and provide just those functions that do what is needed and can't be abused.
UML:
Here is the test program: [ testBuffy.cpp ]
Here is the "Header file":
[ Buffy.h ]
Demo Splitting a header file from a body file and using make
The above was done is the simplest way -- very amateurish. Now to redo it using professional techniques: we separate the detailed code of the functions from the header file and precompile them. Then we use a program called make to assemble the program for use.
[ testBuff2.cpp ]
[ Buff2.h ]
[ Buff2.cpp ]
[ Makefile ]
[ Make in resources ]
Glossary
We will do a little dynamic_cases later.
How does & work in function headers
It is placed before the name of a parameter to set up
call by reference.
Without it the actual parameter is copied into the function
as the initial value of the parameter -- and never comes out again!
Call by reference means that the address of the variable in the call is passed to the function and all actions done to the parameter actually are done to the object. This is mainly used to allow data to flow in and out of the function through a parameter.
Notice it is faster to use call be reference (&) with objects -- the object will have more bytes in it than an address. Copying bytes takes time.
Also -- we used to get the same effect by using '*' and '&' but this
needs a great deal of knowledge and care to work without bugs.
When to use friends
Any class can declare friend classes and friend functions.
Use them as little as you can. They expose the object to abuse by the friends.
The input and output functions (operator<< and operator>>) are the main
functions that you MUST define as friends in a class in the course.
Can you point to a data member in an object
If the data is public you can. But it is wiser not too.
What is * this
When you apply a function to an object:
object.example(data)then, invisibly, the program does this
this = & object;before the function starts to execute. Thus the function knows where the object is.... and can use the information in several ways.
A common way is to tell another object about 'this' object:
user_interface.addButtonListener(this);which means that when the user clicks a button 'this' will be called with a description of the event.
There are other uses for this to be covered later. Most of these use
*thisto refer to the current object -- say to send a message to itself!
Static data belongs to its class, not to the individual objects. It is shared by all the objects. Its value doesn't change when you change objects.
Use it when the data is about the whole class not about one instance/object of the class.
The Wikipedia [ Static_variable#For_class_variables ] has a couple of examples -- one in C# and a simpler one in C++ They define a class of Requests. Each Request is for a particular URL (place on the web), but the class, itself maintains a count of created Requests. This would be used to keep track of how many outstanding requests for web pages have been made, and whether there is a problem or not.
Other examples is a class recording the enrollment in a section of a course. The size of the enrollment is a shard "static" number, each enrollment is for a specific student who is enrolled in a particular section.
In fact, most academic examples of static class members are of counters!
What is a binary operator
A binary operator is something like "+" and "/" which is written between two
operands:
1 + 2
int(rand()*26)+'a'
Seriously: const protects data for abuse -- use it to control bugs.
Lab
Same as Monday:
[ lab03.html ]
on Dia and the UML.
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 06, More about C++ classes) <<Contents | End>>
(Next): Deriving classes and Inheritance
[ 07.html ]
Abreviations