Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards 1st Edition Slobodan Dmitrović instant download
Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards 1st Edition Slobodan Dmitrović instant download
https://textbookfull.com/product/modern-c-for-absolute-beginners-
a-friendly-introduction-to-c-programming-language-
and-c11-to-c20-standards-1st-edition-slobodan-dmitrovic/
https://textbookfull.com/product/modern-c-for-absolute-beginners-
a-friendly-introduction-to-the-c-programming-language-slobodan-
dmitrovic/
https://textbookfull.com/product/modern-c-for-absolute-beginners-
a-friendly-introduction-to-the-c-programming-language-dmitrovic-
slobodan/
https://textbookfull.com/product/modern-c-for-absolute-beginners-
a-friendly-introduction-to-the-c-programming-language-2nd-
edition-slobodan-dmitrovic/
Handbook of Macroeconomics, Volume 2A-2B SET 1st
Edition John B. Taylor
https://textbookfull.com/product/handbook-of-macroeconomics-
volume-2a-2b-set-1st-edition-john-b-taylor/
https://textbookfull.com/product/modern-c-for-absolute-
beginners-1st-edition-slobodan-dmitrovic/
https://textbookfull.com/product/modern-c-for-absolute-
beginners-1st-edition-slobodan-dmitrovic-dmitrovic/
https://textbookfull.com/product/c-programming-for-absolute-
beginners-radek-vystavel/
https://textbookfull.com/product/modern-c-for-absolute-beginners-
second-edition-solbodan-dmitrovic/
Slobodan Dmitrović
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
1. Introduction
Slobodan Dmitrović 1
(1) Belgrade, Serbia
Dear Reader,
Congratulations on choosing to learn the C++ programming
language, and thank you for picking up this book. My name is Slobodan
Dmitrović, I am a software developer and a technical writer, and I will
try to introduce you to a beautiful world of C++ to the best of my
abilities.
This book is an effort to introduce the reader to a C++ programming
language in a structured, straightforward, and friendly manner. We will
use the “just enough theory and plenty of examples” approach
whenever possible.
To me, C++ is a wonderful product of the human intellect. Over the
years, I have certainly come to think of it as a thing of beauty and
elegance. C++ is a language like no other, surprising in its complexity,
yet wonderfully sleek and elegant in so many ways. It is also a language
that cannot be learned by guessing, one that is easy to get wrong and
challenging to get right.
In this book, we will get familiar with the language basics first.
Then, we will move onto standard-library. Once we got these covered,
we will describe the modern C++ standards in more detail.
After each section, there are source code exercises to help us adopt
the learned material more efficiently. Let us get started!
© Slobodan Dmitrović 2020
S. Dmitrović, Modern C++ for Absolute Beginners
https://doi.org/10.1007/978-1-4842-6047-0_2
2. What is C++?
Slobodan Dmitrović 1
(1) Belgrade, Serbia
3. C++ Compilers
Slobodan Dmitrović 1
(1) Belgrade, Serbia
C++ programs are usually a collection of C++ code spread across one or
multiple source files. The C++ compiler compiles these files and turns
them into object files. Object files are linked together by a linker to
create an executable file or a library. At the time of the writing, some of
the more popular C++ compilers are:
– The g++ frontend (as part of the GCC)
– Visual C++ (as part of the Visual Studio IDE)
– Clang (as part of the LLVM)
3.1.1 On Linux
To install a C++ compiler on Linux , type the following inside the
terminal:
g++ source.cpp
This command will produce an executable with the default name of
a.out. To run the executable file, type:
./a.out
The same rules apply to the Clang compiler. Substitute g++ with
clang++.
3.1.2 On Windows
On Windows , we can install a free copy of Visual Studio.
Choose Create a new project, make sure the C++ language option is
selected, and choose - Empty Project – click Next and click Create. Go to
the Solution Explorer panel, right-click on the project name, choose
Add – New Item – C++ File (.cpp), type the name of a file (source.cpp),
and click Add. Press F5 to run the program.
We can also do the following: choose Create a new project, make
sure the C++ language option is selected, and choose – Console App –
click Next and click Create.
If a Create a new project button is not visible, choose File – New –
Project and repeat the remaining steps.
© Slobodan Dmitrović 2020
S. Dmitrović, Modern C++ for Absolute Beginners
https://doi.org/10.1007/978-1-4842-6047-0_4
Let us create a blank text file using the text editor or C++ IDE of our
choice and name it source.cpp. First, let us create an empty C++
program that does nothing. The content of the source.cpp file is:
int main(){}
The function main is the main program entry point, the start of our
program. When we run our executable, the code inside the main
function body gets executed. A function is of type int (and returns a
result to the system, but let us not worry about that just yet). The
reserved name main is a function name. It is followed by a list of
parameters inside the parentheses () followed by a function body
marked with braces {}. Braces marking the beginning and the end of a
function body can also be on separate lines:
int main()
{
4.1 Comments
Single line comments in C++ start with double slashes // and the
compiler ignores them. We use them to comment or document the code
or use them as notes:
int main()
{
// this is a comment
}
int main()
{
// this is a comment
// this is another comment
}
Multi-line comments start with the /* and end with the */. They
are also known as C-style comments. Example:
int main()
{
/* This is a
multi-line comment */
}
int main()
{
std::cout << "Hello World.";
}
Believe it or not, the detailed analysis and explanation of this
example is 15 pages long. We can go into it right now, but we will be no
wiser at this point as we first need to know what headers, streams,
objects, operators, and string literals are. Do not worry. We will get
there.
A brief(ish) explanation
The #include <iostream> statement includes the iostream
header into our source file via the #include directive. The iostream
header is part of the standard library. We need its inclusion to use the
std::cout object, also known as a standard-output stream. The <<
operator inserts our Hello World string literal into that output stream.
String literal is enclosed in double quotes "". The ; marks the end of
the statement. Statements are pieces of the C++program that get
executed. Statements end with a semicolon ; in C++. The std is the
standard-library namespace and :: is the scope resolution operator.
Object cout is inside the std namespace, and to access it, we need to
prepend the call with the std::. We will get more familiar with all of
these later in the book, especially the std:: part.
A brief explanation
In a nutshell, the std::cout << is the natural way of outputting data
to the standard output/console window in C++.
We can output multiple string literals by separating them with
multiple << operators:
#include <iostream>
int main()
{
std::cout << "Some string." << " Another
string.";
}
To output on a new line, we need to output a new-line character \n
literal. The characters are enclosed in single quotes '\n'.
Example:
#include <iostream>
int main()
{
std::cout << "First line" << '\n' << "Second
line.";
}
#include <iostream>
int main()
{
std::cout << "First line\nSecond line.";
}
int main()
{
cout << "A bad example.";
}
use the following:
#include <iostream>
int main()
{
std::cout << "A good example.";
}
For calls to objects and functions that reside inside the std
namespace, add the std:: prefix where needed.
© Slobodan Dmitrović 2020
S. Dmitrović, Modern C++ for Absolute Beginners
https://doi.org/10.1007/978-1-4842-6047-0_5
5. Types
Slobodan Dmitrović 1
(1) Belgrade, Serbia
Every entity has a type. What is a type? A type is a set of possible values
and operations. Instances of types are called objects. An object is some
region in memory that has a value of particular type (not to be confused
with an instance of a class which is also called object).
5.1.1 Boolean
Let us declare a variable b of type bool . This type holds values of
true and false.
int main()
{
bool b;
}
This example declares a variable b of type bool. And that is it. The
variable is not initialized, no value has been assigned to it at the time of
construction. To initialize a variable, we use an assignment operator =
followed by an initializer:
int main()
{
bool b = true;
}
We can also use braces {} for initialization:
int main()
{
bool b{ true };
}
int main()
{
char c = 'a';
}
#include <iostream>
int main()
{
char c = 'a';
Another Random Scribd Document
with Unrelated Content
The Project Gutenberg eBook of A Sermon,
Delivered Before His Excellency Edward
Everett, Governor, His Honor George Hull,
Lieutenant Governor, the Honorable Council,
and the Legislature of Massachusetts, on the
Anniversary Election, January 2, 1839
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
SERMON
DELIVERED BEFORE
HIS EXCELLENCY EDWARD EVERETT,
GOVERNOR,
HIS HONOR GEORGE HULL,
LIEUTENANT GOVERNOR,
THE HONORABLE COUNCIL,
AND
THE LEGISLATURE OF MASSACHUSETTS,
ON THE
ANNIVERSARY ELECTION,
JANUARY 2, 1839.
BY MARK HOPKINS, D. D.
President of Williams College.
Boston:
DUTTON AND WENTWORTH, PRINTERS TO THE STATE.
1839.
Commonwealth of Massachusetts.
Man was made for something higher and better, than either to make,
or to obey, merely human laws. He is the creature of God, is subject
to his laws, and can find his perfection, and consequent happiness,
only in obeying those laws. As his moral perfection, the life of his
life, is involved in this obedience, it is impossible that any power
should lay him under obligation to disobey. The known will of God, if
not the foundation of right, is its paramount rule, and it is because
human governments are ordained by him, that we owe them
obedience. We are bound to them, not by compact, but only as
God's institutions for the good of the race. This is what the Bible,
though sometimes referred to as supporting arbitrary power, really
teaches. It does not support arbitrary power. Rightly understood, it is
a perfect rule of duty, and as in every thing else, so in the relations
of subjects and rulers. It lays down the true principles, it gives us
the guiding light. When the general question is whether human
governments are to be obeyed, the answer is, "He that resisteth the
power, resisteth the ordinance of God." "The powers that be are
ordained of God." But when these powers overstep their appointed
limits, and would lord it over the conscience, and come between
man and his maker, then do we hear it uttered in the very face of
power, and by the voice of inspiration, no less than of indignant
humanity, "We ought to obey God rather than men."
It has been in connexion with the maintenance of this principle, first
proclaimed by an Apostle of Christ eighteen hundred years ago, that
all the civil liberty now in the world has sprung up. It is to the
fearless assertion of this principle by our forefathers, that we owe it
that the representatives of a free people are assembled here this day
to worship God according to the dictates of their own consciences,
to seek to Him for wisdom in their deliberations, and to acknowledge
the subordination of all human governments to that which is divine.
Permit me then, as appropriate to the present occasion, to call the
attention of this audience,
1st. To the grounds on which all men are bound to adhere to the
principle stated in the text; and
2d. To the consequences of such adherence, on the part, both of
subjects, and of rulers.
I have thus shown, as fully as the time would permit, though far too
briefly to do justice to the subject, the grounds on which we ought
to obey God rather than men. These are to be found in the relation
of the divine, and of human government respectively, to the ends of
individual, and of social existence. But the occasion on which the
text was uttered, a subject having directly refused obedience to
rulers lawfully constituted, will lead us to consider the effects of the
principle of the text when acted upon by men in those relations in
which civil liberty is directly involved—in the relations of subjects and
of rulers. What then will be the effect of an adherence to this
principle on the part of subjects, as such?
There is a tendency in irresponsible power to accumulate. It first
gains control over property, and life, and every thing from which a
motive to resistance based on the interests of the present life, could
be drawn. But it is not satisfied with this. Nothing avails it so long as
there is a Mordecai sitting at the King's gate that does not rise up
and do it reverence. It must also control the conscience, and make
the religious nature subservient to its purposes. Accordingly, the
grand device of the enemies of civil liberty, has been so to
incorporate religion with the government, that all those deep and
ineradicable feelings which are associated with the one, should also
be associated with the other, and that he who opposed the
government should not only bring upon himself the arm of the civil
power, but also the fury of religious zeal. The most melancholy and
heart-sickening chapter in the history of man, is that in which are
recorded the enormities committed by a lust of power, and by
malignity, in alliance with a perverted religious sentiment. The light
that was in men has become darkness, and that darkness has been
great. The very instrument appointed by God for the deliverance and
elevation of man, has been made to assist in his thraldom and
degradation. When christianity appeared, the alliance of religion with
oppressive power was universal. In such a state of things, there
seemed no hope for civil liberty but in bringing the conscience out
from this unholy alliance, and putting it in a position in which it must
show its energies in opposition to power. This christianity did. It
brought the conscience to a point where it not only might resist
human governments, but where, as they were then exercised, it was
compelled to resist them. This appeared when the text was uttered,
and there was then a rock raised in the ocean of tyranny which has
not been overflowed to this day. The same qualities which make the
conscience so potent an ally of power, must, when it is enlightened
by a true knowledge of God and of duty, and when immortality is
clearly set before the mind, make it the most formidable of all
barriers to tyranny and oppression.
By thus bringing the moral nature of man to act in opposition to
power, and by giving him light, and strength, and foothold, to enable
him to sustain that opposition, christianity has done an inestimable
service, and has placed humanity at the only point where its highest
grandeur appears. At this point, sustained by principle, and often in
the person of the humblest individual, it bids defiance to all the
malice of men to wrest from it its true liberty. It bids tyranny do its
worst, and though its ashes may be scattered to the winds, it leaves
its startling testimony, and the inspiration of its great example to
coming times. The power to do this, christianity alone can give. No
other religion has ever so demonstrated its evidences to the senses,
and caused its adaptations to the innermost wants of the soul to be
felt, as to enable man to stand alone against the influence of
whatever was dear in affection, and flattering in promises, and
fearful in torture. Other religions have had their victims, who have
been led, amidst the plaudits of surrounding multitudes, to throw
themselves under the wheels of a system already established; but
not their martyrs, who, when duty has permitted it, have fled to the
fastnesses of the mountains; and when it has not, have stood upon
their rights, and contested every inch of ground, and met death
soberly and firmly, only when it was necessary. When this has been
done by multitudes it has caused power to respect the individual, to
respect humanity; and while christianity was wading through the
blood of ten persecutions, it was fighting more effectually than had
ever been done before, the battles of civil liberty. The call to obey
God rather than men met with a response, and it is upon this ground
that the battle has been opened in every case in which civil liberty
now exists. It is upon this ground alone that it can be maintained.
I deem it of great importance that this point should be fully and
often presented, because it is vital, and because there are constant
attempts made to obscure it. Whatever elevates the individual,
whatever gives him worth in his own estimation and that of others,
whatever invests him with moral dignity, must be favorable both to
pure morality and to civil liberty. Hence it is that these are both
incidental results of christianity. They are not the gifts which she
came to bestow—these are life and immortality. They are not the
white raiment in which her followers are to walk in the upper
temple; but they are the earthly garments with which she would
clothe the nations—they are the brightness which she leaves in her
train as she moves on towards heaven, and calls on men to follow
her there. These belong to her alone. Infidels may filch her morality,
as they have often done, and then boast of their discoveries. But in
their hands that morality is lopped off from the body of faith on
which it grew, and produces no fruit. They may boast, as they do, of
a liberty which they never could have achieved. But under its
protection they advance doctrines and advocate practices which
would corrupt it into license. Their only strength lies in endeavoring,
in the sacred name of liberty, to corrupt the virtuous, and to excite
the hatred of the vicious against those restraints without which
liberty cannot exist, and society has no ground of security.
"Promising liberty to others, they are themselves the servants of
corruption." Liberty cannot exist without morality, nor general
morality without a pure religion.
The doctrine thus stated is fully confirmed by history. The
reformation by Luther was made on strictly religious grounds. He
found an opposition between the decrees of the Pope and the
commands of God, and it was the simple purpose, resolutely
adhered to, to obey God rather than men, that caused Europe to
rock to its centre. In the train of this religious reformation civil liberty
followed, but became settled and valuable only as religious liberty
was perfected. It was every where on the ground of conscience
towards God that the first stand was taken, and in those countries
where the struggle for religious liberty commenced but did not
succeed, as in Spain and Italy, civil liberty has found no resting place
for the sole of her foot to this day. It is conceded even by Hume that
England owes her civil liberty to the Puritans, and the history of the
settlement and progress of this country as a splendid exemplification
of the principle in question, needs but to be mentioned here.
In speaking thus of the resistance of christian subjects to the
government, perhaps I should guard against being misunderstood.
In no case can it be a factious resistance. It cannot be stimulated by
any of the ordinary motives to such resistance—by discontent, or
passion, or ambition, or a love of gain. In no case can it show itself
in the disorganizing, the aggressive, and in a free government, the
suicidal spirit of mobs. Christians have in their eye a grand and a
holy object, and all they wish is to go forward, without violating the
rights of others, to its attainment. In so doing they set themselves in
opposition to nobody, but merely exercise an inalienable right, and if
others oppose them, they must still go forward and obey God, be
the consequences what they may.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com