If we know how to use an object, we don't need to know how it does it. It is like a watch. We know how to read and set the time. We don't need to know how it works. We don't have to think about cogs and spring (or chips and batteries) to tell the time.
The properties of an object (what data it will hold for instance) is defined by its class. So my wrist watch is in class "Watch". There can be any number of objects for any given class.
As a rule in C++ you can say that if something is not a number then it is an object.
Strings -- an example standard class of objects
The book uses string in this chapter (Section 3.5 below).
It is a very useful standard class.
I will show you some uses in the laboratories.
Each string contains a sequence of
zero or more
characters. Examples: words, names, dates, sentences, formulas....
They are numbered 0,1,2,3,4,....
If you want to use strings in your program you need these two lines:
#include <string>
using std::string;
You can then do the following things with a string objects
called s, t, etc:
Table
| Example | Effect |
|---|---|
| Commands | |
| string s = "Hello"; | Declare and initialize s |
| cin >> s; | Read the next word from input into s |
| getline(cin,s); | read the whole next line into s |
| s="World of Pain"; | copy in a new value replacing the old s. |
| s=t; | copy t into s replacing the old s. |
| Expressions | |
| string("example") | Construct a new string containing the characters. |
| s + t | Concatenate s with string t |
| s.length() | find out how many characters in s |
| s.substr(f, l) | part of s starting at f and of length l. |
| s.find(t) | Place in s with the first t occurs. |
| s == t | True is s and t have the same characters in the same order. |
| ... | and many more TBA |
Can strings be used for arithmetic
Not easily. You have to use some advance techniques to convert the
characters into numbers beforehand.
How many strings can you have
As many as you want.
3.2 Classes, Objects, Member Functions and Data Members
Short definitions:
Classes are static -- handled by the compiler. They
can't do much until the program runs and they constructs objects.
3.3 Overview of the Chapter Examples
Note. The book grows the GradeBook class from a useless beginning.... but by the
end of this course a GradeBook object is starting to look very useful.
Also notice the style used for naming classes, functions, objects etc. The book does it right.
Can we use a different style of name
Not in this course! For example: please don't abbreviate names of
functions or classes without a strong reason for doing so.
When you move on to other things -- find out the style of the people you work with and fit in.
What is a grade book
It is a book that records all the grades in a course.
3.4 Defining a Class with a Member Function
Take note of the syntax for declaring classes:
class Name
{
...
};Do not forget the semicolon above!!!
A class says what is possible. Calls to its functions make possible things happen... to that particular object.
Why is data private and functions public
Becuase a class has to have public functions for it to be used, and
the hidden data is then protected from abuse.
Why must we create objects before we can call member functions
Becuase member functions operate on the data inside an object. ANd if there
is no object, then there is no data to work with.
Drawing UML Diagrams
We have a tool called Dia in our lab machines that is quite good at
doing diagrams, including the UML. It is free software and works on
MS Windows as well. In the lab, in a terminal window, just type:
dia &(don't forget the "&"!) and a working window and a tool box will pop up. Have fun....
However -- you can draw very useful diagrams by hand. I will give examples in class of the kind of rough diagrams I use. I'll expect you to draw UML diagram in quizzes and exams.
Do you have a handout on the UML
[ uml.html ]
Will we use the UML
Yes. In labs, quizzes, and the final.
They may also help you in your project.
3.5 Defining a Member Function with a Parameter
Notice the slightly different notation for declaring a member function
void name (parameters)
{
...
}Exercise: List all the differences between the syntax for declaring a function and the syntax for declaring a class above.
Notice that you always need to test a class by using a main function.
Hint: it is not necessary to have a "set" and "get" for every attribute (data member). True objects don't expose their secrets easily.
How does a set function differ from a get function
A set function or
setter
Note: older functions tend to not use the words "set" and "get" so you have to look at the definitions...
. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver -- a question
Exercises
Next
Bring a calculator!
[ 06.html ]
[ lab03/ ]
Abbreviations