- [Instructor] Welcome back.
This week we are reviewing C++.
Basically, everything
that you learned from C
will still be there,
but we're adding classes and objects.
Classes, classes will help you
to encapsulate data and operations,
like you did in Java.
One big difference
between C++ and Java,
is that in C++ you can have classes,
but also you can have
methods and variables
that are not inside of classes.
So your program is a mixture
of what you have in Java,
everything inside of classes,
and what you have in C,
everything methods, and global variables.
In this example, you
have the class FooBar,
which have one attribute, int,
and one method.
Notice that the definition
of the class ends
with the closing curly bracket
and a semi-colon like structs in C,
and after the class we
have x as a global variable
and method main.
In C++ the method main
is not inside of any class.
You do not put main inside
of any of your classes.
As you did in Java,
the variables and methods
that you encapsulate
inside of a class
can be private, protected or public.
The only difference is that in Java
you specified private, protected or public
for each variable or method.
In C/C++ what you do is to
create these paragraphs.
So you put the keyword
private, and then a column,
and you can put variables
and methods there,
and all of them are going to be private.
Until you have another statement,
protected or public.
In the example, char a and int variable
are going to be private.
AnotherVariable is going to be protected,
and the methods, method1 and method2
are going to be public.
Usually methods are going to be public.
They are called the
interface of the class,
and variables are going to
be private or protected.
The idea of having classes
assist to create hierarchies,
and that basically means
that you can connect classes
by creating objects from a class
inside of another class,
or you can do inheritance
from one class to other.
To create an object,
you follow the process that you know,
the classes, the type,
and you need a name for the object.
So in this example, a is an
object from the class FooBar
inside of the class Rectangle,
and it's private.
To indicate inheritance,
we use the column.
The column is gonna
replace the extend keyword
that we used in Java.
Moreover in C/C++ you can have
public, private and protected inheritance.
Before you can indicate column public,
column private, and column protected.
For now, let me use public inheritance.
The first one is the
class you are creating,
like Rectangle,
and the second one Shape,
is the class from which
you are inheriting.
In Java you usually put one class
with clear methods and variables
inside of one file, a .java file.
In C++ we have the option
to put attributes and methods
inside of the class in one file.
We can put the definition of the methods
outside of the class,
and we can put in two different files
the definition of the class
and the definition of
the method of the class.
I'm gonna show you two examples,
one with everything in one
file inside of the class,
and one with a class defining one file,
and its methods implemented
in another file.
In this first example we have one class
and the method main in one file,
queue.cpp,
programs in C++ are gonna
have the extension .cpp,
meaning C++.
The first line of the program
is the inclusion of a library.
In C++ iostream
is gonna be the replace for stdio.
Usually in C++
you are not going to add the
extension .h to the libraries,
so you'd use the name inside
of the angular brackets.
The second line defines
that we're going to use
the namespace std.
That is gonna help us
with the standard input
and output instructions.
Inside of the class Queue
we have the three paragraphs,
private, protected and public.
We have one variable private,
three variables protected,
and three methods public.
Things that you are familiar
with are number one,
we have pointers,
there is a pointer to integer buffer.
As you remember from C,
that will be an array.
We have constructors as in Java.
If you put a method
the same name to the class
without a return type,
that is called a constructor,
so in C++ you will have also constructors.
This is a constructor for the class Queue.
The input and output instruction in C++
are cout and cin.
In this example we're using cout,
I'm printing some strings on the screen.
You will need to use cout
and cin in your homework,
so review the structure
of these instructions
in the book.
As you can notice, this
looks like a class in Java,
but notice that the closing curly bracket
that is closing the class
has a semi-colon also.
That is important.
Then, the main method
is outside of the class.
Inside of the main method
we are creating one object from the class,
queue one, and one pointer,
we're gonna talk more about
pointers in another video.
Then we can call the
methods from the class
using the name of the
object dot the method,
as usual in Java,
or using the pointer to the object,
the arrow, and the name of the method.
Let's review our second example.
Here we're gonna create
a class using two files.
In the first file we're gonna define
the structure of the class.
Usually that file is gonna
have the extension .h.
So for instance, if we
create a class Time,
we can create that class in a file time.h.
That file is gonna have
the variables of the class,
in this case hour and
minute, both private,
and we are gonna put the methods,
but without the body,
without the curly bracket
and all the instructions that go inside.
As notice in this case,
our methods, they are public.
We have the constructor,
and we have three methods.
And we need to add a semi-colon
after the definition
of each of the methods.
Finally a second file, time.cpp,
contains the definition of the methods.
The connection between the
.h file and the .cpp file
is done using the #include instruction.
Notice in red
that we are doing include time.h,
and time.h, it defines what we create
with the definition of the class.
Instead of angular bracket,
we use quotation marks,
and that means that that .h file
is part of our program,
not part of the libraries in the language.
An important thing to notice,
in order to define the
method as part of a class,
we use, before the name of the method,
the name of the class,
and we connect the name of the class
and the name of the method
with the scope resolution operator,
two columns together.
The second part of the same file
include the definition
of another method of the
class, printStandard.
You notice the name of the
class, the resolution operator
and the name of the method,
and finally the main method.
And as you notice the main method
is not part of the class.
We are not using the resolution operator
with the name of main.
Therefore basically we
have one file, the .h,
with the definition of the class,
and this file, .cpp,
with the bodies of the methods
that are part of the class,
and with main.
In this example the main method
is used, creating an object
for the class, Time t,
and then calling the
methods of that object.
This is a very simple example.
And that's it.
I recommend you to copy these examples,
and play with them.
Moreover, review the
cin, cout instructions
for input output in C++,
and the concept of namespaces.
See you in the next video.
