If you depend on computers and software,
ViaGraphics video training is the right choice for you.
When you use video training, you learn faster, retain more, and perform better at your computer.
And best of all, less time at your computer means more time for you and the things that are really important.
Why bother with expensive seminars and time-consuming classes?
You can get the best training available anywhere, at home, in the office, or anywhere you have a VCR.
If you're a beginner, our easy-to-follow tapes start with the fundamentals of your program,
and then teach you the most commonly used features.
You'll become more confident and productive after watching just one time.
And, if at any time you don't understand a section or want to review,
with ViaGraphics video training, it's as easy as rewinding the tape.
If you're an experienced user, you already know how valuable a tool your computer can be.
ViaGraphics training tapes are the perfect way to get the most from your software.
Our training videos show you how to fine-tune the options,
update technical information, and take the features you already use an added step further,
all in an easy-to-understand way.
Word processing, desktop publishing, business graphics, computer-aided design.
You can learn quickly and effectively.
If you use computers, decide today to make your life better.
The sooner you use video training, the sooner you can put your computer to work for you,
and the sooner you can be on your way to the flexibility and productivity
that will give you the time for the things you really enjoy.
Get started today.
...
Now more than ever, keeping up with the world means keeping up with computer technology and software.
ViaGraphics, the leader in video training services, is proud to present this video tutorial.
Now your instructors, Kurt Gortney and Kim Wilson.
Hello and welcome to this video tutorial, Programming in C++.
I'm Kurt Gortney along with Kim Wilson.
This video covers the concepts of C++.
Watching this tape will not make you a C++ programmer.
Writing programs in C++ is the only way to do that.
However, this course presents the fundamental concepts you have to understand
in order to write C++ programs.
In this video, we assume you have some knowledge of the C programming language.
If you are not familiar with the C language, you can still learn from this tape.
To learn more about C, refer to the ViaGraphics tutorial, Programming in C.
You don't have to memorize the syntax and format of every C++ statement as you work through the examples.
You can consult the manual later for that.
Instead, remember the functions of the various statements and the context in which they can be used.
Then, when you write your own programs, you will know which statements to use and how to use them.
You can use the manual anytime, if necessary, for the precise syntax of a command.
While watching, if you don't fully understand a concept, rewind the tape and watch the section again.
If you don't completely follow the way a program segment works,
pause the tape and examine the program listing for a few moments before retrying the section.
In this video, we are using Microsoft C++.
The material we cover applies to most versions of C++, including Borland's.
As you may know, C++ is an enhancement of the C programming language.
Even the name C++ means C incremented by one, implying that C++ is one better than C.
For the most part, C++ is a superset of the C language.
Most features are supported by C++.
In addition, C++ enhances many of the existing options of C and provides many new features.
One of the new features of C++ is the use of streams for input and output.
For example, this program, sample1.cpp, included on the VIA graphics learning disk,
asks for the input file name, assigning it to the string fname.
We'll run this program and then take a closer look at it.
And here we see the file name.
The two less than and two greater than operator signs are used to designate output and input
to the streams C out and C in.
C in and C out are not part of the C language proper, but are members of the stream library.
This is analogous to printf and scanf, which are not technically part of the C language,
but are found in the C function libraries.
The two less than signs and two greater than sign operators are the same operators used
for bitwise shift operations.
The context of the operators or the type of operands they are used with determines the
function of the operators.
When the two less than sign is used with the stream operand, it causes an output operation
to be performed.
Formats can be used with C out.
For example, this program sample2.cpp on a learning disk outputs the number 65 in decimal
and hexadecimal formats.
It reads and include iostream.h.
Then the main part of the program void main, parenthesis open, parenthesis closed, left
brace, plus int x equals 65 semicolon.
Then we have C out, two less than signs, decimal, two less than signs, x semicolon.
This is equivalent to C out, two less than signs, x semicolon.
C out, two less than signs, hex, two less than signs, x semicolon, and then the right
closing brace.
Here's our program.
Notice the comment line that begins with the two slashes.
With C, comments are enclosed in slash star, star slash pairs.
In C++, you can use the C form of comments or you can use two slashes to designate a
comment from that point to the end of the current line.
Functions are an integral part of C++ and also C. However, C++ has somewhat stricter
rules and function declarations.
For example, in C, it is possible to declare a function in a program before it is used.
This declaration is called a function prototype.
In C++, you are required to use a function prototype to declare a function before it
is used.
With C, you can declare function parameter types either inside or outside the parameter
list.
In C++, however, function parameter types must be declared in the parameter list.
We're showing the file sample3.cpp from the learning disk.
This program can be compiled under the C language.
In C++, we must add a function prototype and declare the parameters x and y to be the type
float in the parameter list.
We'll place the function prototype here just below the pound sign include io stream statement.
We declare the parameter types here in the program.
We'll enter float height parenthesis float x comma float y parenthesis semicolon.
This is the function prototype that we must use.
We'll enter float height parenthesis float x comma float y parenthesis left brace.
We also could use the printf statement instead of the cout form.
C++ supports both cout and the printf statements.
A C++ function prototype can list default values for its parameters.
Then, if you call the function using arguments, those values will be used in the function.
If you omit the arguments in a function call, the default values will be used as the arguments.
This prototype provides default values for the three parameters int x, int y, int z.
We have void printval parenthesis int x equal 10, int y equal 20, int z equal 30.
This function can be called in the following ways.
Printval 40, 50, 60 will print 40, 50, and 60.
Printval with an open and closed parentheses will print 10, 20, and 30.
Printval 40 will print 40, 20, and 30.
Printval 40 and 50 will print 40, 50, and 30.
Notice that whenever we enter an argument, that argument is printed in place of the default
value.
Whenever we do not enter a number in the argument, then that default value is printed.
This function cannot be called in this way.
Printval comma 50.
If an argument is omitted, then all arguments to its right must also be omitted.
In C, variables must be declared at the beginning of a block.
In C plus plus, variables can be declared anywhere as long as they are declared before
they are used.
This makes it possible to use declarations like this.
For int j equal 1, j less than or equal to 50, j plus plus.
You should be careful when you declare variables in places other than the beginning of a block.
If you have variable declarations scattered throughout your program, their program will
become difficult to read and difficult to maintain.
In C plus plus, there is a scope resolution operator, the double colon sign.
We can use this to reference a global variable when there is a local variable by the same
name.
For example, the following program, sample5.cpp on your learning disk, outputs 10 and 20.
The double colon x refers to the global variable x, which is the one at the beginning of the
program.
Here's an important point you should keep in mind when programming in C plus plus.
The scope resolution operator makes it possible to reference global variables when there are
local variables by the same name.
However, using two different variables with the same name can be a dangerous programming
practice.
If two variables have different functions, they should have different names.
C plus plus provides the inline function qualifier to produce inline functions in the compiled
code.
This means that each function call causes a copy of the function to be inserted in the
object code of the program.
If you call an inline function from 20 places in your program, 20 copies of the function
will be inserted into your exe file.
An inline function can mean faster execution time with a trade-off for space.
The inline qualifier is typically used with small functions that are not called from many
places and also with time-critical functions.
Inline functions are similar to macros defined with the pound sign define directive.
However, inline functions are usually preferable to macros because they provide parameter checking
and they do not have the side effects that sometimes occur in macros due to the textual
substitution.
Even if you want a function to accept more than one parameter type, you can still use
an inline function rather than a macro.
Overloaded functions, which we'll discuss in a moment, provide this capability.
The program sample6.cpp on the learning disk demonstrates a macro versus an inline function.
Notice that the macro function in this part of the program actually outputs the wrong
answer.
This macro section outputs 21 and 32, when in reality the answer should be 21 and 31.
We can see that the macro section doesn't work as good as the inline section.
The inline section outputs the correct answers, 21 and 31.
The constant qualifier is used to declare variables to be constant values, so they cannot
be changed.
C also supports the const qualifier, but with C++, constants are more flexible.
In C++, you can use a constant variable anywhere you can use a constant expression.
For example, you can use a constant variable to declare an array size.
You can also declare a function parameter to be a constant to prevent it from being
modified within the function.
For example, float type, parenthesis, constant float x, constant float y.
The constant qualifier is very, very important for C++ class declarations as we'll see later.
The function overloading is an important feature of C++ that allows you to use functions that
operate a variable number and type of parameters.
Overloaded functions are actually different functions with the same name that differ in
the number and type of their parameters.
For example, we can write a function to calculate the distance between two points using integer
coordinates, a function to calculate the distance using float coordinates, and a function to
calculate the distance using double-taught coordinates.
In C, this would require three functions with three different names, but using C++, we can
write all three functions using the same name.
The compiler can then tell the difference between the functions by comparing the number
and type of arguments.
For example, let's write a pair of functions called search that search for the first occurrence
in a string of a string or a character.
In C, we would use the function string str to search one string for another and string
chr for string character to search a string for a character.
In C++, we'll overload the function search to perform these searches.
This section of program is on the learning disk as sample 7.cpp.
It begins with character star or asterisk search, parenthesis, character star target
comma, character star ID, close parenthesis, semicolon.
This is for the string search.
Character star search, parenthesis, character star target comma, character ID without an
asterisk or star, searches for the character string.
The next line is void main with empty parentheses.
Next is the character name with empty brackets equaling quotation mark ABCDEFGHIKL, ending
quotation mark, semicolon.
Next is character A and 21 in square brackets, semicolon.
Then character C, semicolon.
The next line reads character asterisk or star PTR1 comma star PTR2, semicolon.
Next we have string copy STRCPY and then name comma and ABCDEFGHIKL in quotation marks.
This part of the statement is in parentheses.
The next line is also a string copy line, A comma GHI.
Then we have a PTR1 statement equaling search name comma A.
The next line reads C equal G. PTR2 equals search name comma C is our next line.
We have now made pointer 1 equal to pointer 2.
The next part of our program segment reads character star search, parenthesis, character
star target comma, character star ID.
Left brace.
Now we'll enter return string string, parenthesis, target, comma ID, in parenthesis, semicolon.
This statement searches for the string GHI.
A closing brace, character star search, parenthesis, character star target, comma, character ID
is our next to last line in this segment with the final line being return string character
target ID.
A reference is a new type of variable supported by C++.
A reference is actually an alternate name for a variable.
For example, this declaration defines two names for the same variable, normal and alias.
Normal int and sign alias equal normal.
The and operator identifies the name alias to be a reference.
The first statement declares normal to be an ordinary integer variable.
The second statement declares alias to be a reference variable referring to the variable
normal.
In the program, the names normal and alias can be used interchangeably.
They refer to the same address in memory.
A reference is not a copy of the variable to which it refers.
It is the same variable with a different name.
Let's look at this program segment using normal and alias.
This equal 15 semicolon, normal++ semicolon, c out to less than signs, alias to less than
signs, single quote, backslash, n, single quote, semicolon.
With this program segment, the value 16 should be output.
A reference may seem like a simple idea at this point, but it is very important and widely
used in function parameters of C++ and in building C++ classes.
Let's examine references in more detail.
In C, there are two ways to pass a variable as a parameter.
One, passing the variable itself, which causes a duplicate of the variable to be created
when the function is called.
This can be inefficient, especially for large arrays and structures.
Two, passing a pointer to the variable.
This allows the function to access the caller's original variable.
In C++, there is a third option, passing a reference to the variable.
This is similar to passing a pointer since the address of the original variable is actually
passed, but the syntax of the function call and the called function is simpler.
Let's look at the three ways to pass a parameter to a function.
We'll use a large structure as the parameter called record.
We have three functions, value, pointer, and reference.
We've loaded the file sample8.cpp from the learning disk onto the computer.
It's important to read the comments associated with this program.
It will help you figure out exactly what we're talking about in our three functions, value,
pointR, and reference.
In the void main section, we see that we pass a copy of the variable, pass the address of
the variable, and pass a reference to the variable.
At the end of the program, and also near the beginning, we see that we have called by value,
called by pointer, and called by reference.
The call to the function value makes a copy of the parameter and passes it to the function.
The call to function pointer, pointR, passes the address of the record as does the call
to the function reference.
Using a reference parameter allows us to use a cleaner syntax because we can skip the and
in the function call.
And we don't have to use the hyphen greater than symbol within the function to address
the structure members.
When a reference is passed to a function, the original parameter can be modified by
the function if the reference parameter is not declared with a constant qualifier.
This can be misleading to people who read the program and assume that the function call
without the and on a parameter designates a call by value.
You should document your program well to guard against this or use pointer parameters for
those that may be modified by the called functions.
A reference can be used as a return value from a function.
This makes it possible to use a function on the left side of an assignment statement.
This can be used in dynamic memory allocation, operator overloading, and conversion functions.
It is important not to confuse the and sign used as a reference operator with the and
sign used as an operator to take the address of a variable.
When an and sign variable is preceded by the name of a type, it represents a reference
declaration.
Otherwise, the and sign means the address of the variable.
The most important and powerful feature of C++ is the class.
A class is a user defined object consisting of both data and function definitions.
Dates are the building blocks of object oriented programming.
In our discussion of C++ classes, we'll define a class for dates and date manipulation.
Before we do this, let's define a structure for dates in C.
We'll define the structure with this program segment.
Struct date, left brace, int month semicolon, int day semicolon, int year semicolon, a closing
brace, and a closing semicolon.
To store a date in our structure, we can use the assigned values to the structure members.
Struct date, b date semicolon, b date dot month equal 11 semicolon, b date dot day equal
23 semicolon, b date dot year equal 1955 semicolon.
If we want to print the date, we cannot print b date by passing it to print f.
We have to print the structure members individually.
This method for printing is incorrect.
Print f, open parenthesis, quotation mark, percent 2D slash, percent 2D slash, percent
4D backslash n, close quote, comma, b date, close parenthesis, semicolon.
This is the correct way.
Print f, open parenthesis, quotation mark, percent 2D forward slash, percent 2D slash,
percent 4D backslash n, quotation mark, comma, b date dot month comma, b date dot day comma,
and b date dot year, close parenthesis, semicolon.
Alternatively, we can write our own print date function.
It would look like this.
Void print underline date, open parenthesis, struct date, star x date,
close parenthesis, left brace.
Static character, star m name, empty square brackets, equal, left brace, quote zero,
end quote, comma, then the months, January, February, March, April, May, June, July,
August, September, October, and November, and December, all enclosed in quotes
and separated by commas, and then the closing brace.
Our last statement is print f, parenthesis, quote, percent s, comma,
percent d, comma, percent d, quote, comma, name, square bracket, x date,
hyphen, greater than symbol, month, square bracket, comma, x date, hyphen,
greater than sign, day, comma, and x date, hyphen, greater than sign, year,
close parenthesis, semicolon, and then the closing brace.
We may have the need to compare dates to see if one date falls after another.
This can be done by writing a date comparison function
or by comparing the structure members individually.
Other functions can be written to operate on the structure date.
When we define a data type in C and write functions for it,
there are some disadvantages.
First, validity checking does not always occur.
For example, in our print date function,
it's possible to print February 30, 1992, or an uninitialized day that
may be negative 14,356.
Second, once we've written the functions for date and used them in programs,
the implementation of the data structure cannot be easily changed.
For example, suppose that we needed to save space and combine the day
and month into a single integer from 1 to 365 day, or 366 for a leap year.
This could be done only by changing all the programs that use the date
structure and its functions.
Every expression uses day or month as separate integers must be rewritten.
It is possible in C to get around these problems
with some programming effort.
Class definitions in C++, on the other hand,
make it easy to define and use new data types.
Let's make a class definition in C++ to handle dates.
In C++, a class definition is similar to a structure definition,
but a class definition contains both data and function members, not just data.
We have loaded the file sample9.cpp from the learning disk.
The class date contains three data members, month, day, and year.
These data members are in the private section of the class definition.
This means the programs using the date class cannot access the data members
directly.
They must go through the dates member functions.
The member functions in our date class are date, the constructor, display,
a function to print the date, and tilde date, the destructor.
The constructor is a function used to create an object
or an instance of the class.
And the destructor is a function that destroys an object.
The constructor is an initialization function
that's called whenever an instance of the class is declared.
The constructor can be used to initialize the data members
and for checking the validity of the initialization values.
In our case, the constructor will make sure
the month is between 1 and 12 and that the day falls
within the valid range for the month.
It will also initialize the private data members day, month, and year.
Let's look at the member functions for the class date.
The member functions come immediately after the class declaration.
In the constructor, we accept three parameters for the month, day,
and year.
We assign these values to the private data members month, day,
and year.
And we check the input values mn and dy for a valid range.
The member function to print the date
is similar to our c function to print the date.
However, the private data members month, day, and year
are printed rather than the parameters.
The validity of these variables has
been checked in the constructor.
So we are assured of having a legitimate date.
The destructor for a date class is a null function.
We need a destructor function for the class definition.
But in the date class, we do not need
to take any action when a date is destroyed.
Now let's write a C++ program that uses the date class.
Yes, here we have the main program for date class.
Void main, empty parenthesis, left brace.
Date some day, parenthesis 9, 12, 1992.
Then we have date old day with a date 12, 25, 1942.
Next, we have some day dot display
with the open and closed empty parenthesis,
and old day dot display again with the empty parenthesis.
Then we close our program.
The declarations of some day and old day
contain three values in parentheses.
These are the parameters passed to the constructor
function of the date class.
The syntax of the function calls to the display member function
is new to C++.
In C, we used this form to display the date.
Print underscore date, parenthesis and date,
close parenthesis, semicolon.
However, in C++, we use this form.
Some day dot display, parenthesis, open and closed.
The some day means we are referring to the object some day.
And it's to be used in the call to the member function display.
This seems to imply that there is a copy of the function
display for each object of the class date.
But that's not true.
The same code for display is used for all the objects
of class date.
Only the data members of date are duplicated for each object.
In a class definition, we can have member functions and data
in the private and public sections of the class.
In our date class, we place the data in the private area.
This makes it easy for us to make modifications
to the data structure without affecting programs
that use our class.
Since the date is not used as parameters
in the call to display, we can completely
change the structure of the data without having
to modify our calling program.
Member functions that are used only within the class
can also be placed in the private section
of the class definition.
The public area can be thought of as the user interface
of the class, even though it is actually
the programmer who uses it.
In our date class, each date object
is initialized when it is declared.
This is not very flexible because it
is likely that we want to assign a date at runtime.
We need to have access to the data members.
We can do this in one of two ways.
We can place the data members in the public area of the class,
in which case they would be accessed much like a C
structure.
Or we can write a function to assign values to the members.
It's usually better to write a function to access the members
because this insulates the main program
from the internal structure of the data.
Also, things such as validity checking and type conversion
can be added to the member access functions.
Let's add member access functions
to our class that get and set the month, day, and year.
We see the member access functions in the middle here.
First, we get the month, the day, and the year.
Then we set the month, the day, and the year.
Now let's add the functions to read and modify
the month, day, and year.
We declare the function prototypes
with the class declaration and then add the number
functions in the class body.
We can modify the constructor function
to use these functions so the code will not
have to be duplicated.
Our constructor requires that an initial value
for the month, day, and year be specified whenever
a date object is declared.
We can remove this requirement by overloading the date
constructor and adding a function with no parameters.
This constructor is used in addition to the original.
It does not replace it.
That way, we have the option of initializing the object
when we declare it.
This is on the learning disk as the file sample12.cpp.
You'll want to study this file to get a better
idea of how classes work.
Let's see how soulful this class is.
Now let's look at a program that uses our date class.
The declaration of some day does not specify initial values,
so the constructor without parameters is used.
It's set to January 1, 1900 immediately upon declaration.
The date old day is set to December 25, 1942
when it's declared using the constructor with parameters.
Using the member access functions
rather than public data has two major advantages.
First, it's easy to perform consistent validity checking.
Second, and more important, it provides encapsulation
for your program.
Using functions to access the data members of a class
allow you to hide the implementation of the class
from the calling programs.
If the implementation of the class
is later changed or enhanced, it can generally
be done without affecting the calling programs.
This is not true if the calling programs access the data
directly.
It is a good idea to use access functions rather than
public data in C++ classes.
The next area we'll discuss is operator overloading.
Now let's assign the value of the object old day
to the object some day.
Remember, old day and some day are
occurrences of the date class.
They are date objects.
In other words, old day and some day
are variables of the class date.
But since a class has functions as well as data definitions,
we call them objects.
As our class is defined now, we must use the data access
functions to assign the dates.
We have replaced the constants in the cells
to set month, set day, and set year with calls
to get month, get day, get year.
This is a cumbersome way to assign the value of one object
to another.
It would be nice if we could assign it
across the equal sign in an ordinary assignment statement.
In fact, with C++, we can define the equal sign
or any other operator to work with an object.
We can actually define any function with any operator.
But common sense tells us to use functions similar to those
existing for the operator.
For example, our program would be very confusing
if we defined the plus sign to be a subtraction operator.
Now let's look at an operator definition for our date class.
In the class declaration, we can add this statement
to declare an assignment operator.
Date operator equal parenthesis date parenthesis closed semicolon.
This is the prototype for the date assignment operator equal.
The key word operator followed by the equal sign
means that we are defining the operator equal.
The first date is the data type of the result of the operation.
The second date is the operand, the data type of the object
to the right of the equal sign.
In the body of the class definition,
we define the operator much like we do the member functions.
Now we can change our main program
to assign old day to some day using a simple assignment
statement.
This is not only easier to write,
but the assignment is recognized at once by nearly anybody
reading the program.
Now if we run the program, it will display December 25,
1942 twice, once for some day and once for old day.
Microsoft C++, Borland C++, and most other implementations
of C++ come with the extensive class libraries.
You can use these libraries in place of or in addition
to the C++ function libraries.
For example, Microsoft C++ comes with an extensive foundation
class library for Windows programming,
which greatly simplifies programming C++ for Windows.
As we said at the beginning of this tutorial,
you won't be a C++ programmer after watching this one tape.
However, you should now understand
the fundamental concepts you need
to know in order to write C++ programs.
Don't forget that you can rewind the tape
to review any of the programming sections.
Also, most of these programs are included on the VIA graphics
learning disk, so you can take your time in reviewing them.
That concludes this video tutorial,
Programming in C++.
We at VIA graphics would like to thank you
for choosing our company for your computer training needs.
Remember, if you plan to learn C++ or any other computer
software, there is no better way than through video training
from VIA graphics.
I hope you enjoyed this video tutorial, and I'll see you
in the next video.