This course is for experienced C programmers who want to program in C++. The examples and exercises require a basic understanding of algorithms and object-oriented software.

Loading...

来自 University of California, Santa Cruz 的课程

C++ For C Programmers, Part B

72 个评分

This course is for experienced C programmers who want to program in C++. The examples and exercises require a basic understanding of algorithms and object-oriented software.

从本节课中

Hex and the use of AI and C++ Move semantics

This module explains Min-Max and the Alpha-Beta algorithm for game playing. Its programming topics include C++ 11 Move semantics and a detailed example of referential garbage collection.

- Ira PohlProfessor

Computer Science

Here are a whole bunch of Tree constructors.

Then call the appropriate node constructors.

And you get this sort of, base pointer.

And then here's how it all might work.

So I have two variables, A and B. I've assigned A the value 3.

B the value 4.

When they print this out so we will know that A has been assigned 3 and B 4.

And then I want you to compute these two trees and here, you can see them as

if you want

this is your Polish. And what's

going to be produced

here is Parenthesized

version. So this an expression

walker, it walks along the tree. Produces from a parenthesis

free notation the fully parenthesized and fixed notation.

And it also will do the evaluation.

So, you can actually find out what these trees will evaluate to algorithmetically.

Okay, given that, I want you to be able to

do all that. So that's, our Quiz on this topic.

Again I very, very much want you

to play with the example. Take it home, run a version

of it on your computer, try and improve it, try

and extend it. So what is this program going to print?

What's going to be printed by main? We can see all of these

and the first one is easy. The first printout statement is easy.

See what happens, answer.

Well, this is pretty trivial.

So we see that we would get A equals 3, B equals 4.

Just, very explicit with printing out that string.

Then we call the right(tree) constructors.

Notice we're going to have a whole bunch of constructors called.

This is going to call some copy constructors t1 is going to call

a tree constructor, which has two subtrees, it's

binary tree 2, has t1 embedded in it.

So we're going to use all of those various constructors that we saw.

We're going to use all this evaluation.

All this polymorphism.

And here we're going to do t1, and now we're going to call.

But walks the tree, and gets us

the, right this is call, this actually ends up getting us a print call.

Right,this operator's been overloaded and implicitly causes print.

Print walks that tree and does that fully parenthesized stuff.

And so if we look at, minus 5

is the inner most, but we build it first by building minus

5, and then we build the second segment which is A plus 4.

And then next binary and the binary operator there is times.

And then its print puts the last pair of parentheses.

So that's how we get this.

And then the semicolon comes out of that, and then equal

to, so that's that part. And now, we evaluate t2.

And t2 has t1 embedded. So that's, look at t1 embedded in there.

That's,

that's t1.

Remember, that's through a copy constructor, an

appropriate use of referential copying. So we get a big savings.

And then we add B to it, that's this part.

So we get one further set of parens.

And then, in the first part of the tree, the left tree, we get A minus 1.

And then, the overall tree

is a plus. So, you should end up

with that fully parenthesized expression. And then

if we were to evaluate them t1 evaluates to

minus 5 times

A, which is 3 because that's a leaf node with a valtab of 3.

So it's 3 plus 4 which is 7 so, minus 5 times 7 is minus 35.

So the inner tree is minus 35, we have to add 4, so

that's minus 31. And on the other side we

have A which is

3 minus 1, so we get 3 minus 1 or 2 and

that's equal to minus 29. Okay.

So,

that should be the answer you get.

If you had trouble with that as I say, very important to go home.

you may be home already and that's the value of taking this course online.

So right where you are, on your computer.

Play with this example, and make sure you

fully understand it because this example has great value.

It has value at the level at the

value of OO design, and inheritance hierarchies.

It has value in terms of garbage collection.

It has value in terms of parsing.

It has value in terms of understanding tree structure.

It's about five or six topics is all embedded in what is

among the best examples I've ever seen making

use of these interesting.

And sophisticated ideas in OO programming.

Okay, next time we're going to get back to

what is a relatively amazing development in AI.

it actually pervades lots of AI, so we're going to see

it in a context of writing an artificial intelligence

for the next program. This is going to be a Monte Carlo

Evaluation of positions. But there's a much bigger theme here.

This AI idea applies to things for example like machine translation, of natural

language even. And all sorts of problem solving areas.

So that will be our next topic in detail.