Chapter 1. An
Overview of C++
Home
|
#include <iostream>
using namespace std;
int main()
{
int num1 = 1234, num2 = 5678;
cout << endl; //Start on a new line
cout << num1 << num2; //Output two values
cout << endl; //End on a new line
return 0; //Exit program
}
|
|
#include <iostream>
using namespace std;
int main()
{
int apples, oranges; // Declare two integer variables
int fruit; // ...then another one
apples = 5; oranges = 6; // Set initial values
fruit = apples + oranges; // Get the total fruit
cout << endl; // Start output on a new line
cout << "Oranges are not the only fruit... " << endl
<< "- and we have " << fruit << " fruits in all.";
cout << endl; // Start output on a new line
return 0; // Exit the program
}
|
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num1 = 1234, num2 = 5678;
cout << endl; //Start on a new line
cout << setw(6) << num1 << setw(6) << num2; //Output two values
cout << endl; //Start on a new line
return 0; //Exit program
}
|
|
// Calculating how many rolls of wallpaper are required for a room
#include <iostream>
using namespace std;
int main()
{
double height = 0.0, width = 0.0, length = 0.0;
// Room dimensions
double perimeter = 0.0; // Room perimeter
const double rollwidth =21.0; // Standard roll width
const double rolllength = 12.*33.; // Standard roll length(33ft.)
int strips_per_roll = 0; // Number of strips in a roll
int strips_reqd = 0; // Number of strips needed
int nrolls = 0; // Total number of rolls
cout << endl // Start a new line
<< "Enter the height of the room in inches: ";
cin >> height;
cout << endl // Start a new line
<< "Now enter the length and width in inches: ";
cin >> length >> width;
strips_per_roll = rolllength/height;
// Get number of strips in a roll
perimeter = 2.0*(length + width); // Calculate room perimeter
strips_reqd = perimeter/rollwidth; // Get total strips required
nrolls = strips_reqd/strips_per_roll;
// Calculate number of rolls
cout << endl
<< "For your room you need " << nrolls
<< " rolls of wallpaper."
<< endl;
return 0;
}
|
|
#include <iostream>
using namespace std;
int main()
{
long num1 = 0, num2 = 0, num3 = 0, num4 = 0;
num4 = (num1 = 10, num2 = 20, num3 = 30);
cout << endl
<< "The value of a series of expressions "
<< "is the value of the right most: "
<< num4;
cout << endl;
return 0;
}
|
|
// Demonstrating variable scope
#include <iostream>
using namespace std;
int main()
{ // Function scope starts here
int count1 = 10;
int count3 = 50;
cout << endl
<< "Value of outer count1 = " << count1
<< endl;
{ // New scope starts here...
int count1 = 20; // This hides the outer count1
int count2 = 30;
cout << "Value of inner count1 = " << count1
<< endl;
count1 += 3; // This affects the inner count1
count3 += count2;
} // ...and ends here
cout << "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3
<< endl;
// cout << count2 << endl; // uncomment to get an error
return 0;
} // Function scope ends here
|
|
// Demonstrating variable scope
#include <iostream>
using namespace std;
int count1 = 100; // Global version of count1
int main()
{ // Function scope starts here
int count1 = 10;
int count3 = 50;
cout << endl
<< "Value of outer count1 = " << count1
<< endl;
cout << "Value of global count1 = " << ::count1
// From outer block
<< endl;
{ // New scope starts here...
int count1 = 20; //This hides the outer count1
int count2 = 30;
cout << "Value of inner count1 = " << count1
<< endl;
cout << "Value of global count1 = " << ::count1
// From inner block
<< endl;
count1 += 3; // This affects the inner count1
count3 += count2;
} // ...and ends here.
cout << "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3
<< endl;
//cout << count2 << endl; // uncomment to get an error
return 0;
} // Function scope ends here
|
|
// Demonstrating namespace names
#include <iostream>
int value = 0;
int main()
{
std::cout << "enter an integer: ";
std::cin >> value;
std::cout << "\nYou entered " << value
<< std::endl;
return 0;
}
|
|
// Declaring a namespace
#include <iostream>
namespace myStuff
{
int value = 0;
}
int main()
{
std::cout << "enter an integer: ";
std::cin >> myStuff::value;
std::cout << "\nYou entered " << myStuff::value
<< std::endl;
return 0;
}
|
|
#include <iostream>
namespace myStuff
{
int value = 0;
}
using namespace myStuff;
int main()
{
std::cout << "enter an integer: ";
std::cin >> value;
std::cout << "\nYou entered " << value
<< std::endl;
return 0;
}
|
[Last Update: 2002.2.20] Dongseo University Cyber Campus
|