Introduction To C++ - Day 1
Introduction To C++ - Day 1
Introduction to the C++ programming language, the C++ Standard Library and
modern C++ standards.
}
This simple program has no parameters, and no statements inside the function body. It will serve us as a
blueprint, a placeholder for future C++ code.
int main()
{
std::cout << "Hello World.";
}
Explanation
The #include <iostream> preprocessor directive includes the content of the iostream header into our
source file. The iostream header is part of the C++ Standard Library. We need this header to be able to
use the std::cout object, also known as a standard-output stream.
The << operator inserts our Hello World string literal into the output stream. A string literal is enclosed in
double quotes "". The ; character marks the end of the statement. The std name is the standard-library
namespace and the :: symbol is the scope resolution operator. Object cout is part of the std namespace,
and to access it, we need to prepend the call with the std::.
int main()
{
std::cout << "Some string." << " Another string.";
}
To write to a new line, we need to output a new-line character using the special '\n' literal. Character literals
are enclosed in single quotes.
std::cout << "First line" << '\n' << "Second line.";
Characters can also be part of the string literal.
std::cout << "First line\nSecond line.";
The \ symbol followed by a character represents an escape sequence, a mechanism to output certain
special characters such as the new-line character '\n', single quote character '\'' or a double quote
character '\"'.
int main()
{
char c = 'A';
std::cout << "The value of c is: " << c;
}
Every character is internally represented by an integer number corresponding to a character using the
character set. We can assign both numeric literals (up to a certain number) and character literals to our
char variable:
char c = 'a';
// is the same as if we had
char c = 97;
// the number 97 in the ASCII table corresponds to a character 'a'
int main()
{
int x = 123;
std::cout << "The value of x is: " << x << '\n';
int y = -256;
std::cout << "The value of y is: " << y << '\n';
}
Integer literals can be decimal, octal, and hexadecimal. In production, we mainly use the decimal literals.
int main()
{
int a = 10; // decimal literal
int b = 012; // octal literal
int c = 0xA; // hexadecimal literal
}
int main()
{
double d = 3.14;
std::cout << "The value of d is: " << d;
}
Some of the floating-point literals used for representing double values are:
double x = 213.456;
double y = 1.;
double d = 0.15;
double e = .15;
double f = 3.14e10;
The idiomatic type for representing floating point values in C++ is double. There are other floating-point
types such as float and long double.
int main()
{
unsigned long int x = 4294967295;
std::cout << "The value is: " << x;
}
int main()
{
std::cout << "Please enter a number and press enter: ";
int x = 0;
std::cin >> x;
std::cout << "You entered: " << x;
}
This reads: read from the standard input into our variable x. We can accept multiple values from the
standard input by using multiple >> operators.
int x = 0;
double d = 0.0;
std::cin >> x >> d;
int main()
{
std::cout << "This is a message for the user." << '\n';
char c = 'A';
std::cout << "The value of c is: " << c;
int x = 123;
std::cout << "The value of x is: " << x;
}
To output multiple values, we separate them using multiple stream insertion operators <<.
char c = 'A';
int x = 123;
std::cout << "The values are: " << c << " and " << x;
int main()
{
for (int i = 0; i < 10; i++)
{
std::cout << "The counter is: " << i << '\n';
}
}
int main()
{
int i = 0;
while (i < 10)
{
std::cout << "The value of i is: " << i << '\n';
i++;
}
}
int main()
{
int i = 0;
do
{
std::cout << "The value of i is: " << i << '\n';
i++;
} while (i < 10);
}
int main()
{
std::string s = "Hello World.";
}
We can add a character, a string literal or another string to our string.
std::string s = "Hello ";
char c = 'W';
s += c; // adding a character
s += "or"; // adding a string literal
std::string s2 = "ld.";
s += s2; // adding another string
Accessing individual characters of a string.
std::string s = "Hello World.";
char c1 = s[0]; // 'H'
char c2 = s.at(0); // 'H';
33 Copyright © Slobodan Dmitrovic
Strings
Comparing two strings using the equality operator. Accepting a string using the std::getline function.
#include <iostream> #include <iostream>
#include <string> #include <string>
int main()
{
int* p = new int;
*p = 123;
std::cout << "The pointed-to value is: " << *p;
delete p;
}
This example allocates space for one integer on the free store. Pointer p now points to this newly allocated
memory. We assign a value to our newly allocated integer object by dereferencing a pointer with *p = 123;.
Finally, we free the memory by calling the operator delete.