This content originally appeared on DEV Community and was authored by Robbie Tambunting
Why C++?
I'm on my journey to pursuing my Master's in CS! Along with taking the GRE and obtaining letters of rec, I also must take a handful of prerequisites that my Computational Mathematics major didn't include.
Today marked the start of my C++ Programming course. So why are we learning C++ instead of let’s say Python or Java? While this question wasn’t answered in my first lecture, I would like to highlight a few points:
- C++ is more similar to C and requires manual memory management.
- C++ supports Object Oriented Programming (OOP)
Since both Python and Java support OOP, I’d imagine that the main selling point is C++’s low-level programming capabilities. Higher-level languages abstract memory management, so C++ forces us to efficiently manage it ourselves.
Side-note: It’s really convenient that most C code can also be run as a C++ program.
Now back to my class activities.
Components of a C++ Program
We took a look at a particular code segment and were introduced to the basic components of a C++ program. Afterward, we were asked to make a few edits for an in-class assignment.
The following code snippet is as follows
#include <iostream>
using namespace std;
int main() {
int totalInches;
int totalFeet;
int totalMiles;
int remainingFeet;
int remainingInches;
cout << "Please enter the total number of inches: ";
cin >> totalInches;
totalFeet = totalInches / 12;
totalMiles = totalFeet / 5280;
remainingFeet = totalFeet % 5280;
remainingInches = totalInches % 12;
cout << totalInches << " inch(es) is equal to "
<< totalMiles << "mile(s),"
<< remainingFeet << " feet and "
<< remainingInches << " inch(es)."
<< endl << endl;
cout << "Bye Now!" << endl;
return 0;
}
Let’s break down each unfamiliar keyword and operator.
General
- #include - a directive that tells the compiler to include contents of a specified file. The preprocessor replaces the line with #includes with the actual code located in the header file
- header file - any file that contains C-language functions. May be included in other existing C files
- namespace - an area that defines the scope of all things (functions + variables) inside it. It’s much like adding curly braces {} around a whole region of code and calling it a name.
-
int main() - defines our main program that all other functions are called from
- Standard practice is to specify an int return value and to return 0 on finish
IoStream + std
-
std - namespace for the C++ standard library that holds many useful classes and functions
- cout - used to print characters to the console. stands for “character output”. part of std
- cin - used to get input from the user. stands for “character input”. part of std
- endl - used to print a newline “\n” to the console and flushes the output buffer (AKA all data is written to the output device in that instant)
- “using namespace std;” - allows us to define things like cout without having to write the full std::cout statement
-
iostream - a file that includes functions that allow for input and output operations using streams (streams - flow of data in or out of a program)
- printing to the console
- printing an error message
- << operator - used for outputting data onto the stream. Usually used to push data into the cout output stream
- >> operator - used for getting input data from the stream. Usually used to pull user inputted data from the stream into a variable via cin
And that’s all for the first day! I’m looking forward to furthering my understanding of computer programs as this course progresses
This content originally appeared on DEV Community and was authored by Robbie Tambunting
Robbie Tambunting | Sciencx (2024-08-20T04:40:29+00:00) C++ Programming Lecture 1. Retrieved from https://www.scien.cx/2024/08/20/c-programming-lecture-1/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.