 Hello and welcome. This lecture is about the structure of a simple C++ program. Here is a quick recap of some topics that we have studied earlier and are relevant to this lecture. We have already seen the Dumbo model of computing and within that we have seen the notion of instructions, the notion of memory where we can write values and read values from that memory. We have seen the notion of input and output and we have seen an intuitive idea of a program as a sequence of instructions that Dumbo can execute. We will now build up on this in this lecture and see what a real C++ program looks like and more importantly what are its different components. So, this is an overview of this lecture. We are going to understand the structure of a simple C++ program. We are going to look at some common features, their significance of course all illustrated through an example and I would like to remind you here that there is much more to the structure of C++ programs and we are going to encounter these later in the course as we look at more complicated programs. So, here is our friendly C++ program that we have seen earlier which sums up two numbers. I have color coded different parts of this program to highlight the different components or parts that we are going to study in this lecture. I am going to use the same color coding scheme throughout this lecture. But at this point, let us just remember that a C++ program just like a Dumbo program is a sequence of instructions and there are a few other things that we will also see which we will call directives declarations and we will see what these are in today's lecture. Now, the sequence of instructions, directives and declarations have to be written according to some rules just like any of our natural languages like English or Hindi or Marathi have grammars and when we construct sentences or sentences have to follow the rules of the grammar. So, computer languages also have grammars and these basically define the rules in which we can write computer programs. So, when we write a C++ program, it has to be according to the rules of C++ grammar and when we write a C++ program, we usually store it in one or sometimes more than one files. Currently, we are going to just consider a C++ program written in one file. Typically, we use the extension dot CPP standing for C++. So, for example, we use the extension dot CPP standing for C++ to denote a file containing a C++ program. So, for example, this program could be saved in a file add to numbers dot CPP, but you could choose your own file name and add the extension dot CPP to it. So, now, let us look at our friendly program and I have highlighted in blue two lines at the beginning of the program has include IO stream with an angle brackets and using namespace std. These two lines are what are called compiler directives. What are compiler directives? These are instructions that are given to the compiler, which is the compiler is going to use when it is compiling your program in C++ down to the language of the machine. So, for example, if you look at hash include IO stream, this is basically telling the compiler to include some instructions that are already saved in a header file called IO stream. Header files often contain useful instructions that are used by various programs. So, we collect all of these useful instructions and put them together in a header file and then we include such compiler directives as hash include IO stream in our program files to basically tell the compiler, please go ahead and include the instruction from the IO stream header file. Now, why are we asking the compiler to include instructions from IO stream? This is because input and output in C++ programs are handled as streams of bytes. So, for example, when I type in something from the keyboard, the C++ program sees it as an input stream of bytes and it has to be converted to the internal representation of the computer. Similarly, when I ask a C++ program to print some result, the internal representation of the result must be converted to a stream of displayable characters. Now, who is going to do this conversion? There are special entities called CN and C out which you might have already seen in C++ programs. These are the entities that we are going to use for inputting from keyboard and for outputting to console and how do these work? These work based on instructions that are present in the IO stream header file. So, unless we tell the compiler to include the instructions from the IO stream header file, we will not be able to input values to our program or output values from our program using CN and C out. Here is another compiler directive called using namespace std. Now, in a program, there are going to be several objects and these names of objects can be grouped in several namespaces. A nice analogy is names of students in CS 101 grouped in different divisions. So, just like a division can be given a name, so each namespace can be given a name. And when we are referring to a name, we must indicate which namespace we are referring to. For example, we could say the student sham from division E. So, in a C++ program, we could say the variable my var name in the namespace my namespace. And we can actually use the same name in different namespaces because as long as we can distinguish which namespace the name is coming from, we know that these are different entities. So, for example, sham from division E would be different from sham from division A. So, similarly my var name from my namespace 1 would be different from my var name from my namespace 2. Now, by issuing the directive using namespace std to the compiler, we are basically asking the compiler to use a global namespace called std which means for every variable name that we are going to use, we are asking the compiler to prefix the name std as the name of the namespace to it. So, when we say my var name, we are really referring to std namespace and my var name variable in that std namespace. Now, here is our friendly program again and in this program, now I have highlighted in red int main open brace and close brace. So, this is really the main function of the program. The main function begins here at the open brace following int main and it ends here at the corresponding matching closing brace. Now, what is the main function? This is really the main part of the program. The operating system is going to invoke this function when the compiled program is run. In general, you could pass one or more parameters to this function. However, we have not passed any parameters in our example over here. If there were parameters, we could place them within these parentheses here. We will see more about functions with parameters in a later lecture and when the main function ends, then the operating system gets back control of the computer. Here is another very important component of a program which are called variable declarations. Now, if you recall the drawers of Dumbo in the Dumbo model of computing, these were basically used to store values. So, variables are very similar to drawers. They are basically used to store values and retrieve values and these are very basic computational objects. Almost every program will have variables. Now, when you have variables in a program, you must have what are called declarations for these variables. A declaration tells you what the name of a variable is and for each variable, it tells you what is the type of value that it can store. For example, here we are saying that there are three variables named a, b, c and each of these variables can store an integer. One very important rule to remember is that before we use a variable in a program, it must be declared. Now, remember we were using this compiler directive using namespace std. So, when we say a, b, c are three integer variables, what it really means is the variable a in the namespace std is an integer variable. Similarly, the variable b in the namespace std is an integer variable and similarly for c and now you can really see that it is really convenient to use a compiler directive like using namespace std. Otherwise, we will have to prefix this std double colon every time. Here is another very important component of our c++ program. These are input-output statements. We have already mentioned about c in and c out being used to do input from the keyboard and output to the console. And input-output statements are basically statements which allow us to interact with the program and these are going to be essential components of most programs that we will write. Here is another very interesting part of a program. This is an assignment statement. C is being assigned the value of a plus b. And assignment statements and similarly there will be other statements that we will see later in the course is really where the fun part happens in a program. So, if I look at the statement c is assigned a plus b, the a plus b part here is an arithmetic expression. It is saying take the value stored in the variable a, take the value stored in the variable b and add them up. The c is the destination of the assignment. This is where the result of this of evaluating this arithmetic expression will be stored. And so this entire assignment statement is an instruction which is asking you to add the values in these two variables and store it in the variable c. And as I said we will see lots more examples of other kinds of statements that allow us to do really cool stuff in C++ programs. Here at the end of the program we have a statement called return 0. This is also a very important component of a C++ program. This is used to return control back to whoever called the function. Here we are in the main function. To whoever called the main function that is the operating system in our case. So, by executing the statement we are returning control back to the operating system and we are also passing the value 0 back to the caller here the operating system. So, a return statement is basically used to return control to whoever called the function. Every function is called or invoked either by the operating system or by another function and the return statement returns control back to the caller. And the value that is returned along with the return statement just like here we were returning the value 0. This is very useful for returning the result of a computation or even for indicating error status if an error happens during the execution of the program. In this slide you can see that the statements highlighted in brown are basically grouped together between these two braces. We call these a logical block of statements and in C++ such logical blocks of statements can be grouped together using a pair of braces. And this is useful for demarcating different logical parts of a program. And within each group we can have its own variable declarations. We will see more of these advanced features later in the course. These groups can also be hierarchically arranged. So, for example here between these orange braces is a group of statements and within that group of statements these green braces denote another hierarchically arranged group of statements. So, here there is one top level group and under that there are two lower level groups. So, whenever you use these pairs of braces it forms a logical group of statements which can be enclosed within another pair of braces which forms a higher level group of statements. Finally in a program we can add English language descriptions of what the different parts of the program are doing. These are usually done by putting two double slashes at the beginning of the line. These are also called comments. Comments are absolutely essential for good readability of the program and you can put them anywhere in a program by putting two double slashes. Comments are completely ignored by the compiler. So, in a line of your file if you put a double slash then everything in that line up to the end of that line. Starting from the double slash up to the end of the line will be ignored by the compiler and it is here that where you can put good comments in plain English language. And it is good programming practice to insert adequate comments throughout your code. You should really make your program speak its story through good comments. So, here is a summary of what we have studied today. We have studied the structure of a simple C++ program. Within it we have seen compiler directives, the main function, variable declarations, input output statements, assignment statements, return statement. We have seen how to group statements into logical blocks using braces and finally we have looked at comments. Thank you.