0% found this document useful (0 votes)
4 views

Programming in C++ - Module 02

C++ notes

Uploaded by

Sagar Yennam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming in C++ - Module 02

C++ notes

Uploaded by

Sagar Yennam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Module 02

Partha Pratim
Das Module 02: Programming in C++
Objectives & Programs with IO & Loop
Outline
Hello World
Add numbers
Square Root
Standard Library
Sum Numbers Partha Pratim Das
Using bool

Summary
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
[email protected]

Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan

NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 02

Partha Pratim Understand differences between C and C++ programs


Das
Appreciate the ease of programming in C++
Objectives &
Outline
Hello World
Add numbers
Square Root
Standard Library
Sum Numbers
Using bool

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline

Module 02

Partha Pratim Contrast differences between C and C++ programs for:


Das
I/O
Objectives & Variables
Outline
Hello World
Using math library
Add numbers Standard Library – Headers
Square Root
Standard Library Loop
Sum Numbers
Using bool bool type
Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 3


Program 02.01: Hello World

Module 02 C Program C++ Program

Partha Pratim // FileName:HelloWorld.c: // FileName:HelloWorld.cpp:


Das #include <stdio.h> #include <iostream>

int main() { int main() {


Objectives &
Outline printf("Hello World in C"); std::cout << "Hello World in C++";
Hello World printf("\n"); std::cout << std::endl;
Add numbers
Square Root return 0; return 0;
Standard Library
} }
Sum Numbers
Using bool

Summary Hello World in C Hello World in C++

• IO Header is stdio.h • IO Header is iostream


• printf to print to console • operator<< to stream to console
• Console is stdout file • Console is std::cout ostream (in std namespace)
• printf is a variadic function • operator<< is a binary operator
• \n to go to the new line • std::endl (in std namespace) to go to the new line
• \n is escaped newline character • std::endl is stream manipulator (newline) functor

NPTEL MOOCs Programming in C++ Partha Pratim Das 4


Program 02.02: Add two numbers
C Program C++ Program
Module 02
// FileName:Add_Num.c: // FileName:Add_Num_c++.cpp:
Partha Pratim #include <stdio.h> #include <iostream>
Das int main() { int main() {
int a, b; int a, b;
Objectives & int sum;
Outline
printf("Input two numbers:\n"); std::cout << "Input two numbers:\n";
Hello World
scanf("%d%d", &a, &b); std::cin >> a >> b;
Add numbers
Square Root
sum = a + b; int sum = a + b; // Declaration of sum
Standard Library
Sum Numbers
Using bool printf("Sum of %d and %d", a, b); std::cout << "Sum of "
printf(" is: %d\n", sum); << a << " and "
Summary << b << " is: "
return 0; << sum << std::endl;
}
return 0;
}
Input two numbers: Input two numbers:
34 34
Sum of 3 and 4 is: 7 Sum of 3 and 4 is: 7

• scanf to scan (read) from console • operator>> to stream from console


• Console is stdin file • Console is std::cin istream (in std namespace)
• scanf is a variadic function • operator>> is a binary operator
• Addresses of a and b needed in scanf • a and b can be directly used in operator>> operator
• All variables a, b & sum declared first (C89) • sum may be declared when needed
• Formatting (%d) needed for variables • Formatting is derived from type (int) of variables

NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Program 02.03: Square Root of a number
C Program C++ Program
Module 02
// FileName:Sqrt.c: // FileName:Sqrt_c++.cpp:
Partha Pratim #include <stdio.h> #include <iostream>
Das #include <math.h> #include <cmath>
using namespace std;
Objectives &
int main() { int main() {
Outline
double x; double x;
Hello World
double sqrt_x;
Add numbers
Square Root
printf("Input number:\n"); cout << "Input number:" << endl;
Standard Library
Sum Numbers
scanf("%lf", &x); cin >> x;
Using bool
sqrt_x = double sqrt_x = // Declaration of sqrt_x
Summary sqrt(x); sqrt(x);

printf("Sq. Root of %lf is:", x); cout << "Sq. Root of " << x;
printf(" %lf\n", sqrt_x); cout << " is: " << sqrt_x << endl;

return 0; return 0;
} }
Input number: Input number:
2 2
Square Root of 2.000000 is: 1.414214 Square Root of 2 is: 1.41421

• Math Header is math.h (C Standard Library) • Math Header is cmath (C Standard Library in C++)
• Formatting (%lf) needed for variables • Formatting is derived from type (double) of variables
• sqrt function from C Standard Library • sqrt function from C Standard Library
• Default precision in print is 6 • Default precision in print is 5 (different)

NPTEL MOOCs Programming in C++ Partha Pratim Das 6


namespace std for C++ Standard Library

Module 02 C Standard Library C++ Standard Library

Partha Pratim • All names are global • All names are within std namespace
Das
• stdout, stdin, printf, scanf • std::cout, std::cin
Objectives &
• Use
Outline
Hello World using namespace std;
Add numbers
Square Root
to get rid of writing std:: for every standard
Standard Library
library name
Sum Numbers
Using bool

Summary W/o using W/ using


#include <iostream> #include <iostream>
using namespace std;

int main() { int main() {

std::cout << "Hello World in C++" cout << "Hello World in C++"
<< std::endl; << endl;

return 0; return 0;
} }

NPTEL MOOCs Programming in C++ Partha Pratim Das 7


Standard Library Header Conventions

Module 02

Partha Pratim C Header C++ Header


Das C Program Use .h. Example: Not applicable
#include <stdio.h>
Names in global namespace
Objectives &
C++ Program Prefix c, no .h. Example: No .h. Example:
Outline
#include <cstdio> #include <iostream>
Hello World
Names in std namespace
Add numbers
Square Root
Standard Library
Sum Numbers
Using bool
Any C standard library header is to be used in C++ with a prefix ’c’ and
without the .h. These symbols will be in std namespace. Like:
Summary
#include <cmath> // In C it is <math.h>
...
std::sqrt(5.0); // Use with std::
It is possible that a C++ program include a C header as in C. Like:
#include <math.h> // Not in std namespace
...
sqrt(5.0); // Use without std::

This, however, is not preferred.


Using .h with C++ header files, like iostream.h, is disastrous. These
are deprecated. It is dangerous, yet true, that some compilers do not
error out on such use. Exercise caution.
NPTEL MOOCs Programming in C++ Partha Pratim Das 8
Program 02.04: Sum n natural numbers

Module 02 C Program C++ Program

Partha Pratim // FileName:Sum_n.c: // FileName:Sum_n_c++.cpp:


Das #include <stdio.h> #include <iostream>
using namespace std;
Objectives & int main() { int main() {
Outline int n; int n;
Hello World int i;
Add numbers int sum = 0; int sum = 0;
Square Root
Standard Library
printf("Input limit:\n"); cout << "Input limit:" << endl;
Sum Numbers
scanf("%d", &n); cin >> n;
Using bool

Summary for (i = 0; i <= n; ++i) for (int i = 0; i <= n; ++i) // Local Decl.
sum = sum + i; sum = sum + i;

printf("Sum of %d", n); cout << "Sum of " << n ;


printf(" numbers is: %d\n", sum); cout << " numbers is: " << sum << endl;

return 0; return 0;
} }

Input limit: Input limit:


10 10
Sum of 10 numbers is: 55 Sum of 10 numbers is: 55

• i must be declared at the beginning (C89) • i declared locally in for loop

NPTEL MOOCs Programming in C++ Partha Pratim Das 9


Program 02.05: Using bool

Module 02 C Program C++ Program

Partha Pratim // FileName:bool.c: // FileName:bool.c: // FileName:bool_c++.cpp:


Das #include <stdio.h> #include <stdio.h> #include <iostream>
#define TRUE 1 #include <stdbool.h>
#define FALSE 0 using namespace std;
Objectives &
Outline int main() { int main() { int main() {
Hello World int x = TRUE; bool x = true; bool x = true;
Add numbers
Square Root printf printf cout <<
Standard Library
("bool is %d\n", x); ("bool is %d\n", x); "bool is " << x;
Sum Numbers
Using bool
return 0; return 0; return 0;
Summary } } }

bool is 1 bool is 1 bool is 1

• Using int and #define for bool • stdbool.h included for bool • No additional headers required
• May use Bool (C99) • Bool type & macros (C99):
bool which expands to Bool bool is a built-in type
true which expands to 1 true is a literal
false which expands to 0 false is a literal

NPTEL MOOCs Programming in C++ Partha Pratim Das 10


Module Summary

Module 02

Partha Pratim Understanding differences between C and C++ for:


Das
IO
Objectives & Variable declaration
Outline
Hello World
Standard Library
Add numbers
Square Root C++ gives us more flexibility in terms of basic declaration
Standard Library
Sum Numbers and input / output
Using bool

Summary Many C constructs and functions are simplified in C++


which helps to increase the ease of programming

NPTEL MOOCs Programming in C++ Partha Pratim Das 11


Instructor and TAs

Module 02

Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Hello World Srijoni Majumdar, TA [email protected] 9674474267
Add numbers
Square Root Himadri B G S Bhuyan, TA [email protected] 9438911655
Standard Library
Sum Numbers
Using bool

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 12

You might also like