Basics of C++

So after going over chapter 0 and 1 from LearnCPP.com (https://www.learncpp.com/) (if you haven't been there, just visit and soak yourself in beautiful knowledge hub) and learning for few weeks, I made a small code for myself which will cover pretty much abstract of it. I hope this can be useful to learn things quickly and precise manner. 

I will still recommned to go in deep since I won't be covering how to setup the compiler and there's alot to learn prior even running this code. 

For this code I will use Visual Studio Community Edition 2022 (https://visualstudio.microsoft.com/). It's pretty much free for students, open-source and  individual developers.

Snapshot of Visual Studio Community Edition (2022)


So here we go, our little jurney with basics of C++. Kindly go through each comment section for details:

#include <iostream>

int main() {					// main() is always required to return int
	std::cout << "Hello World! :D" << '\n';		// Normal string with escape character, 'cout' = character output, '<<' insertion operator
	
	int a = 3, b = 5;	// Copy initialization
	int x{ 23 };		// Direct list initialization
	int y(11);		// Direct initialization
	int z{};		// Value initialization
	z =567,

	std::cout << "Lets print the value of Z: " << z << '\n';	// '<'< can be used multiple times to output string
	
	[[maybe_unused]] double pi{ 3.14 };		// Used when we don't want to call the variable afterwards, supresses "local variable is initialized but not referenced" in compiler

	std::cout << "This is the way to output with one line after" << std::endl; // endl = End line; Using std::endl is often inefficient, as it actually does two jobs: it outputs a newline (moving the cursor to the next line of the console), and it flushes the buffer (which is slow). If we output multiple lines of text ending with std::endl, we will get multiple flushes, which is slow and probably unnecessary.

	std::cout << "So always prefer backward slash n than std::endl, its modern take and saves buffer \n";

	std::cout << "Now lets try entering a number: ";
	int v{};
	std::cin >> v;		// std:cin = character input, used for inputing, notice the operator >> 
	std::cout << "You entered: " << v << '\n';

	int m{}; 
	int n{};
	std::cout << "Lets try entering two numbers seperated by space: ";
	std::cin >> m >> n;		// You can enter multiple values at the same time
	std::cout << "You entered: " << m << " and " << n << '\n';
        return 0;		// The return value from main() is sometimes called a status code (or less commonly, an exit code, or rarely a return code). The status code is used to signal whether your program was successful or not. By convention, a status code of 0 means the program ran normally (meaning the program executed and behaved as expected).

}

No comments:

Post a Comment