Overview
Teaching: 5 min
Exercises: 5 minQuestions
How do you create a C++ program?
Objectives
Create, compile, and run your first C++ program
Here we will create our first C++ program and dissect its components and in doing so will introduce some important concepts that we will dive into in more detail as we go.
$ nano hello.cpp
#include <iostream>
int main(){
std::cout<<"Hello, world!\n";
}
Use the GNU C++ compiler g++ from the GNU Compiler Collection (GCC) to build it:
$ g++ hello.cpp -o hello
$ ./hello
Hello, world!
cout is an object, an object can be thought of as a particular instantiation of a class. int a; a is an instantiation of an int. In this example int would be considered the class and a would be the object. Though in this case int is a basic type and not actually a class but the relationship between int and a in the example is exactly the same as the relationship between an class and an object.cout is of type ostream, ostream is a class(see: https://www.cplusplus.com/reference/iostream/cout/)#include <iostream> includes a file that defines the ostream class and the cout objectstd is the “standard” namespace which contains the C++ standard library https://www.cplusplus.com/reference/std::list versus some custom made list class<< is an operator. Like +, -, etc.<< operator will shift all the bits of a basic type (e.g. int, char etc.) to the left a given amount.Nano line numbers
When compiling programs a compiler usually reports errors it encounters by line number so it is particularly important to be able to find a particular line in your editor.
There are a couple ways that nano can display line number/column information. Which methods are available will depend on your version of Nano.
While in nano press
alt+cto display line number and column information for the line the cursor is currently on. This works for this particular nano session and on the version of nano on our training cluster.Line numbers can be toggled on/off using
alt+shift+#. This is nice, but only works on newer versions of nano.When starting nano the
-loption can be used to tell nano to display line numbers along the left hand side. Again only works with newer versions of nano.Tell nano to always display line numbers and current line information:
$ nano ~/.nanorcand enter the following two lines and save and exit
set linenumbers set constantshowNote that
set linenumberswill only work on newer versions of nano whileset constantshowwill work on older versions of nano also.
Useful Nano shortcuts
Here are some shortcuts that are helpful while editing text in nano
alt+a: mark or select text starting from where the cursor is when these keys are pressed to where you move the cursor. Then selected text can be cut, or pasted over.
ctl+k: cut text
ctl+u: uncut (e.g. paste) previously cut text either from nano or elsewhere
alt+u: undo last changes
What is a class?
Is a class:
a variable which can have multiple members like a struct
an object which can have multiple members like a struct
a datatype which can have multiple members like a struct
a datatype which can contain only a single value like
int,float,etc.Solution
NO: while a class can have multiple members like a struct, a class defines a new datatype where as a variable or object are the instantiation of that class or datatype.
NO: while an object has a class type, the object is not the class its self.
Yes: a class is a kind of datatype that can have multiple members.
No: classes can have multiple members, it is possible that they only have one member but they are not restricted to holding a single value.
Key Points
g++can be used to compiler and link C++ programs.Program execution starts in the
mainfunction.
std::coutallows you to write strings to the terminal using the<<operator.A class is a datatype.
An object is a particular instance of that datatype or class.
Namespaces group names and help to avoid name clashes.