Lec 01

<aside> 💡 Import and Include import ← from module.(feature of c++20 - 2020) include ← from something not module

Import: you have to compile the module using. (ex. IO stream)

Include: Automatically compile for you

</aside>

Hello World in C↓ Hello World in C++20 →

#include <stdio.h>
	void main(){
		printf("Hellow World\\n");
}
import<ioStream>; // <- C++20 modules
int main(){
	std::Cout<<"Hellow World"<<std::endl; // <- USing namespace std;
	return 0;
}

<aside> 💡 Explanation

<aside> 💡 Compilation

Not using C++20 modules:

#include <iostream>
	g++20i main.cc -o program

<aside> 💡 g++20h g++20m g++20i

</aside>

Input/Output

Example: Read two ints, add them, print back

import <iostream>;
USing nameSpce std;
	int main () {
		int x, y;
		Cin >> x >> y;
		Cout << x + y << endl;
}

Operators:

Arrows point indirection of infomation flow

Cin ignores all whitespace by defalut

Cin >> x - "Get from"
Cout << y - "put to" operator

<aside> 💡 How could this go wrong?

  1. User gives us non-integer input
  2. User supplies EoF (via Ctrl - D)
  3. What if they are too big? </aside>

Example: Read integers from Stdin until failure

int main() {
	int i;
	while(true) {
	cin >> i;
	if (cin.fail()) break;
	cout << i << endl;
	}
}

One improvement: Implicit conversion from Cin to bool

V2:

int main() {
	int i;
	whlie(true) {
		Cin >> i;
		if(!Cin) break;
		Cout << i << endl;
	}
}

In Cin >> and << are bitshift operators

int x = 21;

printf(”%d\n”, x >> 3); // ← shifts x’s bits to the right by 3

21 = 10101 → 2^0 + 2^2 + 2^4 = 21

21 >> 3 = 10_2 = 2

In C++, dependend on context LHS; int - bit shift

LHS: stream - printing/ reading input

This is an example of overloading - >>, << operators differ depending on context This applies to opetators and fns

With int i 21 >> 3 - returns an int What about Cin >> x, Cout << y?

Example: Cout << “Hello” << “World” << endl;

→ returns Cout & simplifies to Cout << “World” << endl;

Cin >> i;
	if (!Cin) break;
-> if (!(Cin >> 0)) break;