C++: Complete Developers Guide: Part — 3

C++: Complete Developers Guide, khizaruddins, khizaruddins.medium.com
Photo by Alejandro Luengo on Unsplash

Arrays:

Arrays are data types which are used to store multiple values, there is an index through which we can access array elements, index is just the position of an element inside an array, indices plural form of index, index starts from 0 and till the length of array -1.

C++: Complete Developers Guide Part 3, Arrays, khizaruddins, khizaruddins.medium.com
int days_in_week [7]; // eg. 1const int days_in_year [356]; // eg. 2int test_scores [5] {1,2,3,4,5}; // eg. 3const double high_score [10] {20,40}; // eg. 4int dynamic_array [] {1,93,4,53,23}; // eg. 5
C++: Complete Developers Guide Part 3, Access arrays, khizaruddins, khizaruddins.medium.com
Photo by Katerina Pavlyuchkova on Unsplash

Accessing array element:

An array has its first value starting form index 0 and so on.., to access first element or item of an array we use name of that variable holding array elements followed by square bracket passing index of the element to be found.

const string odd_numbers [5] {1, 3, 5, 7, 9};cout << odd_number[2] << endl; 
const int rows {3};
const int cols {4};
int movie_rating[rows][cols];
C++: Complete Developers Guide Part 3, Multi dimensional Arrays, khizaruddins, khizaruddins.medium.com
cout << movie_rating[0][1]; // this will print 4 from first row
C++: Complete Developers Guide Part 3, Multi Dimensional Array, accessing array, khizaruddins, khizaruddins.medium.com

Vectors:

Arrays are fixed in size, there are many real use cases where we need dynamic arrays, for such cases we need Vectors, Vectors are dynamic arrays, they are containers in C++ Standard Template Library which can grow or shrink in size at execution time, they provide similar syntax and semantics as that of an array, they are efficient than arrays, they have same indexing like arrays, example as below for declaration of vector.

#include <vector>
using namespace std;
int main () { vector <int> test_scores; vector <char> vowels; return 0;}
vector <int> scores (5);
vector <int> scores { 20, 60, 70, 90, 45 };
vector <double> temperatures (365, 80.3);
scores.at(3) = 78;
vector <int> scores {100,44}; cout << scores.at(3);
// for eg.
vector <vector<int>> scores { {1,2,3,4}, {4,5,3,2} };
cout << scores[0][1]; // this will print out 2
cout << scores.at(0).at(1); // this will print out 2

Expressions, Statement and Operators

Expressions are building blocks of statements and statements are building block of programming, we have already seen I/O statements, variable declaration statements, return statements, assignment statements.

34 // literalsscore_number = 32 // variable1.5 + 1.8 // add2 * 5 // multiplya < b // comparea = b // assignment
C++: Complete Developers Guide Part 3, Statements, khizaruddins, khizaruddins.medium.com
Photo by Ian Schneider on Unsplash
int x; // declaration statementscore_point = 90;  // assignment statement1.5 + 1.8 // expressionx = 2 * 5; // assignment statement if (a < b) cout << "a greater than b" << endl; // if statement; // null statement

Operators

C++ has unary operator binary and ternary operator,

  1. Assignment operator : Assigns value to operands. (=)
  2. Arithmetic operator: Does arithmetic operations on operands. for eg (+,- *, /)
  3. Increment / Decrement operator: Increases or decreases value of operands.
  4. Relational operator (comparison): Compares two values.
  5. Logical operator: tests for logical or boolean conditions. eg ‘and’ or ‘or’ operator
  6. Member access operator: as we have seen to access array in specific index for eg. scores[2]
C++: Complete Developers Guide Part 3, Arithmetic operators, khizaruddins, khizaruddins.medium.com
Photo by Dan-Cristian Pădureț on Unsplash

Arithmetic operator:

These operators performs arithmetic operations, Normally it includes addition (+), subtraction (-), multiplication(*), division (/) and modulo (%).

#include<iostream>
using namespace std;
int main () {
const double usd_rate {75.89}; // 1 USD is 75.89INR today,3/3/2022

double usd {0.0};
double inr {0.0};

cout << "Enter the value in USD" << endl;

cin >> usd;
inr = usd * usd_rate; // arithmetic expression

cout << "$" << usd << " is equivalent to ₹"<< inr << endl;
return 0;
}

Increment and Decrement operator:

Basically it is a plus plus (++) increment operator or minus minus (- -) decrement operator, which means increment operand by one or decrement operand by one, it is also used to move pointers will talk about pointers in later sections. these operators have two variants depending on their placement, prefix notation and postfix notation, that means before operand or after operand.

#include <iostream>using namespace std;int main () {  int incr {0};
int result {0};
// example 1 result = incr + 1; // result becomes 0 + 1 ~ 1
incr++; // short version of incr = incr + 1; incr ~ 1;
++incr; // works same as incr++, hence incr ~ 2

result = result + incr; // 1 + 2 ~ 3
cout << "Eg1. Result: " << result << endl;
// example 2 result = 0; // resetting values
incr = 0;
result = ++incr; // increment 1 and then store in result cout << "Eg.2 Result: " << result << endl;

result = incr++;
cout << "Eg.2 Result: " << result << " Incr: " << incr << endl; return 0;}
Eg1. Result: 3
Eg.2 Result: 1
Eg.2 Result: 1 Incr: 2
2 * 3.5 ~ 7.0 // 2 is promoted to 2.0int num {0};
num = 100.2;
// 100.2 is demoted to 100 and num will hold 100
#include <iostream>
using namespace std;
int main () { int total_amt {100};
int total_num {10};
double avg {0.0};
avg = total_amt / total_num; cout << "Average: " << avg << endl; // output Average: 12 avg = static_cast<double>(total_amt) / total_num; cout << "Average: " << avg << endl; // output Average: 12.5 return 0;
}

Equality or Comparison operators:

Equality operator determines whether two variable holds same values or not if it is same it results to true if not same it results false, whereas comparison operator, compares variable value with some number or two different variable to be greater or less than each other, we can also test for expressions having two variables not being equal, or password match etc.

expr1 == expr2 // true, only if expr1 and expr2 values are same 
expr1 != expr2 // true, only if expr1 and expr2 values are not same
num1 == num2 // for eg, num1 is 100 and num2 is 200,
// hence, 100 == 200, so it results, false
bool result {false};result = (100 == (50 + 50);
result = (num1 != num2);
cout << (num1 == num2)<< endl; // if false prints 0 if true prints 1cout << std::boolalpha;
cout << (num1 == num2) << endl;
// now it will print true or false
cout << std::noboolalpha; // now it will print 0 or 1
cout << num1 <= 10; // compares num1, less than or equal to 10
cout << num2 < 10; // compares num2, less than, 10
cout << num1 > 10; // compares num1 greater than, 10
cout << num1 >= 10; // compares num1 greater than or equal to 10
C++: Complete Developers Guide Part 3, Logical operators, khizaruddins, khizaruddins.medium.com
Photo by Lidiia Nemyrova on Unsplash

Logical Operators:

There are mainly three operators ,which are ‘not’ (!) operator or negation ‘logical and’ (&&) operator and ‘logical or’ (||) operator. It allows us to create complex condition in programming.

  1. Logical not truth table (!):
C++: Complete Developers Guide Part 3, Logical not truth table, khizaruddins, khizaruddins.medium.com
bool a {true}; 
cout << a; // prints 1
cout << !a; // prints false or 0
C++: Complete Developers Guide Part 3, Logical and truth table, khizaruddins, khizaruddins.medium.com
bool a {true};
bool b {false};
cout << a && b;
// prints false as true && false ~ false
C++: Complete Developers Guide Part 3, Logical or truth table, khizaruddins, khizaruddins.medium.com
bool a {true};
bool b {false};
cout << a || b;
// prints false as true || false ~ true
  1. Logical not, has higher precedence than, Logical and.
  2. Logical and, has higher precedence than, logical or.
num1 <= 10 && num1 < 20 // both expression must be true for entire
//
expression to be true
num1 <= 10 || num1 >= 20 // only one or both expression needs be
// true for expression to be true
!is_raining && temp >= 32.0 // here, first logical not will get
// evaluated, then logical and
temp > 100 && is_humid || is_raining // here, first logical and will
// be evaluated, then or

Short Circuit Evaluation:

C++ stops evaluating an expression as soon as it knows the result. for eg, expr1 && expr2 && expr3

C++: Complete Developers Guide Part 3, Compound assignment operator, khizaruddins, khizaruddins.medium.com

TLDR;

For more tech related blogs pls visit:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store