Day 02: Teaching myself C++
NOTE:
I’m a self-taught programmer, who recently started a new adventure to learn C++ by reading a ton of other people’s code and language documentations. This method might not work for all the people. Beacause it requires a certain degree of experience in a low-level or a mid-level programming language like C in order to read and learn a language like C++ from other people’s code and language documentations. For me this was possible because, I had some experience using C which I learned for the course CS50x. First, I started with the scripts that Daniel Gakwaya, had released in his github repository The C 20 Masterclass source code. I will compose this blog post to demonstrate the lessons I learnt on my second day of analyzing his scripts.
1. Type of Initializers
1.1 Braced Initializers
//Braced initializers
//Variable may contain random garbage value . WARNING
int elephant_count;
int lion_count{};//Initializes to zero
int dog_count {10}; //Initializes to 10
int cat_count {15}; //Initializes to 15
//Can use expression as initializer
int domesticated_animals { dog_count + cat_count };
std::cout << "Elephant count : " << elephant_count << std::endl;
std::cout << "Lion count : " << lion_count << std::endl;
std::cout << "Dog count : " << dog_count << std::endl;
std::cout << "Cat count : " << cat_count << std::endl;
std::cout << "Domesticated animal count : " << domesticated_animals << std::endl;
1.2 Functional Initialization
//Functional Initialization
int apple_count(5);
int orange_count(10);
int fruit_count (apple_count + orange_count);
//int bad_initialization ( doesnt_exist3 + doesnt_exist4 );
//Information lost. less safe than braced initializers
int narrowing_conversion_functional (2.9);
std::cout << "Apple count : " << apple_count << std::endl;
std::cout << "Orange count : " << orange_count << std::endl;
std::cout << "Fruit count : " << fruit_count << std::endl;
std::cout << "Narrowing conversion : " << narrowing_conversion_functional << std::endl;//Will loose info
2. Types of Integers
- signed : Signed variables can store negative, zero, and positive numbers.
range of possible values : [-2147483648 to 2147483647]
- unsigned : Unsigned variables can store the number zero and positive numbers
range of possible values : [0 to 4294967295]
Types of integers:
- short int
- int
- long int
- long long int
Type | Size (in bytes) | Range |
---|---|---|
signed short int | 2 | -32,768 to 32,767 |
unsigned short int | 2 | 0 to 65,535 |
signed int | 4 | [-2147483648 to 2147483647] |
unsigned int | 4 | [0 to 4294967295] |
signed long int | 8 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 8 | 0 to 4,294,967,295 |
signed long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
3. Types of fractional Numbers
Types of fractional numbers:
- float
- double
- long double
Type | Size (in bytes) | Range | Digits of precision |
---|---|---|---|
float | 4 | 3.4 x 10-38 to 3.4 x 10+38 | 7 |
double | 8 | 1.7×10-308 to 1.7×10+308 | 15 |
long double | 16 | 1.7×10-308 to 1.7×10+308 | 18 |
4. If Statement
#include <iostream>
int main(){
bool red_light {false};
bool green_light{true};
if(red_light == true){
std::cout << "Stop!" << std::endl;
}else{
std::cout << "Go through!" << std::endl;
}
if(green_light){
std::cout << "The light is green!" << std::endl;
}else{
std::cout << "The light is NOT green!" << std::endl;
}
return 0;
}
5. Type Casting
#include <iostream>
int main(){
char value = 65 ; // ASCII character code for 'A'
std::cout << "value : " << value << std::endl; // A
std::cout << "value(int) : " << static_cast<int>(value) << std::endl;
return 0;
}
6. Integer modifier suffixes
auto var6 { 123u}; // unsigned
auto var7 { 123ul}; //unsigned long
auto var8 { 123ll}; // long long
Thats it for day 2, hope you enjoyed the content.