4. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!“<<endl;
return 0;
}
comment
Preprocessor
Directives
Main function
braces
output
Return statement
Header file
5. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
a. comments
Program comments are explanatory statements that you can include in the C++ code. These
comments help anyone reading the source code. All programming languages allow for some
form of comments.
C++ supports single-line and multi-line comments. All characters available inside any
comment are ignored by C++ compiler.
6. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
#include < Filename.h >
#include tells the computer to copy, or include the given file name into
C++ program. #include tells the preprocessor to dump in the contents
of another file, here the iostream file, which defines the procedures
for input/output.
< Filename.h> is called an include file or header file.
< > symbol use to tell computer to looks in the primary system default
directory and not the current working directory.
DO NOT terminate a preprocessor directive with a semicolon.
#include <filename.h> ; Wrong!
b. preprocessor directives
7. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
.h files or "headers" are libraries of code you may insert in your program by including
them by reference in the top block.
The point of header files is to create libraries of code that can be used over and over
#include< stdio.h > is the Standard Input and Output header file. Expl : printf, scanf.
#include< iostream.h > is the Input and Output Stream header file. Expl : cout, cin.
Class declaration from the executable code of the associated methods or function,
because we need the definitions and declaration to be known in a variety of places
through a large program. Example of common standard header file:
<stdio>
<fstream>
<iostream>
c. header files
<list>
<map>
<stack>
<math>
<numeric>
<functional>
8. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
Types of Header Files :
i. iostream.h -Input/Output, interaction with the program. Expl : getline.
ii. math.h –For using mathematic formula
iii. string.h- C++ has no built-in string handling. Use this library to copy strings, find string
length, etc.
iv. stdio.h- Similar to stdlib but also has some file functions. printf, scanf(for C language)
c. header files
9. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
d. main() function
int main()
{
return 0;
}
void main()
{
}
Is the place where the code execution begins.
C++ program is simply a collection of function blocks.
void main() {...}
- defines the code that should execute first when the program starts up.
- The curly braces represent grouping of multiple commands into a block.
10. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
d. main() function
void is a keyword, follow by the function’s name, and then all C++ program is written
within this open and closing curly brace { }.
void main ( ) {…}
// void mean that the function named main do not return a value.
void function2 ( ) {…}
// the function named function2 do not return a value.
11. 1.1.1 DESCRIBE THE ITEM IN C++ PROGRAM STRUCTURE
e. return statements
The return statements will stop the execution and returns to the calling
function.
Return optional in void functions
Return required in non-void functions
12. 1.1.2 DESCRIBE TWO TYPES OF COMMENTS THAT SUPPORTED BY
C++ PROGRAM
There are 2 types
of comment
// Double Forward Slash
//This program is for lab1
Computer will ignore
anything you write after
the double forward slash
but for one line comment
only.
/*Multiline
Comment*/
/*This program is for
you to understand
how to write multiline
comments
using multiline
comment symbol */
13. 1.1.2 DESCRIBE TWO TYPES OF COMMENTS THAT SUPPORTED BY
C++ PROGRAM
C++ comments start with /* and end with */. For example −
14. 1.1.2 DESCRIBE TWO TYPES OF COMMENTS THAT SUPPORTED BY
C++ PROGRAM
A comment can also start with //, extending to the end of the line. For example −
15. 1.1.2 DESCRIBE TWO TYPES OF COMMENTS THAT SUPPORTED BY
C++ PROGRAM
A comment can also start with //, extending to the end of the line. For example −
When the above code is compiled, it will ignore // prints Hello World and final executable will produce
the following result −
16. 1.1.2 DESCRIBE TWO TYPES OF COMMENTS THAT SUPPORTED BY
C++ PROGRAM
Within a /* and */ comment, // characters have no special meaning. Within a // comment, /* and */ have no
special meaning. Thus, you can "nest" one kind of comment within the other kind. For example −
17. 1.1.3 EXPLAIN CODING STANDARDS BEST PRACTICES.
Definition and Purpose of coding standards and best practices
Coding best practices are a set of informal rules that the software development community has
learned over time which can help improve the quality of software.
To develop reliable and maintainable applications, you must follow coding standards and best
practices.
There are several standards exists in the programming industry. None of them are wrong or
bad and you may follow any of them. What is more important is, selecting one standard
approach and ensuring that everyone is following it.
18. 1.1.3 EXPLAIN CODING STANDARDS BEST PRACTICES.
How to follow the standards across the team
If you have a team of different skills and tastes, you are going to have a tough time convincing everyone to follow the
same standards. The best approach is to have a team meeting and developing your own standards document. You may
use this document as a template to prepare your own document.
Distribute a copy of this document (or your own coding standard document) well ahead of the coding standards meeting.
All members should come to the meeting prepared to discuss pros and cons of the various points in the document. Make
sure you have a manager present in the meeting to resolve conflicts.
Discuss all points in the document. Everyone may have a different opinion about each point, but at the end of the
discussion, all members must agree upon the standard you are going to follow. Prepare a new standards document with
appropriate changes based on the suggestions from all of the team members. Print copies of it and post it in all
workstations.
After you start the development, you must schedule code review meetings to ensure that everyone is following the rules. 3
types of code reviews are recommended: Peer review, Architect review and Group review
19. 1.1.3 EXPLAIN CODING STANDARDS BEST PRACTICES.
Best Practices for Writing Super Readable Code
Commenting &
Documentation
Consistent Indentation
Avoid Obvious Comments
Code Grouping
Consistent Naming Scheme
Avoid Deep Nesting
Limit Line Length
File and Folder Organization
21. 1.2.1 EXPLAIN IDENTIFIER, VARIABLE AND CONSTANT.
Identifiers refers to the name of variables, functions, arrays, classes, etc. created by the user. Identifiers
are the fundamental requirement of any language.
A variable is a meaningful name of data storage location in computer memory. When using a variable you
refer to memory address of computer.
Constants refers to fixed values that do not change during the execution of a program.
22. 1.2.2 STATE THE RULES FOR NAMING AN IDENTIFIER.
Naming conventions rules for Identifier
i. Only alphabetic characters, digits and underscores are permitted.
Ex: sum_of_squares, box_22A, GetData
i. First letter must be an alphabet or underscore (_).
Ex: box_22A, _GetData
iii. Identifiers are case sensitive.
Ex: box, BOX, Box
iv. Reserved keywords cannot be used as an identifier's name. Reserved words or keywords are identifiers
reserved for system use.
Ex: local, private, int
v. The blank space cannot be used.
Ex: Get Data - cannot be an identifier as blanks are not allowed in identifiers
vi. Avoid excessively long identifiers.
23. 1.2.2 STATE THE RULES FOR NAMING AN IDENTIFIER.
vii. You can develop your own conventions like below:
A single word identifier will be written in lower case only.
Ex: grade, number, sum.
If an identifier is made up of several words, the first letter will be lower case. Subsequent words will begin
with upper case.
Some examples are: stringType, passingScore, largestNum.
Identifiers used as constants are often fully capitalized.
Ex: PI, MAXSTRLEN.
24. 1.2.3 NAME THE VARIABLES ACCORDING THE STANDARDS.
We know that in C, all variables must be declared before they are used, this is true with C++.
The main difference in C and C++ with regards to the place of their declaration in the program.
C requires all the variables to be defined in the beginning of scope.
C++ allows the declaration of a variable anywhere in the scope, this means that a variable can
be declared right at the place of its first use.
How to declare variable?
25. 1.2.3 NAME THE VARIABLES ACCORDING THE STANDARDS.
How to declare variable?
When you ask user to input a data, you need to save the data into a
memory location inside computer. To do so, you need to Define the
variable, a variable is a named location in memory.
i) Give the computer the name or address of a memory location where
the data to be save.
ii) Tell the computer, the data type you want to save.
After you define a variable, computer will reserves a location inside
the computer memory for the variable value.
26. 1.2.3 NAME THE VARIABLES ACCORDING THE STANDARDS.
Before you can
use a variable in
C++, it must be
defined.
When you defined a
variable, the compiler
reserves a location
inside the computer
memory for the
variable value.
You can define a
variable anyplace
within a program
as long as it is
defined before it is
used.
How to declare variable?
27. 1.2.3 NAME THE VARIABLES ACCORDING THE STANDARDS.
Abu ! = abu
charA9 ; //Correct
char 9A; //Incorrect
char_ali ; //Correct
char_9A; //Correct
char Nama1; //Correct
char Nama 1; //Incorrect
char Nama1! ;//Incorrect
Length is depend on the type of compiler.
Difference compiler may have different
rules. As an example, Microsoft VC++
allowed until 32 character long for the
identifier Name.
Case-sensitive
start with a Letter
or Underscore
No spaces or
punctuation
Cannot use
KEYWORD
Length
char public; //InCorrect
char void1; //Incorrect
char main! ;//Incorrect
Rules of Naming Variables
28. 1.2.4 EXPLAIN THE DATA TYPES.
While writing program in any language, you need to use various variables to store various information.
Variables are nothing but reserved memory locations to store values. This means that when you create a
variable you reserve some space in memory.
You may like to store information of various data types like character, wide character, integer, floating point,
double floating point, boolean etc. Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory.
29. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
30. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
byte is the minimum amount
of memory that we can
manage in C++.
1 byte = 8 bit
the computer has to know
what kind of data we want to
store in them
Things to be consider ??
defines which kind of data will store in variables and also defines memory storage of data
.
Data type concept
31. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
A byte can store a relatively small amount of data.
One single character or a small integer (generally an integer between
0 and 255).
The computer can manipulate more complex data types
By forming grouping of several bytes
e.g; long numbers or non-integer numbers.
.
Data type constraints
Why ?
32. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
Data type size in byte
33. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
Data type range
34. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
Data type size & range Name Description Size* Range*
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int
(short)
Short Integer 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int
(long)
Long integer 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of
two values: true or false.
1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point
number.
8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating
point number.
8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t Wide character. 2 or 4
bytes
1 wide character
35. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
Integer
Floating
point
Character
Boolean
String
36. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
a. Integer
int is used to declare integers, whole numbers either positive, negative or 0.
Examples :1, 4, 67, 89, 98, 344, 2777, 0, - 3, -89, -999
The following statement shows how the variables of int type are declared.
Examples :
int var1;
int b = 10;
37. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
a. Integer
38. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
b. Floating point
Floating point include all of the whole number that must be represented using decimal point/decimal
places.
Example 1 : 3.14, 2.90, 0.0, -8.98, -99.7
Example 2 :
13200 = 1.32 x 104 = 1.32e4
0.00000045 = 0.45 x 10-6 = 1.45e-6
-13330000 = -1.333 x 10-7 = -1.333e7
This keyword float and double is used to declare floating point decimal numbers. A sample declaration would
be:
float var2; //Sample declaration for float
double var2 = 18.52;
39. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
b. Floating point
40. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
c. Character
All of the symbols on your keyboard are character. Includes lowercase or uppercase A to z, punctuation,
numbers and special symbols.
Example 1 : a, A, h, H, z, Z, &, !, 2, 3, <,+.@, #
This keyword is used to declare characters. The characters that can be used with this data type are ASCII
characters.
char a[9]=“Malaysia”;
char a[]=“Malaysia”;
char noPhone[11]=“0134022331”;
Character VS String
Character = ‘a’, ‘d’, ‘D’. Using single quote ‘
String = “ Hello”, “Abu?”. Using double quotes “
41. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
c. Character
42. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
d. Boolean
The result of a comparison or a logical association using AND or
OR is a boolean value, which can be true or false.
C++ uses the bool type to represent boolean values.
An expression of the type bool can either be true or false,
where the internal value for true will be represented as the
numerical value 1 and false by a zero.
43. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
d. Boolean
To declare a boolean variable, we use the keyword bool.
bool bValue;
When assigning values to boolean variables, we use the keywords true and false.
bool bValue1 = true;
44. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
e. String
A string is a series of characters treated as a single unit.
A string may include letters, digits and various special characters such as +, -, *, / and $.
String literals, or string constants, in C++ are written in double quotation marks.
45. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
e. String Must include header file string.h
#include <iostream.h>
#include <string.h>
Int main()
{
……
…..
string mystring = "This is a string";
……
return 0;
}
46. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
Example :
15.25F - float
1000.5L - Long double
25000L - Long integer
25 - integer
Data Type ?
Data Type ?
Data Type ?
Data Type ?
Did you know that
long,short,signed and
unsigned are called qualifier
purpose of qualifier :
to manipulate the range of a
particular data type or its size
size qualifiers :
long and short
sign qualifiers :
signed and unsigned
47. 1.2.5 LIST THE DATA TYPES AND THEIR RESPECTIVE RANGE WITH
EXAMPLES
Things to ponder :
Integer data type :
int is normally 2 bytes
with range of 0 to 255
What will happen if ?
1. you declare it as long int
2. you declare it as short int
3. you declare it as unsigned int
1. size will increase to 4 bytes
2. size will reduce to 1 byte
3. range increase from 0 to 65535