0:06
OK, today we're going to continue with our main theme.
You as a C programmer, can almost instantly become a C++
programmer and that C++ will improve
your programming because C++ at a first take,
is a better C, and the migration from some of
the standard C techniques to C plus plus techniques are
very natural and easy. And you saw that our
first assignment, which is to convert a C program to C++
will ha-, w-, you'll be able to benefit by what we
go through today, to add a few other features that you
can do in that conversion. So, that's our chief agenda for today.
1:02
So why is C++ better? First
off, C++ is more type safe types
are the critical underpinnings of a programming language.
They're the domains that the programming language works under.
So, if we're in the native language, we have types
already like int, but we also have types like double.
1:48
Now one way to clear a variable to be, one of that type and we mix these types in
a program, we don't want a double which has a certain bit configuration.
To be reinterpreted as a pointer or an int.
That's just going to be terribly misleading.
Now, when C was invented in 1972 as
a system implementation language, very low level,
2:25
knew exactly what they were doing at the machine level.
And they wanted a lot of efficiency, and they even wanted the ability to hack code
readily, maybe make a transfer between domains, when they knew it to be safe.
2:40
But now, when we're going to C and C++'s general purpose programming
languages Coding large systems, having large teams of programmers
on a project, we need as much type safety,
2:57
as possible because type safety can be caught by the compiler, using
language rules, that's enforcing type safety, so that's one dimension.
Of improvement.
In
C plus over C. Now, C has not been static, so some of you
may know that there is a standard C 99 and it has improved
type safety, but C itself was designed to be
compiled. To a local architecture, and that
architecture the, the language. The compiled
code could be highly optimized for that particular platform.
And be, because of that the rules of the language were
3:48
a bit loose, it let the compiler writer for that platform,
choose a scheme that again, could be optimized for that platform.
So, at a simple example,
an architecture on one machine might have Eight
byte integers in another machine an integer may be naturally two bytes.
The compiler writer could choose what the size
4:17
and the range of the int was, so that when you would go to run a C program,
across different platforms. You might see different behaviors.
C++ makes a big step towards a
considerable amount more type safety. C++ has
generated more libraries. Libraries are very important.
4:45
In making a language and a system, powerful.
Why?
Professional libraries, first off, can be very heavily
tested, so they can assure that they're correct,
5:32
and C++ expands the number of libraries, the
scope of the libraries, the domains for those libraries.
And later we will be talking a lot about the
standard template library which is a major addition to the language
6:25
The sharp defines where in C, and in the C community, it was appropriate
to sharp-define global constants, and sharp-defined macros goes away.
That's no longer good practice.
In the C plus plus community.
And again, we'll revisit this but in software engineering,
software methodology has found that the pre-processor
which does not contextually obey the rules of the language, but instead
is something involving textual substitution
7:10
creates more opportunity for error than having the equivalent
facilities that are in the language and can be checked by the language rules.
So again, here's a place where C plus plus has importantly improved on C.
Finally, C was never intended as an object or a language.
Sort of a classic language
from the era of the 60's and 70's. It's a
language that's small, which is a good thing and it
supports imperative programming, which is a reasonable thing, but later
in doing large programs across many domains one found that the
object oriented paradine was a critical
paradigm to developing large scale software across many
domains using the same kernel language. So object
orientation is added to C in getting C
plus plus. So all of these things make C++ better,
and all of these things can be readily learned by people, with a
significant C background. So, so far we've seen in
programs we've already, designed that we've used,
inline ad const, and the inline and const
material replaces the preprocessor.
So sharp defined for macro gets Replaced
by inline as a keyword that modifies the function.
And that informs the compiler that it
should still retain all of the function semantics.
Nevertheless, try to avoid function call overhead.
9:15
Const is, again, a modifier to a
type, and C++, tries to inject
const into the Injects const into the
typing system, so that the compiler can
help in checking const correctness.
So, by understanding what variables are
non-modifiable, the compiler again
supports correctness, we have safe
cast in C++. The typical cast that involves an
understood conversion is given the key words static cast type.
10:09
We have another form of encapsulation, name case encapsulation.
[BLANK_AUDIO]
And again, in the development of programming,
and programming methodology, what's been discovered over the,
50, 60 years now, that there have been
big digital computers, lots of programming heavily applied.
10:41
That each generation of language design provides further encapsulation tools.
Capsulation means that you can build something in a self contained way.
Check it out and debug it, and then plug it in as a module, into something bigger.
And the more effective ways that you
can isolate something from its other interactions, the
better you can design and keep the code bug-free and this is just absolutely vital
to rating now the systems that may have as much, as a 100 million lines of code.
Systems that weren't even thought about when we
had the earliest digital configure, post world war two.
11:29
very famous quote by John Von Neumann, who is one of the principal founders
of the modern digital era is the great applied mathematician,
who worked on things like the Manhattan Project in World War II.
And sometimes we call a modern computer, a Von Neumann
because he was part of a team that conceived the
12:02
and Vinoimen thought that those machines, which he viewed as scientific instruments
might have 20 or 30 codes, programs. And that would be it.
That'd be the extent of their usefulness. So, you could hand tune them.
Write these codes. They wouldn't be that long.
Maybe they would at most be 1,000 lines and then you were done.
12:25
And of course, that was a total misconception,
you just couldn't foresee the, the, the usefulness, and especially, the usefulness
beyond numerical science application, that computers would be available for.
12:42
Okay, another thing that we're going to immediately
make use of is our iostream io again.
That blends some convenience with type safety of
avoid the use of print F's and scan F's,
where we have to tie things up with special formats.
13:03
And we're also going to have the capability of declaring any where in a
program and again the usefullness of this is Localization.
You want something near where it's used.
Especially inside a for statement, where you
typically want some kind of iterative variable.
In a, in a case of
a for statement you like to see something like for Int i, and i is
going to be what you iterate over in that, in the vicinity of that for statement.
And it just stays there, again, it's the localization, which is a good thing.
13:58
and answer this question questions, with const
double pi equal 3.14159, thus true of false?
This is create a non-mutable variable pi Is
it equivalent to sharp define of Pi 3.14159,
which is the pre-processor alternative, and see, are both of
the statements, the above statements true? Take a second.
14:41
const double PI equals 3.14159; is now a
declaration. That declaration has its context.
So wherever it's been declared, the language,
we'll have it's definition in that context.
We don't have to worry about a misplaced textural substitution.
15:06
Now, this does create basically a const, A const
is a constant, Non-mutable variable, Pi is a variable.
It is a variable of tight double, but
it cannot have its value changed in that scope.
So it's entirely safe.
The old style, which is B. Is it equivalent?
Well,
15:31
at a superficial level, it's equivalent.
But the answer is false, it's not an equivalent.
Sharp define means that, pi is a macro, and
the macro substitution is going to occur throughout pi file scope.
15:48
So, if for some strange reason, there is a pi
somewhere outside of the context that you meant to use
const, will still be substituted what that
3.14159. Again, this can lead to far more errors
because the textual substitution is not matched up with
things like blocks and classes and functions, the
context in which you are going to declare a variable.
So, both are not true, so that last thing is false.
[BLANK_AUDIO]