computer programingcs101/2014.2/lecture-slides/...•include instructions from “iostream” header...

23
IIT Bombay Computer Programing Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Structure of a Simple C++ Program 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Upload: others

Post on 09-Aug-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

Computer ProgramingDr. Deepak B Phatak

Dr. Supratik ChakrabortyDepartment of Computer Science and Engineering

IIT Bombay

Session: Structure of a Simple C++ Program

1Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Page 2: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• Dumbo model of computing

• Notion of instructions

• Notion of memory• Reading and writing values in memory

• Notion of input and output

• Intuitive idea of program

2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Some Relevant Topics

Page 3: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• Understand structure of a simple C++ program• Some common features

• Significance of features

• Example program

• There’s more to structure of C++ programs• We’ll see these later in the course

3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture

Page 4: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

4 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program

• A program is a sequence of directives, declarations and instructions

• Written according to some rules Computer languages also have grammars!

• Stored in one (or more) filese.g. add_two_numbers.cpp

Page 5: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

5 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Compiler Directives

Page 6: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• Instructions to compiler used when compiling your program to machine language

#include <iostream>

• Include instructions from “iostream” header file

• Input/output handled as ‘streams’ of bytes• Input stream converted to internal representation of computer• Internal representation converted to stream of displayable characters

• “cin” (for keyboard input) and “cout” (for console output) work because of instructions in “iostream” header file

6 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Compiler Directives

Page 7: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

using namespace std;

• Names of objects in a program can be grouped in “namespaces”• Analogy: Names of students in CS101 grouped in divisions

• Each “namespace” can be given a name• Analogy: Each division can be given a name

• When referring to a name, we must indicate which “namespace” we are referring to

• Analogy: Shyam from division E• C++ programs: myNameSpace::myVarName

• Can use same name in different namespaces• Shyam from division E different from Shyam from division A• myNameSpace1::myVarName different from myNameSpace2::myVarName

• “using namespace std” asks compiler to use a global namespace called “std”• myVarName refers to std::myVarName

7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Compiler Directives

Page 8: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

8 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

“main” function begins here

“main” function ends here

Page 9: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• Literally, the main part of the program

• Operating system invokes this function when you run your compiled program

• Can pass one or more parameters to this function

• None passed in our example

• Operating system gets back control of computer when this function ends

9 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

“main” function

Page 10: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

10 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Variable declarations

Page 11: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• Variables• Recall the drawers of Dumbo• Basic computational objects

• Declarations• Each variable must be named• For each variable, we must indicate type of value it can store

• A variable must be declared before it is used

• Remember “using namespace std”• int A, B, C; really means int std::A, std::B, std:C;• Isn’t it convenient to use “using namespace std”?

11 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Variables and Declarations

Page 12: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

12 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Input/Outputstatements

Page 13: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• This is how we interact with the program

• “cin” allows input from keyboard

• “cout” displays/outputs on console

• Essential components of most programs we will write

13 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Input/Output Statements

Page 14: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

14 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Assignment statement

Page 15: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• This is where the program does the really interesting part

• C = A + B ;• A + B: Arithmetic expression

• C: Destination of assignment

• Instruction:

Add values in variables A and B, and store it in variable C

• We’ll see lots more examples of statements that allow us to do fantastic stuff with our programs

15 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Assignment (and other) Statements

Page 16: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

16 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Return control back to caller (here, OS), and pass the value 0

Page 17: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• A function is called/invoked• By operating system (e.g. “main” function)

• By another function

• “return” returns control to caller• In our example, the operating system

• Can also return a value to caller• Useful for returning result of computation

• Useful for indicating error status

17 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

“return” Statement

Page 18: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

18 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

A logical block ofstatements

Page 19: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• In C++, statements can be grouped by enclosing them within a pair of ‘{’ and ‘}’

• Each group is treated as one logical unit of the program

• Useful for demarcating logical parts of a program

• Each group can have its own variable declarations

19 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Grouping Statements

Page 20: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; { cout << “Give two numbers”; cin >> A >> B; }C = A + B; {cout << “Sum is” << C; return 0; }

}

20 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Hierarchically grouped blocks

Page 21: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

// file add_two_numbers.cpp#include <iostream>using namespace std; // this program reads two integers // and calculates the sum int main() {

int A, B, C; cout << “Give two numbers”; cin >> A >> B;C = A + B; cout << “Sum is” << C; return 0;

}

21 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Our Friendly C++ Program ...

Comments

Page 22: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

• Essential for good readability of program

• Can appear anywhere in program

• Completely ignored by compiler

• Good programming practice• Insert adequate comments throughout your code

• Make your program speak its story through good comments

22 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Comments

Page 23: Computer Programingcs101/2014.2/lecture-slides/...•Include instructions from “iostream” header file •Input/output handled as ‘streams’ of bytes •Input stream converted

IIT Bombay

Summary

• Structure of a simple C++ program• Compiler directives

• “main” function

• Variable declarations

• Input/output statements

• Assignment (and other) statements

• “return” statement

• Grouping statements into logical blocks

• Comments

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 23