C++ overview, Input, Output, Operations
Comments
2 types of formats for comments are allowed in C++
/* This is the traditional way carried over from the C programming language */
// C++ introduced this new way
// of writing comments
Anything that follows // is a C++ comment and the compiler does not translate it into machine language
.....
.....
float fah, cel; // Variable declarations
....
C style comments (using /* and */) are still useful in commenting large blocks
/* If you want to write comments
that span multiple lines
and go on and on
then this is the way to do it */
#include <iostream>
iostream is the standard class for most of the C++ input and output
This class is defined in file named iostream.h
The above directive can also be replaced by
#include <iostream.h>
The #include directive tells the compiler to include code from the iostream class when compiling our program
using namespace std;
The above line tells the compiler that we are going to use objects defined in the standard namespace (std)
Every C++ program must have a main function
Program execution begins with the function main
return_type function_name(argument_list)
{
Function Body
}
return_type for the main function is int This indicates that the main function returns an integer. Hence we have the statement
return 0;
towards the end of the main function
Used to declare variables that will be used in our program
Consist of the data type and the variable name
Other keywords like const, static etc... can be a part of them
These statements are the place where your program DOES something like doing some computation, taking input from user, displaying output etc...
cout << "Enter temperature in Fahrenheit : ";
is an output statement
cin >> fah;
is an input statement
cel = (fah - 32) * 5 / 9 ;
is a statement that does some arithmetic operations
Reserved words are used in C++ for some pre determined purposes and hence cannot be used for any other purposes. Appendix B in the book lists the C++ reserved words.
Identifiers are used to represent data elements and objects in a program. An identifier MUST consist of letters, digits or underscores only, it must begin with either a letter or the underscore symbol. Any reserved word cannot be used as an identifier.
first_name
are all valid identifier names.
num1
temp_1_f
.....
Identifiers are case sensitive. AVERAGE, average, AveRage
are all
distinct identifiers. Choose identifiers in a way that relates the name to the identifier's
purpose.
Integers, Floating Point, Characters and Boolean are the pre-defined standard data types
int
float
bool
char
stringclass
Strings in C++ can be represented as a sequence of characters. This is the preferred way of doing string representation.
There is a pre-defined string class that can be used to manipulate strings in C++.
#include <string>
must be included as a compiler directive
"CS 120", "a", "123", "Ball State" are all string objects. "a" is a string whereas 'a' is a character. "123" is a string whereas 123 is an integer.
Can take on different values (hence the name)
Variables must follow the rules for naming identifiers since a variable name is essentially
an identifier.
data_type identifier;
data_type identifier_list;
data_type identifier = initial_value;
Here are some examples
int num1;
int avg = 0;
float gcd, lcm;
string first_name = "John";
Never change value
Used for representing data that will always have the same value anywhere in the program.
Multiplying factor in a formula, conversion factor etc...
const data_type identifier = initial_value;
const float CE_TO_FA = 1.8;
fah = (cel * CE_TO_FA) + 32;
One of the advantages is that it makes changes to code easier
Conventionally declared with all uppercase letters
cin, coutare used for standard input and output respectively
cout << stuff_to_output;
cin >> variable_to_store_input;
cout directs output to standard output (normally the screen)
cout is able to determine the data type of the object and outputs it
accordingly
cout << " First Name is ";
name, salary are string and int variables repsectively.
cout << name;
cout << "Salary is ";
cout << salary;
cout << "This is CS 120 \n"; // Outputs a newline character towards the end
cout << "This is CS 120 " << endl; // Signifies end of line
cout << "First name is "<< name <<" Salary is "<< salary << endl; // Chaining is allowed
cout << "This is the ";
cout << "the same line ";
cout << "but uses multiple ";
cout << "cout's for output.";
cin maps to standard input (normally the keyboard)
cin is able to determine the data type of the variable and stores data
appropriately
cin >> num1; // Stores data in num1
cin >> num1 >> num2; // Chaining is allowed
cin reads input till the next whitespace character (space, tab, newline).
Type mismatches are sometimes handled but one needs to be careful
Add +
Subtract -
Multiple *
Divide /
Remainder %
speed = distance / time;
Result is float if any of the operands is float.
salary = no_of_hours * rate;
17 / 5 = 3
17.0 / 5 = 3.4
17 / 5.0 = 3.4
6 + 3 = 9
6 + 3.0 = 9
24 % 7 = 3
Order of evaluation is
30 % 3 = 0
In order to avoid confusion, use parentheses whenever needed
++ and -- are respectively increment and decrement operators
....
int x = 4;
cout << x << endl;
x++;
cout << x << endl;
will output
4
x++ is post increment
++x is pre increment
5
x++ // x = x + 1;
y-- // y = y - 1;
int p = 4;
int q = 10;
int y, z;
y = p++; // p = 5, y = 4
z = ++q; // q = 11, z = 11
x += 3; // Addition assignment operator. x = x + 3
x *= 3; // Multiplication assignment operator. x = x * 3
x -= 3; // Subtraction assignment operator. x = x - 3
x /= 3; // Division assignment operator. x = x / 3
cout sends output to screen and cin gets input from the keyboard.
It is possible to send output to a file and read input from a file
a.out > outputfile // Send output to outputfile
a.out < inputfile // Take input from inputfile
a.out < inputfile > outputfile // Send output to outputfile
// and take input from inputfile
// Add 2 integers #include <iostream> using namespace std; int main(){ int num1, num2; cin >> num1 >> num2; cout << "Sum is " << (num1 + num2) << endl; return 0; }
// Expression with multiple operations #include <iostream> using namespace std; int main(){ float num1, num2, num3, num4, num5; cin >> num1 >> num2 >> num3 >> num4 >> num5; float ans; ans = (num1 * num2 * (num3 - num4 )) / num5; cout << ans; return 0; }
// Division and Remainder #include <iostream> using namespace std; int main(){ int num1, num2; cin >> num1 >> num2; cout << "Quotient is " << (num1 / num2) << endl; cout << "Remainder is " << (num1 % num2) << endl; return 0; }
// Assignment Operators #include <iostream> using namespace std; int main(){ float num1, num2; cin >> num1 >> num2; num1 += 20; num1 *= 5; cout << num1 << endl; num2 -= num1; cout << num2 << endl; return 0; }
// Strings #include <iostream> #include <string> using namespace std; int main(){ string str1 = "Bill"; string str2; str2 = "Gates"; cout << str1 << " " << str2 << endl; return 0; }
// Characters and Integers #include <iostream> using namespace std; int main(){ char ch1, ch2; cin >> ch1 >> ch2; cout << ch2 << endl; cout << ch1 + 5 << endl; return 0; }