Day 01: Teaching myself C++
Introduction:
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 first day of analyzing his scripts.
1. Printing Strings
#include <iostream>
int main(){
std::cout << "Hello World!" << std::endl;
return 0;
}
2. Functions
#include <iostream>
int addNumbers(const int number_1, const int number_2){
int result = number_1 + number_2;
return result;
}
int main(){
// method 1
sum = addNumbers(25,7);
std::cout << "Sum : " << sum << std::endl;
// method 2
std::cout << "Sum : " << addNumbers(3,42) << std::endl;
return 0;
}
3. Getting user input
#include <iostream>
int main(){
std::string name;
int age;
std::cout << "Enter your full name" << std::endl;
std::getline(std::cin >> std::ws,name);
std::cout << "Enter your age" << std::endl;
std::cin >> age;
std::cout << "Hello " << full_name << " you are " << age << " years old!" << std::endl;
return 0;
}
4. Macros
#include <iostream>
#define NUM1 3
#define NUM2 5
int main(){
int sum = NUM1 + NUM2
std::cout << sum << std::endl;
return 0;
}
5. const, constexpr, consteval, constinit
This became a huge topic since I had no idea about constexpr, consteval and constinit. so, I decided to write a seperate blog post explaining about these 4 topics. A detailed explanation about these can be found in the blog post This blog post
6. Number Systems
#include <iostream>
int main(){
int decimal_value = 22; // Decimal
int octal_value = 026; // Octal
int hex_value = 0x16; // Hexadecimal
int binary_value = 0b00010110; // Binary
std::cout << "Decimal : " << decimal_value << std::endl; // Output: 22
std::cout << "Octal : " << octal_value << std::endl; // Output: 22
std::cout << "Hexadecimal : " << hex_value << std::endl; // Output: 22
std::cout << "Binary : " << binary_value << std::endl; // Output: 22
return 0;
}
Thats it for day 1, hope you enjoyed the content.