C++ Course 

Lesson 1 recap

Objectives

C language refresher

- History c/c++

- Expression & Statements

- Variables and elementary datatypes

- Operators 

- Compilation sequence

- Loops

C++ concepts

- namespaces

 

 

C/C++ History

- Simula first first computer language with Object Oriented support was created in the 1960's by Ole-Johan Dahl and Kristen Nygaard.

- Inspired by Simula's Object Oriented-ness smalltalk is invented, with one notable contributor being Alan C. Kay

In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of "The C Programming Language"  by Kernighan & Ritchie caused  a revolution in the commercial computing world.

Bjarne Stroustrup, added Object-Oreinted support to the C language, and while working in  AT&T Bell Labs he fathered a superset language named CFront to be later dubbed  C++.

                    C++ standard revisions

Year            C++ Standard          Informal Name 

1998    ISO/IEC 14882:1998               C++98

2003    ISO/IEC 14882:2003               C++03

2007    ISO/IEC TR 19768:2007          C++07/TR1

2011    ISO/IEC 14882:2011               C++11

2014    ISO/IEC 14882:2014               C++14

2017    to be determined                   C++17

C Expression & Statements

In a mathematically clean computer language like Pascal.

An expression(or function) returns a value, while a statement (or procedure) has only some side effects, like printing to the screen.

In C and it's C++ superset, the 2 are somewhat mixed

Any statement/expression must terminate with a semi-colon(;)

int main()
{
    1 + 2;     // Pure Expression, no side effects
    int x;
    x = 2;     // Seemigly an assigment statement but 
               // beside the side effects of assigning 2 to x
               // the statement returns the value 2, so the following 
               // are also possible
    int y;
    y = x = 100; // Assign both 100 to both x and y    
}

Variables 

Valid variables can start any with any alphabetic character or underscore (_) and any

consecutive letter can be either

alphanumeric or an underscore

 

 

 

 

The statement of declaring a 

variable is always 

<Type> <VariableName> [=Value]

Assigning a value is optional

In C++ assigning a value can also be perfumed as follows

<Type> <VariableName>(Value)

 

int main() {
    int _a;         // Valid
    int ab_cde123;  // Valid 
    int A;          // Valid
    int AbC_dE123;  // Valid
    
    int 1abc;   // Can't start with numeric 
    int @ab;    // Can't start with special character
    int a bc;   // Space is disallowed 
    
    // Declaration of variables with initialization.
    int variableName1 = 1; // Initialize to 1
    int variableName2(2);  // Initialize to 2
}

Elementary Data types

Primitive data in C are either integers, floats or pointers

They occupy in memory a certain number of bytes and have a certain range of values.

 

To identify the size in bytes of a datatype you can use the 

sizeof  operator as shown in the C source

 

#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits
#define INFO(TYPE)  std::cout << #TYPE << ": " << sizeof(TYPE) << " Bytes  "                   \
        << std::numeric_limits<TYPE>::min()    << " upto " << std::numeric_limits<TYPE>::max() << std::endl;
int main () {
  INFO(int);           // 4 Bytes   -2147483648 upto 2147483647
  INFO(unsigned int);  // 4 Bytes   0 upto 4294967295
  INFO(long);          // 8 Bytes   -9223372036854775808 upto 9223372036854775807
  INFO(unsigned long); // 8 Bytes   0 upto 18446744073709551615
  INFO(long long);     // 8 Bytes   -9223372036854775808 upto 9223372036854775807
  INFO(float);         // 4 Bytes   1.17549e-38 upto 3.40282e+38
  INFO(double);        // 8 Bytes   2.22507e-308 upto 1.79769e+308
  INFO(long double);   // 16 Bytes  3.3621e-4932 upto 1.18973e+4932
}

Operators

Operands can be categorised by the number of operands they accept

Examples for unary operators in C 

 - negative (-)   Ex.  -2

-  negation (!)   Ex. !true 

Examples for binary operators in C

- Arithmetic addition (+)  Ex 1 + 2

- Arithmetic subtraction (-) Ex  3 - 1

The only ternary Operator is the Ternary conditional

- (cond) ? (if true) : (if false).

       Ex ( 2 > 1) ? 10 : 1  will return 10 because condition is true

Operators - Arithmetic

++a and a++ are equivalent to a=a+1

 --a and a-- are equivalent to    a=a-1

the prefix operators perform inc/dec first and than return the value, while postfix will first return the value and than  inc/dec

Example.

   int a = 10;

   int b = a++;   // b is assigned the value 10 (Now a is equal to 11)

   int c = ++a;   // b is assigned the value 12 ( Now a is equal to 12)

 

 

int main() {
   int a = 10;  // Assignment operator
   a + 10;      // Addition operator
   a - 10;      // Subraction operator
   +a;          // Unary plus operator
   -a;          // Unary negation operator
   a * 2;       // Multiplication operator
   a / 2;       // division operator
   a % 2;       // modulu opeator
   ++a;         // Prefix Increment operator 
   a++;         // Postfix Increment operator
   --a;         // Prefix decrement operator
   a--;         // Postfix decrement operator
}

Operators - Comparison

Any of the comparison operator returns a boolean value of either true or false.

int main() {
   1 == 2;     // Equality operator
   1 != 2;     // Inequility operator
   1 > 2;      // Greater than
   1 < 2;      // Little than
   1 >= 2;     // Greater or equal to
   1 <= 2;     // Little or equal to
}

Operators - Logical

All the logical operator in C++ return a boolean value (true or false).

int main() {
  false and true;   // Logical and operator
  false && true;    // Logical and operator 
  false or true;    // Logical or operator
  false || true;    // Logical or operator
  not true;         // Negation operator
  !true;            // Negation operator
}

Operators - Bitwise

Bit wise operator return an ordinal type, after the bitwise manipulation

int main() {
    unsigned char a = 1;  // 00000001
    ~a;                   // 11111110 (bitwise not)
    a & 255;              // 00000001 (bitwise and)
    a | 255;              // 11111111 (bitwise or)
    a ^ 255;              // 11111110 (bitwise xor)
    a << 2;               // 00000100 (shift left 2 places)
    a >> 1;               // 00000001 (shift right 1 place)
}

Operators - Compound assignment

Compound assignment is a shorter version of manipulating the data and re-assinging it to the same variable

int main() {
    unsigned char a = 1;
    a += 10;              // same as a = a + 10
    a -= 10;              // same as a = a - 10
    a *= 10;              // same as a = a * 10
    a /= 10;              // same as a = a / 10
    a %= 10;              // same as a = a % 10
    a &= 10;              // same as a = a & 10 (bitwise and)
    a |= 10;              // same as a = a | 10 (bitwise or)
    a ^= 10;              // same as a = a ^ 10 (bitwise xor)
    a <<= 10;             // same as a = a << 10 (bitwise left shift)
    a >>= 10;             // same as a = a >> 10 (bitwise right shift)
}

Operator -Pointer & Member

Pointers & Arrays

Members

int main() {
    int arr[] = {1,2,3,4};
    arr[1];  // = 2. Zero based array subscript. gives the 2nd cell in an array
    *arr;    // = 1. Dereferecing can be performed on arrays as well as pointers.
    
    int *pArr = arr;
    pArr[1]; // = 2. Array subscript also operates on pointers
    *pArr;   // = 1, dereference Pointer, returns value pointed by pArr.
}
struct DemoStruct { 
    int first; 
    int second;
};

int main() {
    DemoStruct s;
    s.first = 1;         // structure reference, to access field of struct
    
    DemoStruct* pS = &s; // Ampersand means "address of" 
                         // pS is assigned addr of s                        
    pS->first = 2;       // Pointer to struct accesses fields via arrow operator
}

Compilation sequence

Source files are fed to the Preprocessor that performs Macro substations & conditional source inclusions.

Compiler receives the preprocessed source files and converts them to assembly.

The Assembler converts the assembly to machine code object and library files.

Linker weaves the necessary machine code together to create a library or an executable

Loops

For loop

While loop

do..while loop

int main() {
    
  // For takes 3 parameters separated by semicolon
  // - Initializer 
  // - Condition. Repeat loop while the condition is evaluated true
  // - Increment clause
  for (int i=0; i < 10; i++)
     continue;   // Continue keyword, instructs to skip the iteration.
}
int main() {
    int i=0;
    // While is performed as long as the condition evaluates true
    while (i < 10) {
        i++;
    }
}
int main() {
    int i=0;
    do {
      // ...
    } while (i++ < 10);
}

Namespaces

Introduced to avoid symbol collisions. 

When all symbols are in one logical namespace there chances increase that several symbols will share a name.

 

 

We can import symbols to the singleton global namespace

// Namespace delaration with variable & function
namespace myNamespace {
   int variables = 1;
   void foo() { /* do something */ }
}  // There is no need for semi-colon after the namespace scope.

// Adding to a namespace is possible. 
namespace myNamespace { 
    void bar() { /* do something else */ }
}

int main() {
    myNamespace::foo();  // Using symbols inside a namespace 
}
// Import all namespace symbols to global namespace
using namespace myNamespace;

Namespace - cont

We can selectively import symbols to global namespace 

using myNamespace::foo;  // Only import foo from myNamespace to global namespace
int main()
{
     foo();
}
Made with Slides.com