Day 03: 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 third day of analyzing his scripts.
1. Switch Statements
#include <iostream>
#include <string>
// Tools
const int Pen{ 10 };
const int Marker{ 20 };
const int Eraser{ 30 };
const int Rectangle{ 40 };
const int Circle{ 50 };
const int Ellipse{ 60 };
int main(){
int tool {Eraser};
switch (tool)
{
case Pen : {
std::cout << "Active tool is Pen" << std::endl;
}
break;
case Marker : {
std::cout << "Active tool is Marker" << std::endl;
}
break;
case Eraser :
case Rectangle :
case Circle : {
std::cout << "Drawing Shapes" << std::endl;
}
break;
case Ellipse : {
std::cout << "Active tool is Ellipse" << std::endl;
}
break;
default: {
std::cout << "No match found" << std::endl;
}
break;
}
return 0;
}
2. Math Functions
should include the
#include <cmath>
2.1 Ceil() & floor()
std::cout << "Weight rounded to floor is : " << std::floor(7.7) << std::endl; //7
std::cout << "Weight rounded to ceil is : " << std::ceil(7.7) << std::endl; //8
2.2 abs()
- returns the absolute value of an argument
std::cout << "Abs of weight is : " << std::abs(7.7) << std::endl; //7.7
std::cout << "Abs of savings is : " << std::abs(-5000) << std::endl; //5000
2.3 exp() (Exponential)
double exponential = std::exp(10);
std::cout << "The exponential of 10 is : " << exponential << std::endl;
2.4 pow() (Power)
std::cout << "3 ^ 4 is : " << std::pow(3,4) << std::endl;
std::cout << "9 ^ 3 is : " << std::pow(9,3) << std::endl;
2.5 sqrt() (Square Root)
std::cout << "The square root of 81 is : " << std::sqrt(81) << std::endl;
2.6 round()
std::cout << "3.654 rounded to : " << std::round(3.654) << std::endl; // 4
std::cout << "2.5 is rounded to : " << std::round(2.5) << std::endl; // 3
std::cout << "2.4 is rounded to : " << std::round(2.4) << std::endl; // 2
3. Output Formatting
3.1 std::endl
std::endl : places a new line character on the output stream. This is identical to placing ‘\n’ on the output stream.
std::cout << "Hello World!" << std::endl;
3.2 std::flush
std::flush : flushes the output buffer to its final destination.
std::cout << "This is a nice message...." << std::endl << std::flush;
//After this std::flush, we're sure that at this line, the message has been sent
//to the stream. This may be important in some applications.
3.3 std::setw() (set width)
std::setw() : Adjusts the field with for the item about to be printed.
- The setw() manipulator only affects the next value to be printed.
//unformatted table
std::cout << "Unformatted table : " << std::endl;
std::cout << "Daniel" << " " << "Gray" << " 25" << std::endl;
std::cout << "Stanley" <<" " << "Woods" << " 33" << std::endl;
std::cout << "Jordan" << " " << "Parker" << " 45" << std::endl;
std::cout << "Joe" << " " << "Ball" << " 21" << std::endl;
std::cout << "Josh" << " " << "Carr" << " 27" << std::endl;
std::cout << "Izaiah" << " " << "Robinson" << " 29" << std::endl;
//formatted table
int col_width{14};
std::cout << std::setw(col_width) << "Lastname" << std::setw(col_width) << "Firstname" << std::setw(col_width/2) << "Age" << std::endl;
std::cout << std::setw(col_width) << "Daniel" << std::setw(col_width) << "Gray" << std::setw(col_width/2) << "25" << std::endl;
std::cout << std::setw(col_width) << "Stanley" << std::setw(col_width) << "Woods" << std::setw(col_width/2) << "33" << std::endl;
std::cout << std::setw(col_width) << "Jordan" << std::setw(col_width) << "Parker" << std::setw(col_width/2) << "45" << std::endl;
std::cout << std::setw(col_width) << "Joe" << std::setw(col_width) << "Ball" << std::setw(col_width/2) << "21" << std::endl;
std::cout << std::setw(col_width) << "Josh" << std::setw(col_width) << "Carr" << std::setw(col_width/2) <<"27" << std::endl;
std::cout << std::setw(col_width) << "Izaiah" << std::setw(col_width) << "Robinson" << std::setw(col_width/2) << "29" << std::endl;
3.4 justify
Justify : Values can be justified in their fields. There are three manipulators for adjusting the justification: left, right, and internal.
3.4.1 right justified
col_width = 20;
std::cout << std::right;
std::cout << std::setw(col_width) << "Lastname" << std::setw(col_width) << "Firstname" << std::setw(col_width/2) << "Age" << std::endl;
std::cout << std::setw(col_width) << "Daniel" << std::setw(col_width) << "Gray" << std::setw(col_width/2) << "25" << std::endl;
std::cout << std::setw(col_width) << "Stanley" << std::setw(col_width) << "Woods" << std::setw(col_width/2) << "33" << std::endl;
std::cout << std::setw(col_width) << "Jordan" << std::setw(col_width) << "Parker" << std::setw(col_width/2) << "45" << std::endl;
std::cout << std::setw(col_width) << "Joe" << std::setw(col_width) << "Ball" << std::setw(col_width/2) << "21" << std::endl;
std::cout << std::setw(col_width) << "Josh" << std::setw(col_width) << "Carr" << std::setw(col_width/2) <<"27" << std::endl;
std::cout << std::setw(col_width) << "Izaiah" << std::setw(col_width) << "Robinson" << std::setw(col_width/2) << "29" << std::endl;
3.4.2 left justified
col_width = 20;
std::cout << std::left;
std::cout << std::setw(col_width) << "Lastname" << std::setw(col_width) << "Firstname" << std::setw(col_width/2) << "Age" << std::endl;
std::cout << std::setw(col_width) << "Daniel" << std::setw(col_width) << "Gray" << std::setw(col_width/2) << "25" << std::endl;
std::cout << std::setw(col_width) << "Stanley" << std::setw(col_width) << "Woods" << std::setw(col_width/2) << "33" << std::endl;
std::cout << std::setw(col_width) << "Jordan" << std::setw(col_width) << "Parker" << std::setw(col_width/2) << "45" << std::endl;
std::cout << std::setw(col_width) << "Joe" << std::setw(col_width) << "Ball" << std::setw(col_width/2) << "21" << std::endl;
std::cout << std::setw(col_width) << "Josh" << std::setw(col_width) << "Carr" << std::setw(col_width/2) <<"27" << std::endl;
std::cout << std::setw(col_width) << "Izaiah" << std::setw(col_width) << "Robinson" << std::setw(col_width/2) << "29" << std::endl;
3.4.3 internal justified
//Internal justified : sign is left justified , data is right justified
std::cout << std::endl;
std::cout << "Internal justified : " << std::endl;
std::cout << std::right;
std::cout << std::setw(10) << -123.45 << std::endl;
std::cout << std::internal;
std::cout << std::setw(10) << -123.45 << std::endl;
3.5 setfill
std::cout << std::endl;
std::cout << "Table with fill characters : " << std::endl;
col_width = 20;
std::cout << std::left;
std::cout << std::setfill('*'); // The fill character
std::cout << std::setw(col_width) << "Lastname" << std::setw(col_width) << "Firstname" << std::setw(col_width/2) << "Age" << std::endl;
std::cout << std::setw(col_width) << "Daniel" << std::setw(col_width) << "Gray" << std::setw(col_width/2) << "25" << std::endl;
std::cout << std::setw(col_width) << "Stanley" << std::setw(col_width) << "Woods" << std::setw(col_width/2) << "33" << std::endl;
std::cout << std::setw(col_width) << "Jordan" << std::setw(col_width) << "Parker" << std::setw(col_width/2) << "45" << std::endl;
std::cout << std::setw(col_width) << "Joe" << std::setw(col_width) << "Ball" << std::setw(col_width/2) << "21" << std::endl;
std::cout << std::setw(col_width) << "Josh" << std::setw(col_width) << "Carr" << std::setw(col_width/2) <<"27" << std::endl;
std::cout << std::setw(col_width) << "Izaiah" << std::setw(col_width) << "Robinson" << std::setw(col_width/2) << "29" << std::endl;
Table with fill characters :
Lastname************Firstname***********Age*******
Daniel**************Gray****************25********
Stanley*************Woods***************33********
Jordan**************Parker**************45********
Joe*****************Ball****************21********
Josh****************Carr****************27********
Izaiah**************Robinson************29********
3.6 boolalpha and noboolapha
control bool output format : 1/0 or true/false
- noboolalpha : 0/1
- boolalpha : True/False
bool condition {true};
bool other_condition {false};
std::cout << "condition : " << condition << std::endl;
std::cout << "other_condition : " << other_condition << std::endl;
std::cout << std::endl;
std::cout << std::boolalpha;
std::cout << "condition : " << condition << std::endl;
std::cout << "other_condition : " << other_condition << std::endl;
std::cout << std::endl;
std::cout << std::noboolalpha;
std::cout << "condition : " << condition << std::endl;
std::cout << "other_condition : " << other_condition << std::endl;
4. Arrays
4.1 Declaration
int array_name[9]
char message4 [] {"Hello"};
char message [6] {"Hello"}; // 6 with the '\n'
4.2 Declare and initialize at the same time
// omit the size declaration
int class_sizes[] {10,12,15,11,18,17,23,56};
double salaries[5] {12.7, 7.5, 13.2, 8.1, 9.3};
4.3 Reading
std:: cout << " scores [0] : " << scores[0] << std::endl;
std:: cout << " scores [1] : " << scores[1] << std::endl;
4.4 Read Only Arrays
const int birds[] {10,12,15,11,18,17,23,56};
4.5 size of
int scores [] {1,2,5};
std::cout << "sizeof(scores) : " << sizeof(scores) << std::endl; // 12
std::cout << "sizeof(scores[0]) : " << sizeof(scores[0]) << std::endl; // 4
5. Multi-Dimensional Arrays
5.1 2D Arrays
#include <iostream>
int main(){
int packages [] [4] {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{3,4,5,6}
};
//Read data from a 2D array
for(size_t i{0} ; i < 3; ++ i){
for(size_t j{0}; j < 4 ; ++j){
std::cout << packages[i][j] << " ";
}
std::cout << std::endl;
}
//Use std::size to query the size of array dimensions
for(size_t i{0} ; i < std::size(packages); ++ i){
for(size_t j{0}; j < std::size(packages[i]) ; ++j){
std::cout << packages[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
5.2 3D Arrays
#include <iostream>
int main(){
//3D arrays are defined in the same way. We just use three sets of indexes
// 3 lights per room, 5 rooms per house 7 houses per block
int house_block [7] [5] [3] {
{
{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}
},
{
{16,17,18},{19,20,21},{22,23,24},{25,26,27},{28,29,30}
},
{
{ 31,32,33},{34,35,36},{37,38,39},{40,41,42},{43,44,45}
},
{
{46,47,48},{49,50,51},{52,53,54},{55,56,57},{58,59,60}
},
{
{61,62,63},{64,65,66},{67,68,69},{70,71,72},{73,74,75}
},
{
{76,77,78},{79,80,81},{82,83,84},{85,86,87},{88,89,90}
},
{
{91,92,93},{94,95,96},{97,98,99},{100,101,102},{103,104,105}
}
};
for(size_t i{0}; i < std::size(house_block) ; ++i){
for(size_t j{0} ; j < std::size(house_block[i]) ; ++j){
for(size_t k{0} ; k < std::size(house_block[i][j]) ; ++k){
std::cout << house_block[i][j][k] << " ";
}
}
}
//For 3d and really any multi dimensional array , you have to specify
//the number of elements in []'s , only the left most is not mandatory.
//Below is the example for 3D reproduced.Omitting the 5 or the 3 or both
//will cause a compile error.
int house_block1 [] [5] [3] {
{
{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}
},
{
{16,17,18},{19,20,21},{22,23,24},{25,26,27},{28,29,30}
},
{
{ 31,32,33},{34,35,36},{37,38,39},{40,41,42},{43,44,45}
},
{
{46,47,48},{49,50,51},{52,53,54},{55,56,57},{58,59,60}
}
};
return 0;
}
5.3 Multi-Dimensional Arrays with characters
#include <iostream>
int main(){
const size_t name_length{15};
char members [][name_length] {
{'J','o','h','n'},
{'S','a','m','u','e','l',},
{'R','a','s','h','i','d'},
{'R','o','d','r','i','g','e','z'}
};
//Better : Using C-string litterals
//Compared to initialization with charactes with in '', this
// is even easier to type. The entire string is a single entity
//you can manage easily.
char members1 [][name_length] {
"John",
"Samuel",
"Rashid",
"Rodriguez"
};
//Printing out members1
std::cout << "Printing out members1 (C-string literals) : " << std::endl;
for (size_t i {0}; i < std::size(members1) ; ++i){
std::cout << members1[i] << std::endl;
}
return 0;
}
6. Generating Random Numbers
#include <iostream>
#include <ctime>
#include <cstdlib>
int main(){
std::srand(std::time(0)); // Seed
int random_num = std::rand();
std::cout << "random_num : " << random_num << std::endl; // 0 ~ RAND_MAX
// Generate random numbers in a loop
int random_num;
for(size_t i {0} ; i < 20 ; ++i){
random_num = std::rand();
std::cout << "random_num " << i << ":" << random_num << std::endl; // 0 ~ RAND_MAX
}
// Generate a range between 0 and 10
int random_num = std::rand() % 11; // [ 0 ~10]
for(size_t i {0} ; i < 20 ; ++i){
random_num = std::rand() % 11;
std::cout << "random_num " << i << " : " << random_num << std::endl; // 0 ~ RAND_MAX
}
// Generate a range between 1 and 10
int random_num = std::rand() % 10 + 1 ; // [1~10]
for(size_t i {0} ; i < 20 ; ++i){
random_num = std::rand() % 10 + 1;
std::cout << "random_num " << i << " : " << random_num << std::endl; // 0 ~ RAND_MAX
}
return 0;
}
7. For Loops
for( unsigned int i{0} ; i < 10000 ;++i){
//Whatever we want the loop to run
}
7.1 Multiple declaration
for (size_t i{0} , x {5}, y{22} ; y > 15 ; ++i , x+=5 , y-=1){
std::cout << "i: " << i << ", x : " << x << ", y : " << y << std::endl;
}
7.2 Range Based For loops
int bag_of_values [] {1,2,3,4,5,6,7,8,9,10};
for (int value : bag_of_values){
std::cout << " value : " << value << std::endl;
}
for (int value : {1,2,3,4,5,6,7,8,9,10}){
std::cout << " value : " << value << std::endl;
}
8. While Loops
#include <iostream>
int main(){
const size_t COUNT{100};
size_t i{0}; // Iterator declaration
while(i < COUNT ){ // Test
std::cout << i << " : I love C++" << std::endl;
++i; // Incrementation
}
return 0;
}
8.1 Do While Loops
#include <iostream>
int main(){
const int COUNT{0};
size_t i{0}; // Iterator declaration
do{
std::cout << i << " : I love C++" << std::endl;
++i; // Incrementation
}while( i < COUNT);
return 0;
}
9. Break & Continue Loops
const size_t COUNT{20};
for(size_t i{0} ; i < COUNT ; ++i ){
if(i==5)
continue;
if(i == 11)
break; // Breaks out of the loop
std::cout << "i : " << i << std::endl;
}
10. Pointers
10.1 Declaring
//Declare and initialize pointer
int* p_number {}; // Will initialize with nullptr
double* p_fractional_number{};
//Explicitly initialize with nullptr
int* p_number1{nullptr};
int* p_fractional_number1{nullptr};
//It doesn't matter if you put the * close to data type or to varible name
int* p_number2{nullptr};
int * p_number3{nullptr};
int *p_number4{nullptr};
10.2 Assigning
int int_var {43};
int *p_int{&int_var};// The address of operator (&);
//Can't cross assign between pointers of different types
int *p_int1{nullptr};
double double_var{33}; // Compile Error
11. File Reader
#include <iostream>
#include <fstream>
#include <string>
int main (){
std::ifstream myfile;
myfile.open("demo.txt");
std::string myline;
if ( myfile.is_open() ) {
while ( myfile ) { // equivalent to myfile.good()
std::getline (myfile, myline);
std::cout << myline << '\n';
}
}
else {
std::cout << "Couldn't open file\n";
}
return 0;
}
Thats it for day 3, hope you enjoyed the content.