Todayโ€™s Outline


  • C++ basic syntax
  • Variable and Constant
  • Operators
  • Basic I/O

C++ basic syntax


Tokens in C++

  • C++ compiler collects the characters of the program into tokens
  • Tokens are separated by space

Keywords

  • Keywords are words reserved by the programming language
  • Each keyword cannot be used for other purposes

Identifiers

  • Identifiers give unique names to various objects in a program like the name of variables, functions, libraries, and namespace
  • Keywords cannot be used as identifiers
  • An identifier is composed of a sequence of letters, digits, and underscore
    • e.g. myRecord, point3D, last_file
  • An identifier must begin with either an underscore or a letter

Variable and Constant


  • Variable - memory storage whose value can be changed during program execution
  • Constant - memory storage whose value cannot be changed during execution

Every variable/constant has 5 attributes

  • Address - location of data in memory storage
  • Value - content in memory storage
  • Name - identifier of the variable
  • Type - C++ is a strictly typed language, variables/constants must belong to a data type
  • Scope - it defines the region within a program where the variable/constant can be accessed, and also the conflict domain

Variable Declaration

int age;
float bathroom_temperature = 28, bedroom_temperature = 30;
char initial;
char student_name[20];
  • Variable/constants must be declared before use

C++ Predefined Data Types

  • Numerical
    • int short long - integer number
    • float double - real number
  • Character
    • char - ASCII character
  • Logic
    • bool - Boolean
  • Other
    • void - empty values

int

  • Typically, an int is stored in 4 bytes(32 bit)
  • The most significant bit of an int data type is the sign bit
    • 0: positive / 1: negative
  • C++ uses twoโ€™s complement to encode negative numbers

short, long, and unsigned

  • long - 8 bytes
  • short - 2 bytes
  • unsigned - non-negative 4 bytes
    • no sign bit, therefore 0 ~ 2

char

char c = 'a';
  • char takes 1 byte representing up to 256 characters

string

char course_name[] = "Computer Programming";
char *course_name = "Computer Programming";

float

float height;          // 4 bytes (7 digits after decimal point)
double weight = 120.8; // 8 bytes (15 digits after decimal point)
long double number;

sizeof

#include <iostream>
 
using namespace std;
int main() {
    int a = 4;
    cout << sizeof(a) << endl;
    cout << sizeof(int) << endl;
    cout << sizeof(double) << endl;
    cout << sizeof(long double) << endl;
    return 0;
}
  • sizeof can be used to find the number of bytes needed to store an object (variable or data type)

Type Conversion

// Binary expression
int r = 2;
double pi = 3.14159;
cout << pi * r * r << "\\n"; // double
 
// Assignment
double a = 1.23;
int b = a;
cout << a << "\\n";
 
// Type-Casting
double a = 1.23;
int b = (double) a;
cout << a << "\\n";
  • Implicit type conversion
    • Binary expression: lower-ranked operand promoted to a higher-ranked operand
    • Assignment: right operand is promoted/demoted to match the variable type on the left
  • Explicit type conversion (type-casting)
    • e.g. double b = (double) a;

Constants

const double light_speed = 299792.458;
const int days_per_week = 7;
const char[] my_email = "jun.huang@cityu.edu.hk";
  • Constants must be initialized during the declaration

Scope

#include <iostream>
 
using namespace std;
int a = 0;
int main() {
    int a = 1;
    cout << "a in main: " << a << endl; // will use local variable
    return 0;
}
  • Scope of a variable/constant refers to the region of a program where the variable/constant is visible (can be accessed)

Scope using namespace

#include <iostream>
 
using namespace std;
int a = 0;
namespace level1 {
    int a = 1;
    namespace level2 {
        int a = 2;
    }
}
 
int main() {
    int a = 3;
    cout << ::a << endl; // will use global variable
    cout << level1::a << endl;
    cout << level1::level2::a << endl;
    return 0;
}

Operators


Assignment Operator, =

float a = 2.0 * 4.0 * 8.0;
float b = a - sprt(a);
char x = 'a';

Increment & Decrement Operators

#include <iostream>
using namespace std;
int main() {
    int x = 3;
    cout << ++x; // 4
    cout << x++; // 4
    cout << x;   // 5
    return 0;
}

Precedence & Associativity of Operators

a = b+++c.     // same as (b++) + c
 
int a, b = 1;
a = b = 3 + 1; // a = 4, b = 4
  • Precedence - order of evaluation for different operators
  • Associativity - order of evaluation for operators with the same precedence

Bitwise Operators

#include <iostream>
using namespace std;
int main() {
    short a = 3, b = 5, c;
    c = a & b;
    cout << c << endl; // 1
    c = a | b;
    cout << c << endl; // 7
    c = a ^ b;
    cout << c << endl; // 6
    char t = 254;
    c = ~t;
    cout << c << endl; // 1
 
    char x = 6;
    int i = (x >> 1) & 1; // 0110 -> 0011 -> 0001 -> 1
    cout << i << endl;
    int j = (x >> 3) & 1; // 0110 -> 0000 -> 0000 -> 0
    cout << j << endl;
    return 0;
}
  • Bitwise AND &
  • Bitwise OR |
  • Bitwise XOR ^
  • Bitwise NOT ~
  • Left shift <<
  • Right shift >>

Basic I/O


cout: Output Operator <<

cout: Change the Width of Output

cout.width(5);
cout << 123 << endl;     // --123
cout << 123 << endl;     // effect lasts for one field only
 
cout << setw(5);         // same as cout.width, but requires <iomanip>
cout << 1234567 << endl; // 1234567, prints all if exceeds

cout: Floating-Point Precision and Format

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    // all effects are permanent
 
    // default precision 5 digits after decimal points
    cout << 1.23 << endl;           // 1.23
    cout << 1.230 << endl;          // 1.23
    cout << 1.2345678 << endl;      // 1.23457
    cout << 0.000012345678 << endl; // 1.23457e-05
 
    // fixed precision 6 digits after decimal points
    cout << fixed;
    cout << 1.23 << endl;           // 1.230000
    cout << 1.230 << endl;          // 1.230000
    cout << 1.2345678 << endl;      // 1.234568
    cout << 0.000012345678 << endl; // 0.000012
 
    // scientific precision always uses scientific notation
    cout << scientific;
    cout << 1.23 << endl;           // 1.230000e+00
    cout << 1.230 << endl;          // 1.230000e+00
    cout << 1.2345678 << endl;      // 1.234568e+00
    cout << 0.000012345678 << endl; // 1.234568e-05
 
    // setprecision(n) output n significant digits, requires iomanip
    cout << defaultfloat;
    cout << setprecision(2);
    cout << 1.234 << endl;          // 1.2
    cout << 0.0000001234 << endl;   // 1.2e-07
    // using fixed/scientific with setprecision(n) output 2 digits after decimal point
    cout << fixed;
    cout << 1.234 << endl;          // 1.23
    cout << 0.0000001234 << endl;   // 0.00
    cout << scientific;
    cout << 1.234 << endl;          // 1.23e+00
    cout << 0.0000001234 << endl;   // 1.23e-07
    return 0;
}

cout: Other Manipulators

cin: Input Operator, >>

#include <iostream>
using namespace std;
int main() {
    // Integer
    int x, y;
    cin >> x;               // input 21
    cout << x << endl;      // x=21
    cin >> x >> y;          // input 5 3
    cout << x << y << endl; // x=5, y=3
 
    // Float
    float a, b;
    cin >> a;               // input 14.5
    cout << a << endl;      // a=14.5
 
    // Character
    char c, d;
    cin >> c;               // input Hi
    cout << c << endl;      // c=H
    cin >> c >> d;          // input Hi
    cout << c << d << endl; // c=H, d=i
 
    // String
    char i[20];
    char j[20];
    cin >> i;               // input hello
    cout << i << endl;      // i=hello
    cin >> i >> j;          // input Hello World
    cout << i << j << endl; // i=Hello, j=World
    return 0;
}