// -- C ++ shortcut course reading notes -- Chapter 2--c ++ Overview
// -- Chapter 2--c ++ Overview
// -- 11/7/2005 mon ..
// -- Computer lab
// -- Liwei
All programs should return 0 at the normal end.
Functions cannot be nested.
The only function that does not require prototype declaration is main (). Because the system has been predefined.
// -- Program #1 The first C ++ Program
# Include <iostream>
Using namespace STD;
Int main ()
{
Cout <"this is my first C ++ program." <Endl;
Return 0;
}
// -- Program #2 use a variable
# Include <iostream>
Using namespace STD;
Int main ()
{
Int X;
X = 1023;
Cout <"this program prints the value of X :";
Cout <x <Endl;
Return 0;
}
// -- Program #3 converts a gallon to a liter
# Include <iostream>
Using namespace STD;
Int main ()
{
Int gallons, liters;
Cout <"Enter number of gallons :";
Cin> gallons;
Liters = gallons * 4;
Cout <"liters:" <liters <Endl;
Return 0;
}
// -- Program #4 Use a floating point to convert a gallon into a litre
# Include <iostream>
Using namespace STD;
Int main ()
{
Double gallons, liters;
Cout <"Enter number of gallons :";
Cin> gallons;
Liters = gallons * 3.7854;
Cout <"liters:" <liters <Endl;
Return 0;
}
// -- Program #5 contains two functions
# Include <iostream>
Using namespace STD;
Void myfunc ();
Int main ()
{
Cout <"in main ()" <Endl;
Myfunc ();
Cout <"back in main ()" <Endl;
Return 0;
}
Void myfunc ()
{
Cout <"Inside myfunc ()" <Endl;
}
// -- Program #6 use function ABS ()
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
Int main ()
{
Cout <ABS (-10) <Endl;
Return 0;
}
// -- Program #7 shows a simple example of calling a function
# Include <iostream>
Using namespace STD;
Void MUL (int x, int y );
Int main ()
{
Mul (10, 20 );
Mul (5, 6 );
Mul (8, 9 );
Return 0;
}
Void MUL (int x, int y)
{
Cout <x * Y <"" <Endl;
}
// -- Program #8 shows a simple example of a function that returns a value.
# Include <iostream>
Using namespace STD;
Int MUL (int x, int y );
Int main ()
{
Int answer;
Answer = MUL (10, 11 );
Cout <"The answer is:" <answer <Endl;
Return 0;
}
Int MUL (int x, int y)
{
Return x * Y;
}
// -- Program #9/n usage
# Include <iostream>
Using namespace STD;
Int main ()
{
Cout <"One/N ";
Cout <"Two/N ";
Cout <"three ";
Cout <"four" <Endl;
Return 0;
}
// -- Program #10 use of the IF statement
# Include <iostream>
Using namespace STD;
Int main ()
{
Int A, B;
Cout <"enter first number :";
Cin>;
Cout <"Enter second number :";
Cin> B;
If (A <B)
Cout <"first number is less than second./N ";
Return 0;
}
// -- Program #11 use of the for statement
# Include <iostream>
Using namespace STD;
Int main ()
{
Int count;
For (COUNT = 1; count <= 1000; count ++)
Cout <count <"";
Cout <Endl;
Return 0;
}
// -- Program # Use of 12 code blocks
# Include <iostream>
Using namespace STD;
Int main ()
{
Int A, B;
Cout <"enter first number :";
Cin>;
Cout <"Enter second number :";
Cin> B;
If (A <B)
{
Cout <"first number is less than second./N ";
Cout <"their difference is:" <B-A <Endl;
}
Return 0;
}