1.1.2.calculating With Integer Constants: Using Using Int
1.1.2.calculating With Integer Constants: Using Using Int
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << 1 + 2 << endl;
cout << 1 - 5 << endl;
cout << 1 - 2 << endl;
cout << 1 * 2 << endl;
cout << 1/3 << endl;
cout << 1 3 << endl;
cout << 1 -3 << endl;
cout << -1 3 << endl;
cout << -1 -3 << endl;
cout << 1 + 2/1 - 5 << endl;
cout << (1 + 2)/(1 - 5) << endl;
cout << 1 + 2/(1 - 5) << endl;
cout << (1 + 2)/1 - 5 << endl;
cout << 4*5/3 + 7/3 << endl;
return 0;
}
3
-4
-1
2
0
1
1
-1
-1
-2
0
1
-2
4
#include <iostream>
using std::cout;
using std::endl;
int main() {
int a = 10;
int b = 3;
cout << a/b;
cout << a b;
return 0;
}
31
#include <iostream>
using std::cout;
using std::endl;
int main() {
int a = 10;
int b = 6;
int c = 3;
int d = 4;
int f = (a + b)/(c + d);
cout << f ;
return 0;
}
2
#include <iostream>
using std::cout;
using std::endl;
void useLocal( void );
void useStaticLocal( void );
void useGlobal( void );
int x = 1;
int main()
{
int x = 5;
cout << x << endl;
{
int x = 7;
cout << "local x in main's inner scope is " << x << endl;
}
cout << x << endl;
useLocal();
useStaticLocal();
useGlobal();
useLocal();
useStaticLocal();
useGlobal();
cout << x << endl;
return 0;
}
void useLocal( void )
{
int x = 25;
cout << "local x is " << x << " on entering useLocal" << endl;
x = x + 20;
cout << "local x is " << x << " on exiting useLocal" << endl;
}
void useStaticLocal( void )
{
static int x = 50;
cout << "local static x is " << x << " on entering useStaticLocal"
<< endl;
x = x + 20;
cout << "local static x is " << x << " on exiting useStaticLocal"
<< endl;
}
void useGlobal( void )
{
cout << "global x is " << x << " on entering useGlobal" << endl;
x = x + 20;
cout << "global x is " << x << " on exiting useGlobal" << endl;
}
5
local x in main's inner scope is 7
5
local x is 25 on entering useLocal
local x is 45 on exiting useLocal
local static x is 50 on entering useStaticLocal
local static x is 70 on exiting useStaticLocal
global x is 1 on entering useGlobal
global x is 21 on exiting useGlobal
local x is 25 on entering useLocal
local x is 45 on exiting useLocal
local static x is 70 on entering useStaticLocal
local static x is 90 on exiting useStaticLocal
global x is 21 on entering useGlobal
global x is 41 on exiting useGlobal
5
int n = 7;
int main()
{
double n = 10.5;
cout << "Local double value of n = " << n
<< "\nGlobal int value of n = " << ::n << endl;
return 0;
}
Local double value of n = 10.5
Global int value of n = 7
#include <limits>
#include <iostream>
using std::cout;
using std::endl;
using std::numeric_limits;
int main() {
cout << "The range for type short is from "
<< numeric_limits<short>::min()
<< " to "
<< numeric_limits<short>::max();
cout << "The range for type int is from "
<< numeric_limits<int>::min()
<< " to "
<< numeric_limits<int>::max();
cout << "The range for type long is from "
<< numeric_limits<long>::min()
<< " to "
<< numeric_limits<long>::max();
cout << "The range for type float is from "
<< numeric_limits<float>::min()
<< " to "
<< numeric_limits<float>::max();
cout << "The range for type double is from "
<< numeric_limits<double>::min()
<< " to "
<< numeric_limits<double>::max();
cout << "The range for type long double is from "
<< numeric_limits<long double>::min()
<< " to "
<< numeric_limits<long double>::max();
cout << endl;
return 0;
}
The range for type short is from -32768 to 32767
The range for type int is from -
2147483648 to 2147483647
The range for type long is from -2147483648 to 2147483647
The range for type float is from 1.17549e-038 to 3.40282e+038
The range for typedouble is from 2.22507e-308 to 1.79769e+308
The range for type long double is from 0 to 1.#INF
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
char first = 10;
char second = 20;
cout << "The value of the expression first < second is: "
<< (first < second)
<< endl
<< "The value of the expression first == second is: "
<< (first == second)
<< endl;
return 0;
}
The value of the expression first < second is: 1
The value of the expression first == second is: 0
#include <iostream>
int main()
{
std::cout << "The size of an int is:\t\t";
std::cout << sizeof(int) << " bytes.\n";
std::cout << "The size of a short int is:\t";
std::cout << sizeof(short) << " bytes.\n";
std::cout << "The size of a long int is:\t";
std::cout << sizeof(long) << " bytes.\n";
std::cout << "The size of a char is:\t\t";
std::cout << sizeof(char) << " bytes.\n";
std::cout << "The size of a float is:\t\t";
std::cout << sizeof(float) << " bytes.\n";
std::cout << "The size of a double is:\t";
std::cout << sizeof(double) << " bytes.\n";
return 0;
}
The size of an int is: 4 bytes.
The size of a short int is: 2 bytes.
The size of a long int is: 4 bytes.
The size of a char is: 1 bytes.
The size of a float is: 4 bytes.
The size of a double is: 8 bytes.
1.5.global variable
1.5.1.Use a global variable
1.5.2.Global class variable
1.5.3.Global variables are known throughout the entire program and may be used by any piece of code.
1.5.4.Use :: to reference global variable
#include <iostream>
using namespace std;
void func1();
void func2();
int count; // This is a global variable.
int main()
{
int i; // This is a local variable
for(i=0; i<10; i++) {
::count = i * 2;
func1();
}
return 0;
}
void func1()
{
cout << "count: " << ::count; // access global count
cout << '\n';
func2();
}
void func2()
{
int count;
for(count=0; count<3; count++)
cout << '.';
}
count: 0
...count: 2
...count: 4
...count: 6
...count: 8
...count: 10
...count: 12
...count: 14
...count: 16
...count: 18
...
#include<iostream.h>
class test
{
int i;
public:
test()
{
i=25;
for(int ctr=0; ctr<10;ctr++)
{
cout<<"Counting at"<<ctr<<"\n";
}
}
~test(){};
};
test anObject;
main()
{
return 0;
}
Counting at0
Counting at1
Counting at2
Counting at3
Counting at4
Counting at5
Counting at6
Counting at7
Counting at8
Counting at9
1.5.3.Global variables are known throughout the entire program and may be used by any piece of
code.
#include <stdio.h>
void func1(void), func2(void);
int count;
main(void)
{
count = 100;
func1();
return 0;
}
void func1(void)
{
int temp;
temp = count;
func2();
printf("count is %d", count); /* will print 100 */
}
void func2(void)
{
int count;
for(count=1; count<10; count++)
putchar('.');
}
#include <iostream>
using namespace std;
int global_name = 1001;
int main(void){
int global_name = 1; // Local variable
cout << "Local variable value " << global_name << '\n';
cout << "Global variable value " << ::global_name << '\n';
}
#include <iostream>
using std::cout;
using std::endl;
int main() {
int count1 = 10;
int count3 = 50;
cout << endl << "Value of outer count1 = " << count1;
{
int count1 = 20;
int count2 = 30;
cout << endl << "Value of inner count1 = " << count1;
count1 += 3;
count3 += count2;
}
cout << endl
<< "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3;
cout << endl;
return 0;
}
Value of outer count1 = 10
Value of inner count1 = 20
Value of outer count1 = 10
Value of outer count3 = 80
#include <iostream>
using namespace std;
int main() {
int x = 19; // x is known to all code.
if(x == 19) {
int y = 20;
cout << "x + y is " << x + y << "\n";
}
// y not known here.
return 0;
}
x + y is 39
#include <iostream>
using namespace std;
int main()
{
int i;
int j;
i = 10;
j = 100;
if(j > 0) {
int i; // this i is separate from outer i
i = j / 2;
cout << "inner i: " << i << '\n';
}
cout << "outer i: " << i << '\n';
return 0;
}
inner i: 50
outer i: 10
#include <iostream>
#include <ostream>
int main()
{
for (int i = 0; i < 10; ++i)
{
int x = 2;
if (x < i)
{
double x = 3.4;
std::cout << x;
}
std::cout << x;
}
//std::cout << x; // Error: no x declared in this scope
}
2223.423.423.423.423.423.423.42
#include <iostream>
using std::cout;
using std::endl;
int count1 = 100;
int main() {
int count1 = 10;
int count3 = 50;
cout << endl << "Value of outer count1 = " << count1;
cout << endl << "Value of global count1 = " << ::count1;
{
int count1 = 20;
int count2 = 30;
cout << endl << "Value of inner count1 = " << count1;
cout << endl << "Value of global count1 = " << ::count1;
count3 += count2;
}
cout << endl
<< "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3;
cout << endl;
return 0;
}
Value of outer count1 = 10
Value of global count1 = 100
Value of inner count1 = 20
Value of global count1 = 100
Value of outer count1 = 10
Value of outer count3 = 80
#include <iostream.h>
int n=0; //Global
main()
{
int n = 1;
{
int n = 2 ;
{
int n = 3;
cout << "In inner n=" <<n<<endl;
cout << "Global n=" << ::n <<endl;
}
cout << "In outter n=" <<n<<endl;
cout << "Global n=" <<::n<<endl;
}
cout << "In main() n=" << n<<endl;
return 0 ;
}
In inner n=3
Global n=0
In outter n=2
Global n=0
In main() n=1
#include <iostream>
using namespace std;
void func();
int main()
{
int var = 5;
cout << "In main() var is: " << var << "\n\n";
func();
cout << "Back in main() var is: " << var << "\n\n";
{
cout << "In main() in a new scope var is: " << var << "\n\n";
cout << "Creating new var in new scope.\n";
int var = 10;
cout << "In main() in a new scope var is: " << var << "\n\n";
}
cout << "At end of main() var is: " << var << "\n";
return 0;
}
void func()
{
int var = -5; // local variable in func()
cout << "In func() var is: " << var << "\n\n";
}
#include <iostream>
using namespace std;
int glob = 10; // global variable
void access_global();
void hide_global();
void change_global();
int main()
{
cout << "In main() glob is: " << glob << "\n\n";
access_global();
hide_global();
cout << "In main() glob is: " << glob << "\n\n";
change_global();
cout << "In main() glob is: " << glob << "\n\n";
return 0;
}
void access_global()
{
cout << "In access_global() glob is: " << glob << "\n\n";
}
void hide_global()
{
int glob = 0; // hide global variable glob
cout << "In hide_global() glob is: " << glob << "\n\n";
}
void change_global()
{
glob = -10; // change global variable glob
cout << "In change_global() glob is: " << glob << "\n\n";
}
1.6.9.Using the scope resolution operator (2)
#include <iostream>
#include <ostream>
namespace n {
struct counter {
static int n;
};
double n = 2.8;
}
int n::counter::n = 7;
int main()
{
int counter = 0;
int n = 10;
std::cout << n::counter::n;
std::cout << n::n;
std::cout << x.n;
std::cout << n;
std::cout << counter;
}
Value of outer count1 = 10
Value of global count1 = 100
Value of inner count1 = 20
Value of global count1 = 100
Value of outer count1 = 10
Value of outer count3 = 80
#include <iostream>
using namespace std;
double box(double length, double width, double height); // use double data
int main()
{
double sum;
sum = box(10.1, 11.2, 3.3) + box(5.5, 6.6, 7.7) + box(4.0, 5.0, 8.0);
cout << "The sum of the volumes is " << sum << "\n";
cout << "The average volume is " << sum / 3.0 << "\n";
return 0;
}
// This version of box uses double data.
double box(double length, double width, double height)
{
return length * width * height ;
}
The sum of the volumes is 812.806
The average volume is 270.935
1.8.const
1.8.1.Define constant
1.8.2.Demonstrates constants
1.8.3.const double value
1.8.4.Using a properly initialized constant variable.
1.8.5.Same Program Using Constant Integers
1.8.6.Demonstrating the const type qualifier
1.8.1.Define constant
#include <iostream>
using namespace std;
const int intCount = 100;
int main()
{
int empNums[intCount];
double salary[intCount];
char *names[intCount];
}
1.8.2.Demonstrates constants
#include <iostream>
using namespace std;
int main()
{
const int ALIEN_POINTS = 150;
int aliensKilled = 10;
int score = aliensKilled * ALIEN_POINTS;
cout << "score: " << score << endl;
enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE};
difficulty myDifficulty = EASY;
enum ship {FIGHTER = 25, BOMBER, CRUISER = 50, DESTROYER = 100};
ship myShip = BOMBER;
cout << (CRUISER - myShip) << " Resource Points.\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double bottles;
cout << "How many bottles do you have? ";
cin >> bottles;
double cans;
cout << "How many cans do you have? ";
cin >> cans;
const double BOTTLE_VOLUME = 2.0;
const double CAN_VOLUME = 0.355;
double total = bottles * BOTTLE_VOLUME
+ cans * CAN_VOLUME;
cout << "The total volume is " << total << " liter.\n";
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
const int x = 7;
cout << x << endl;
return 0;
}
7
#include <iostream>
int main()
{
const int Sunday = 0;
const int Monday = 1;
const int Tuesday = 2;
const int Wednesday = 3;
const int Thursday = 4;
const int Friday = 5;
const int Saturday = 6;
int today;
today = Monday;
if (today == Sunday || today == Saturday)
std::cout << "\nGotta' love the weekends!\n";
else
std::cout << "\nBack to work.\n";
return 0;
}
void f( const int [] );
int main()
{
int a[] = { 10, 20, 30 };
f( a );
cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n';
return 0;
}
void f( const int b[] )
{
cout << b[ 0 ];
}
1010 20 30
1.12.static
1.12.1. Using a static long variable
Use static variable to compute a running average of numbers entered
1.12.2.
by the user
1.12.3. Initialize static member field outside the class declaration
A static local variable causes the compiler to create permanent
1.12.4.
storage for it in much the same way that it does for a global variable.
long next();
int main() {
for(int i = 0 ; i < 30 ; i++) {
cout << std::setw(12) << next ();
}
cout << endl;
return 0;
}
long next () {
static long last = 0;
last = last + 1;
return last;
}
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19
20
21 22 23 24 25 26
27 28 29 30
1.12.2.Use static variable to compute a running average of numbers entered by the user
#include <iostream>
using namespace std;
int f(int i);
int main()
{
int num;
do {
cout << "Enter numbers (-1 to quit): ";
cin >> num;
if(num != -1)
cout << "average is: " << f(num) << "\n";
} while(num > -1);
return 0;
}
int f(int i)
{
static int sum = 0, count = 0;
sum = sum + i;
count++;
return sum / count;
}
Enter numbers (-1 to quit): 1
average is: 1
Enter numbers (-1 to quit): 2
average is: 1
Enter numbers (-1 to quit): 3
average is: 2
Enter numbers (-1 to quit): 2
average is: 2
Enter numbers (-1 to quit): 1
average is: 1
Enter numbers (-1 to quit): 2
average is: 1
Enter numbers (-1 to quit): 3
average is: 2
Enter numbers (-1 to quit): -1
#include <iostream>
using std::cout;
using std::endl;
class Box {
public:
Box() {
cout << "Default constructor called" << endl;
++objectCount;
length = width = height = 1.0;
}
Box(double lvalue, double wvalue, double hvalue) :
length(lvalue), width(wvalue), height(hvalue)
{
cout << "Box constructor called" << endl;
++objectCount;
}
double volume() const {
return length * width * height;
}
int getObjectCount() const {return objectCount;}
private:
static int objectCount;
double length;
double width;
double height;
};
int Box::objectCount = 0;
int main() {
cout << endl;
Box firstBox(17.0, 11.0, 5.0);
cout << "Object count is " << firstBox.getObjectCount() << endl;
Box boxes[5];
cout << "Object count is " << firstBox.getObjectCount() << endl;
cout << "Volume of first box = "
<< firstBox.volume()
<< endl;
const int count = sizeof boxes/sizeof boxes[0];
cout <<"The boxes array has " << count << " elements."
<< endl;
cout <<"Each element occupies " << sizeof boxes[0] << " bytes."
<< endl;
for(int i = 0 ; i < count ; i++)
cout << "Volume of boxes[" << i << "] = "
<< boxes[i].volume()
<< endl;
return 0;
}
Box constructor called
Object count is 1
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Object count is 6
Volume of first box = 935
The boxes array has 5 elements.
Each element occupies 24 bytes.
Volume of boxes[0] = 1
Volume of boxes[1] = 1
Volume of boxes[2] = 1
Volume of boxes[3] = 1
Volume of boxes[4] = 1
1.12.4.A static local variable causes the compiler to create permanent storage for it in much the same
way that it does for a global variable.
#include <stdio.h>
#include <conio.h>
int count(int i);
int main(void)
{
do {
count(0);
} while(!kbhit());
printf("count called %d times", count(1));
return 0;
}
int count(int i)
{
static int c=0;
if(i) return c;
else c++;
return 0;
}