They are also a handy way of expanding the language. The <string> library declares a class of string objects that you have been using for most of the quarter. The <vector> library declares a type of object that has many similar objects hidden inside it. The <iostream> library defines several very useful objects
Notice -- you can use objects in a well written class without knowing what the detailed code for the class is. Classes are highly reusable.
Objects encapsulate little pieces of intelligence. Little parts of the solution to the problem.
The Clock Class
I have taken all the definitions for the Clock class
and the test example on page 217, added a few features,
and put the result in
[ clock.cpp ]
and tested them.
Demo -- sketching the UML for clock.cpp
7.1 Class Definitions
You can have as many public and private sections as you like.
However most classes have one public and one private section.
Further all the data members (variables) are private and most
(if not all) of the member functions are public.
You should learn to distinguish two kinds of functions
Each public/private section can declare as many variables and functions as you like.
You can also include the bodies of functions in the class Do this when the body is small -- one line long. Lots of functions in classes are this small. Functions that are defined inside a class are said to be inline and their calls are executed very much faster than functions that are not inline.
Notice the semicolon at the end of a class definition. It is vital.
Don't ask why!
Page 220 -- using the double colon operator
className::functionName(....){.....}
They need special Makefiles if you do it this way.
In CSCI201 I'll be happy if your projects are not multifile projects. You can defeclare and define a class in the same file that uses it if you wixh. You may wish to place a class in a separate "....cpp" file and use #include to use it in a program:
#include "ClassName.cpp"For large projects this strategy is not efficient. It is easier when learning C++ to do it straightforwardly. In CS202 you will have to do the more complicated technique.
Instead you write functions with the same name as the class. And the
actions you put in the body of a constructor are done when you declare
a new object.
7.3.1 Declarations of constructors
className ( parameters );notice the semicolon above.
ClassName (parameters) { shortBody }
Notice the abscence of a semicolon above!
A long constructor is best defined later:
ClassName::ClassName (parameters) { LotsOfStuff }
Notice the abscence of a semicolon above!
Note: we have a short hand for initializing variables
className( parameters ) : variable(value), ...
Also take care to distinguish (...) from {....}.
7.3.3 Calling Constructors
When you declare variable a constructor is called.
Once a variable is constructed you must not reconstruct it.
You can create objects on the fly by using a constructure in an expression!
You can create whole arrays of objects at one time!
7.3.4 -- 7.4 SKIP
7.5 NEXT Time.
. . . . . . . . . ( end of section Reading) <<Contents | End>>
Example of a Simple Class in C++
Handout:
[ 16ex.cpp ]
plus a UML diagram (the kind I will ask for in the final project).
Exercise 1: Fill in the blanks...
Exercise 2: Predict what it does...
class Name
{
public:
Functions that can be used outside the class
private:
hidden variables and operations
};Here is a template [ class.cpp ] that you can download and copy/paste or read into a C++ program. It includes some handy features -- all you have to do is , copy/and paste what you need, edit the Names, delete what you don't need, and make the functions do what you want.
ClassName variableName ( parameters );
Declaring a pointer to an existing object
ClassName * variableName = &existingObjectName;
Declaring a pointer to a new object
ClassName * variableName = new ClassName( parameters );
variable.functionName( parameters )(May be a command or in an expression).
point->functionName( parameters )(May be a command or in an expression).
You can chain a series of operations
variable.function1(parameters).function2(parameters). ...
Why do we need classes
To control the complexity of a big program. It is the old divide and conquer
technique. Divide up a complex programs into many simple classes.
Connect them by passing messages. As a result the program will be be more
maintainable -- and have fewer bugs.
What is a class
A class is a template ofr a set of similar objects. They all have similar
data and will accept the same operations.
Should my classnames all start with capital letters
Yes.
Does every body start class names with a capital letter
Every body except the people who wrote the standard does it right.
The standard has classnames like: string, vector, etc..
How does a class work
A class is a template describing an object. It includes a
description of the data/attributes of the objects and the
operations/functions that can be applied to these objects.
A class's name is used to construct new objects that belong to the class. Then it is up to the objects.
Why set classes to private at the start
Just to remind ourselves of the rule that things a private at
the start of a class unless until we write "public:".
Be happy -- in Java we have to write private or public before every declaration!
How does an object work
Objects store there attributes/data in primary memory. You use
an object in a program be sending it a message:
object_name . message (data)This works just like a function call.... but while the function executes the function has access to the attributes inside the object.
Normally attributes a private and can not be accessed outside the
object/class.
Where would you find a library of available classes
Start with the standard library. I have a summary
[ ../samples/stl.html ]
of the most useful -- mainly containers like vectors and deques.
The Wikipedia entry
[ C++_standard_library ]
looks a useful place to learn.
I also have
a copy of the draft standard
[ ../c++std/cd2/index.html ]
(not easy to read but comprehensive). You can find other libraries on the
web such as
[ http://www.boost.org/ ]
as an example.
Can we make our own libraries
Yes. Put them in a file with the right incantation and then
#include it -- put the file name in double quotation marks.
The incantations:
#ifndef FILENAME
#define FILENAME(at the top of the file)
#endif(at the end of the file).
So inline functions are fast but take more space. Use them for small function bodies.
Why would destructors be used in real-life program situations
They are used for tidying up and recycling storage allocated by 'new'
on the heap. Only used on large programs. Not needed in CSci201.
What does the double colon operator do in a program
It is used to refer to functions and data inside a function.
ClassName::functionName (....)refers to the function called functionName inside the class called className.
Can we program in Dia like we can in C++\
We cdon't have a compiler for UML. Dia doesn't even have a tool
to output C++ code. Perhaps you should write it?
What does cerr do
cerr does every thing that cout does. But you can tak output sent to
cout and redirect it into a file:
command > fileBut 'cerr' output doesn't go in the file, it is shown to the user.
Use 'cerr' to output error messages and debugging print out.
Where do I declare a variable so that it belongs to a function
Put the declaration
after the opening '{' of the function and before the closing '}'.
The Lab will ask you to draw UML diagrams of the classes in Chapter 7.
The project [ projects.html#P7 ] should fix (if needed) your Project 6 (create and use a function).