// Declaration & definition examples
#include <iostream> // no semicolon after directive
using std::cout; // console output
using std::endl; // newline
extern int i; // Variable declaration without definition
extern float f(float); // Function declaration
float b; // Variable declaration & definition
float f(float a) // Function definition
{
return a + 1.0;
}
int i; // Variable definition
int h(int x) // Function declaration & definition
{
return x + 1;
}
int main()
{
b = 1.0;
i = 2;
cout << f(b) << endl;
cout << h(i) << endl;
int i1, i2;
// int i3, int i4; // compile error
int i3; int i4; // OK
// short sh, long l; // compile error
short sh; long l; // OK
return 0;
}
/*
g++ Declare.cpp -o Declare
./Declare
Declare
2
3
*/
using namespace std;
or use only what you need from the standard namespace, as in:
using std::cout;
using std::endl;
In both cases, you can then write cout and endl instead of std::cout and std::endl respectively.
If you use the entire standard namespace, then all the names in std become available, increasing the risk of name collision.
You can run the program with
./Declare
or
Declare
"." is the current directory. Both
program
and
./program
run the program "program" from the current directory.
You can compile the program without the -o (output) option,
g++ Declare.cpp
creating the file a.out, which you can run with
a.out
./a.out
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Saying Hello with C++
#include <iostream> // Stream declarations
using std::cout;
using std::endl;
int main()
{
cout << "Hello, world! I am "
<< 8 << " today!" << endl;
}
/*
g++ Hello.cpp -o Hello
./Hello
Hello, world! I am 8 today!
*/
<< "8 today!" << endl;
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-1. Modify Hello.cpp so that it prints out your name and age (or shoe size, or your dog’s age, if that makes you feel better). Compile and run the program.
#include <iostream> // Stream declarations
using std::cout; // console output
using std::endl; // newline
int main()
{
cout << "Hello, world! My name is John Doe. I am "
<< 38 << " today!" << endl;
cout << "My dog's name is Wolfy. He is 1 year old.\n";
}
/*
g++ Family.cpp -o Family
./Family
Hello, world! My name is John Doe. I am 38 today!
My dog's name is Wolfy. He is 1 year old.
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl; // newline
using std::dec; // decimal (base 10)
using std::oct; // octal (base 8)
using std::hex; // hexadecimal (base 16)
int main()
{
// Specifying formats with manipulators:
cout << "A number in decimal: "
<< dec << 15 << '\n';
cout << "in octal: " << oct << 15 << "\n";
cout << "in hex: " << hex << 15 << endl;
cout << "A floating-point number: "
<< 3.14159 << char(10); // newline (ASCII 10)
cout << "non-printing char (escape): "
<< char(27) << char(10); // newline
}
/*
g++ Stream.cpp -o Stream
./Stream
A number in decimal: 15
in octal: 17
in hex: f
A floating-point number: 3.14159
non-printing char (escape):
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Character array (string) concatenation
#include <iostream>
int main()
{
std::cout << "This is far too long to put on a "
"single line, but it can be broken up with\n"
"no ill effects as long as there is no "
"punctuation separating adjacent\n"
"character arrays.\n";
}
/*
g++ Concat.cpp -o Concat
./Concat
This is far too long to put on a single line, but it can be broken up with
no ill effects as long as there is no punctuation separating adjacent
character arrays.
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Converts decimal to octal and hex
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::oct;
using std::hex;
int main()
{
int number;
cout << "Enter a decimal number: ";
cin >> number;
cout << "value in octal = 0" << oct << number << endl;
cout << "value in hex = 0x" << hex << number << endl;
}
/*
g++ Numconv.cpp -o Numconv
./Numconv
Enter a decimal number: 15
value in octal = 017
value in hex = 0xf
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-2. Using Stream.cpp and Numconv.cpp as guidelines, create a program that asks for the radius of a circle and prints the area of that circle. You can just use the ‘*’ operator to square the radius. Do not try to print out the value as octal or hex (these only work with integral types).
// prints the area of a circle, knowing the radius
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
float radius;
cout << "Enter the radius: ";
cin >> radius;
cout << "Area: " << 3.14 * radius * radius << endl;
}
/*
g++ Circle.cpp -o Circle
./Circle
Enter the radius: 2.7
Area: 22.8906
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Call another program
#include <cstdlib> // Declare "system()"
int main()
{
system("Hello"); // program Hello must be in the same folder
}
/*
g++ CallHello.cpp -o CallHello
./CallHello
Hello, world! I am 8 today!
*/