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." << '
';
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 << '
';
std::cout << "The value of an int variable is: " << myint << '
';
std::cout << "The value of a double variable is: " << mydouble << '
';
}