6. Exercises – Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards

© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_6

6. Exercises

Slobodan Dmitrović1 
(1)
Belgrade, Serbia
 

6.1 Hello World and Comments

Write a program that has a comment in it, outputs “Hello World.” on one line, and “C++ rocks!” on a new line.
#include <iostream>
int main()
{
    // this is a comment
    std::cout << "Hello World." << '\n';
    std::cout << "C++ rocks!";
}

6.2 Declaration

Write a program that declares three variables inside the main function. Variables are of char, int, and type double. The names of the variables are arbitrary. Since we do not use any input or output, we do not need to include the <iostream> header.
int main()
{
    char mychar;
    int myint;
    double mydouble;
}

6.3 Definition

Write a program that defines three variables inside the main function. The variables are of char, int, and type double. The names of the variables are arbitrary. The initializers are arbitrary.
int main()
{
    char mychar = 'a';
    int myint = 123;
    double mydouble = 456.78;
}

6.4 Initialization

Write a program that defines three variables inside the main function. The variables are of char, int, and type double. The names of the variables are arbitrary. The initializers are arbitrary. The initialization is performed using the initializer list. Print the values afterward.
#include <iostream>
int main()
{
    char mychar{ 'a' };
    int myint{ 123 };
    double mydouble{ 456.78 };
    std::cout << "The value of a char variable is: " << mychar << '\n';
    std::cout << "The value of an int variable is: " << myint << '\n';
    std::cout << "The value of a double variable is: " << mydouble << '\n';
}