Refreshing my C++
Aims:
The primary aims of this are:
- Refresh the basic concepts of C++
- Learn from a longer, deeper dive into C++
- Build out a couple of sample applications in C++
Part One: Refreshing the Basics (Mosh Hamedani- C++ for Beginners Tutorial in 1 hour)
I decided to bring myself a little more up-to-date with this introductory tutorial from Mosh to get set up with a modern C++ development platform before going into a much deeper dive.
Notes:
- I am going to start by provisioning a Windows 11 Hyper-V Virtual Machine for this developer environment.
- Installation of Visual Studio Code.
- Build a quick test app - HelloWorld, Done, Success so let’s move on.
Part Two: A Deeper Dive - C++ in One Hour a Day (Sams Teach Yourself)
I had started reading this book about a year ago, and at the time thought that maybe I should look to go through this book in way more detail to get the maximum benefit from its content.
LESSON 1 - Getting Started:
- A Brief History of C++:
- Initially developed by Bjarne Stroustroup back in 1979, originally to be a successor to C.
- Microsoft Visual Studio 2022 supports C++11, C++14 and C++17.
- C++ is considered an intermediate-level programming language as it allows for both high-level programming of applications as well as low-level programming of libraries that work close to the hardware.
- Applications, operating systems, web services, databases and enterprise software are programmed in C++.
- Software engineers often use C++ as the language of choice for research by physicists and mathematicians - although I believe that languages such as Python are probably in greater use in these areas <- this is my belief.
- Programming First C++ Application (HelloWorld):
- Create Hello.cpp
1
2
3
4
5
6
#include <iostream>
int main() {
std::cout << "Hello Martin, Welcome to the world of modern day C++" << std::endl;
return 0;
}
Exercises
- Predict code output.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
int main() {
int x = 8;
int y = 6;
std::cout << std::endl;
std::cout << x - y << " " << x * y << " " << x + y;
std::cout << std::endl;
return 0;
}
Expected output - “2 48 14”
- Actual output: 2 48 14 [success]
- What is the error in this program?
1
2
3
4
5
6
include <iostream>
int main() {
std::cout << "Hello Buggy World \n";
return 0;
}
- Corrected program - was missing the “#” for the include directive.
1
2
3
4
5
6
#include <iostream>
int main() {
std::cout << "Hello Buggy World \n";
return 0;
}
LESSON 2 - The Anatomy of a C++ Program:
Notes:
- #include - Preprocessor Directive
- Runs prior to compilation
- Tells the Processor to insert the contents of a file at this point in the file
- main() body of the program
- Execution of a C++ program always starts here
- It is convention that the function main() is declared with an int return value
- Some C++ applications may use a variant of the main() function like this:
1
int main(int argc, char* argv[])
- In this instance argc would contain the number of arguments and argv[] would be an array of strings containing the actual arguments(?)
- code that does the actual work in this application
1
std::cout << "Hello Martin, Welcome to the world of modern day C++" << std::endl;
- cout (“console-out” - pronounced see-out)
- cout is a stream defined by the standard std namespace
- In our application we are putting the string literal “Hello Martin, Welcome to the world of C++” into this stream by using stream insertion operator «
- endl is used to end a line, akin to inserting a carriage return “\n”
- In C++ functions need to return a value unless explicitly specified otherwise
- main is a function too, and always returns an integer value
- This value is returned back to the operating system (O/S)
- It is convention that a value of 0 would indicate success and that a value if -1 would indicate an error has occured
- The follwing would be an example of a function that returns no value
1 2 3
void MyFunction() { ...doSomething... }
- The Concept of Namespaces
- Namespaces are names give to parts of code that help in reducing the potential for naming conflict
- By using std::cout we are telling the compiler to use that one unique cout that is provided in the std namespace, for example
1 2 3 4 5 6 7 8 9
#include <iostream> int main() { using namespace std; cout << "Hello Martin, Welcome to the world of modern day C++" << endl; return 0; }
- Functions in C++
- Functions enable us to divide the content of the application into functional units that can be invoked in a sequence of our choosing.
- A function, when invoked, typically returns a value to the invoking/calling function.
Part Three: Sample C++ Applications
The goal of this part is to actually build out a few simple C++ applications to practice what I am learning on my C++ journey.
- C++ Application: Tic-Tac-Toe [DONE]
- C++ Application: Basic Inventory Management System
- C++ Application: Personal Expense Tracker
As always, happy to hear your thoughts…
Modified By: Martin Thompson
Last Modified: September 9, 2024-12:00:00
This post is licensed under CC BY 4.0 by the author.