Professional Documents
Culture Documents
C++ Notes
C++ Notes
identifier, operator
cin, cout
main Function
int main()
is a function. C++ program is a collection of functions. Every
program in C++ begins executing at the function main().
Every C++ program has exactly one main function and a
collection of other functions. A function is the main
entity where all of the functionality of a program is
defined. A function takes some input, processes it
according to the some code written in the function body
and produces an output which can be either sent to the
user or can be used by other functions.
OOP, IT, NUR 3/10/2008 4
Recall(cont.)
Statement
std::cout << “My first C++ program ” << std::endl;
The above line is a statement in C++. A statement must always
terminate with a semicolon (;) otherwise it causes a syntax error.
This statement introduces two new features of C++ language, cout
and << operator. W hen this statement is executed, it sends the
string between the quotation marks to the standard output stream
object – cout. cout is a predefined object in C++ and the standard
output stream is normally the screen.
The purpose of standard output stream object is to print the output on
the device to which it is connected. Normally, the output device is
screen but it can be programmed to output to any other device such
as a printer. The following diagram illustrated the concept of cout.
OUTP U T
W hen this program is run, it prints the following output
My first C++ program
For programs to do exciting things, they should be written,
compiled and then run on a computer using a compile or
IDE.
You have just run your first C++ program and have
understood many important features of C++.
# include <iostream.h>
int square (int);
int cube (int);
int main()
{
cout <<“27 squared is”
<< square(27) << endl;
cout<<“27 cubed is”
<< cube(27) <<endl;
return 0;
}
return 0;
}
Results: a:4 b:7
+ Addition
- subtraction
* multiplication
/ Division
% Modulo
expression Is equivalent to
Value+=increase; Value=value + increase
a - =5; a=a-5;
a / =b a = a/ b;
price *= units + !; price =price* (units +1);
// compound assignment
#include< iostream>
Using namespace std;
Int main ()
{
int a, b=3;
a =b;
a+= 2; // equivalent to a=a+2
Cout<< a;
Return 0;
}
Results: 5
In this example a was 2 and b was 7, so the expression being evaluated (a>b) was not true,
thus the first value specified after the question mark was discarded in favor of the
second value (the one after the colon) which was b, with a value of 7.
int age;
Cin>> age;
The first statement declares a variable of type int called age, and the second
one waits for an input from cin (the keyboard) in order to store it in this
integer variable.
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
Notice how in both calls to getline we used the same string identifier (mystr). What the program does in the second call is
simply to replace the previous content by the new one that is introduced
int main ()
{
string mystr;
float price=0;
int quantity=0;
return 0;
}
Results: Enter price: 22.25
Enter quantity: 7
Total price: 155.75