Professional Documents
Culture Documents
Overview of CPP
Overview of CPP
Overview
ME5107: Numerical Methods in Thermal Engineering
Jul-Nov 2023
History of C++
▪C++ is a middle level OOP (object oriented programming) language created by
Bjarne Stroustrup in 1979 at Bell Labs.
▪It is an extension of C and hence, all programmes written in C can run on C++.
▪Over the years, it has been a handy tool to build other software packages,
especially driver software and software for server applications.
#include <iostream>
Self study:
using namespace std; ▪ Variable naming convention
▪ Keywords and reserved words
int a = 15; //global definition of variable ‘a’ ▪ Constants
▪ Literals & escape sequences
int main() // Main function ▪ Type qualifiers
{ ▪ Storage classes
int a = 10; //local definition of variable ‘a’
cout << a;
return 0;
}
ME5107: NUMERICAL METHODS IN THERMAL ENGINEERING 8
Basic I/O operations with iostream
▪Input operations are done using cin (standard input stream) and output/printing
operations are done using cout (standard output stream).
Syntax:
cin >> Var1 >> Var2 >>... >> VarN;
cin.getline(“character string”, max_string_length);
cin.get(character_input);
cout << “Some printing” << Var1 << “Another printing” << Var2 << endl;
Courtesy: w3schools.com
Courtesy: w3schools.com
Courtesy: w3schools.com
Courtesy: w3schools.com
Courtesy: cppbetterexplained.com
Self study:
▪ Miscellaneous operators
Courtesy: geeksforgeeks.org
Short hand:
Courtesy: w3schools.com
int main() {
for(int i=0; i<=20; i+=2)
{
cout << i << ‘\n’;
}
return 0;
}
Courtesy: tutorialspoint.com
int main() {
int i=0;
while(i<=20){
cout << i << ‘\n’;
i+=2;
}
return 0;
}
Courtesy: tutorialspoint.com
int main() {
int i=0;
What is the difference do {
between the do-while and cout << i << ‘\n’;
while loops? i+=2;
} while(i<=20);
return 0;
}
Courtesy: tutorialspoint.com
int main() {
int i,j,num_f;
for(i=2;i<=100;i++) {
j=2; num_f=0;
while(j<i) {
if(i%j==0) num_f++;
j++; }
if(num_f == 0) cout << i << “ is a prime number\n”; }
return 0;
}
int main() {
int i=0;
while(i<=20){
cout << i << endl;
if(i==15) break;
i++;
}
return 0;
}
Courtesy: tutorialspoint.com
int main() {
for(int i=0; i<=20; i++){
if(i==15) continue;
cout << i << endl;
}
return 0;
}
Courtesy: tutorialspoint.com
int main() {
result = function_name(<actual parameters>); //Function call
return 0;
}
▪The formal arguments can be set as default parameters so that if no parameters are
passed to the function, the default value is considered.
▪This method of passing values of the arguments is called Call by value.
Declaration + Initialisation:
int num[] = {1,2,3,4,5}; // No need to mention the array size
Access:
num[0] = 1; num[1] = 2; num[2] = 3; num[3] = 4; num[4] = 5;
for(int i=0;i<5;i++) {
cout << num[i] << "\t"; }
Declaration + Initialisation:
int num[][2] = {{1,2},{3,4},{5,6}};//Last dimension size needs to be
mentioned
Access:
num[0][0] = 1; num[0][1] = 2; num[1][0] = 3; num[1][1] = 4; num[2][0] = 5;
num[2][1] = 6; // a[i][j] is 𝑎𝑖𝑗 , i.e. i=row index, j=column index
for(int i=0;i<3;i++) {
C++ stores data in a row major format. So, taking
for(int j=0;j<2;j++){
i (row index loop) as outer loop and j (column
cout << num[i][j] << "\t"; }
index loop) as inner loop is faster.
cout << "\n"; }