Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Lab – 1 : Introduction to C++

C++ Get Started


To start Programming in C++, you need two things:

 A text editor, like Notepad, to write C++ code


 A compiler, like GCC, to translate the C++ code into a language that the computer
will understand

There are many text editors and compilers to choose from such as CodeBlocks, DevC++,
Visual Studio Express Edition. All these are free to use and you can download and install any
of these. In this Lab, we shall use Dev-C++ IDE (Integrated Development Environment) and
you can download it from https://sourceforge.net/projects/orwelldevcpp/. An IDE is used to
write, edit, manage and compile the code.

Compilation Process
A Source code written in a programming language is converted to instructions which
computer can understand. This process is known as compilation. The process work as

Object files are intermediate files that represent an incomplete copy of the program: each
source file only expresses a piece of the program, so when it is compiled into an object file,
the object file has some markers indicating which missing pieces it depends on. The linker
takes those object files and the compiled libraries of predefined code that they rely on, fills
in all the gaps, and spits out the final program, which can then be run by the operating
system (OS). The compiler and linker are just regular programs. The step in the compilation
process in which the compiler reads the file is called parsing.

Setting up the Project


1. Run Dev-C++ IDE.

Fundamentals of Programming 1
Lab – 1 : Introduction to C++

2. Click File > New > Project.

3. Write Name: Lab-1, Select Console Application from Basic Tab and Press OK.

Fundamentals of Programming 2
Lab – 1 : Introduction to C++

4. Select the destination folder and click Save Button.

5. The IDE creates the project and opens a main.cpp File. Press Ctrl+S, and save the
file.

Fundamentals of Programming 3
Lab – 1 : Introduction to C++

Fundamentals of Programming 4
Lab – 1 : Introduction to C++

6. The buttons marked within red rectangle compile and run the program. Keyboard
Shortcut for these actions is
a. F9: Compile
b. F10: Run
c. F11: Compile & Run
d. F12: ReBuild All

7. Press Compile and Run Button and the console shall show following output.

8. Press any key to exit the Console Output Window.

9. If some error occours while compiling the code, IDE shows it as following

Fundamentals of Programming 5
Lab – 1 : Introduction to C++

Hello World
Clear the code written in main.cpp file, and write and run the following code.

Upon successful compilation, the program shall show following output

Tokens

Ser Token Type Description Example


1. Words with special meaning to the int, double,
Keywords
compiler for, auto
2. Names of things that are not built into cout, std, x,
Identifiers
the language myFunction
3. Basic constant values whose "Hello,
Literals value is specified directly in the source world!", 24.3,
code 0, ’c’
4. Operators Mathematical or logical operations +, -, &&, %, <<
5. Punctuation/ Punctuation defining the structure of a
{ } ( ) , ;
Separators program
6. Spaces, tabs,
Spaces of various sorts; ignored by the
Whitespace newlines,
compiler
comments

Fundamentals of Programming 6
Lab – 1 : Introduction to C++

Hello World – Code Explanation

# Description
1 // indicates that everything following it until the end of the line is a comment: it is
ignored by the compiler. Another way to write a comment is to put it between /* and */
(e.g. x = 1 + /*sneaky comment here*/ 1;). A comment of this form may span multiple
lines. Comments exist to explain non-obvious things going on in the code. Use them:
document your code well
2 Lines beginning with # are preprocessor commands, which usually change what code
is actually being compiled. #include tells the preprocessor to dump in the contents of
another file, here the iostream file, which defines the procedures for input/output
4 int main(int argc, char** argv) {…} defines the code that should execute when the
program starts up. The curly braces represent grouping of multiple commands into a
block. Other alternatives are void main(){…} or int main{…} etc. We shall discuss
each case in detail later in the semester.
5  cout << (pronounced as “see-out”) – This is the syntax for outputting some piece
of text to the screen. It is prefixed with word std::, which defines its context. The
operator :: is called scope resolution operator. In C++, identifiers can be defined
within a context – called a namespace. Here, we’re telling the compiler to look for
cout in the std namespace, in which many standard C++ identifiers are defined.
The text written in double-quotes (“…”) is called string.
 The \n indicates a newline character. It is an example of an Escape Sequence.
7 return 0 indicates that the program should tell the operating system it has completed
successfully. This syntax will be explained in the context of functions; for now, just
include it as the last line in the main block.

The C++ compiler skips the whitespaces so following two programs gives the same output.
But it is a good practice to properly indent and comment your code. Each line of code
(known as statement) shall end with a semi-colon ;.

#include <iostream>

int main(int argc, char** argv) {

std::cout<<"Hello World!\n";

return 0;
}

#include <iostream>
int main(int argc, char** argv) { std::cout<<"Hello World!\
n"; return 0; }

NameSpace

Fundamentals of Programming 7
Lab – 1 : Introduction to C++

In the above Hello World example, A cleaner alternative is to add the following line below
line 2:
using namespace std;
This line tells the compiler that it should look in the std namespace for any identifier we
haven’t defined. If we do this, we can omit the std:: prefix when writing cout. This is the
recommended practice. So now the program looks like

Run the code shown in photo above and see the output.
Lab Practice(Code): Write the Following two Programs and see the output.
# Code
1. #include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout<<"Hello World!\n";
cout<<"I am Learning C++"<<endl;
cout<<"It is easy";
return 0;
}
Remarks. The keyword endl is called manipulator and its
function is similar to escape sequence ‘\n’;
2. #include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout<<"Hello World!";
cout<<"I am Learning C++";
cout<<"It is easy";
return 0;
}
Remarks. Now all three sentences are written in single
line on the output screen. Because we did not included
new line escape sequence ‘\n’ or endl manipulator in the
code.

Fundamentals of Programming 8
Lab – 1 : Introduction to C++

Escape Sequence. Following is the list of escape sequences which can be used in strings.
Practice each escape sequence by writing it at different places in the above example of Hello
World and see the change in output.

Escape Sequence Represented Character


\a System bell (beep sound)
\b Backspace
\f Formfeed (page break)
\n Newline (line break)
\r “Carriage return” (returns cursor to start of line)
\t Tab
\\ Backslash
\’ Single quote character
\" Double quote character
\some integer x The character represented by x

Lab Practice (Errors): Write following codes in IDE, compile it, read the error shown by the
debugger, correct the errors and see output.
# Code
1. #include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout<<"Hello World!."
cout<<"Test Example";
cout<<"Showing Error"
return 0;
}
Remarks. Each statement should end with a semi-colon.
2. #include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout<<"Hello World!.";
cout<<"String Example’;
cout<<’Showing Error";
return 0;
}
Remarks. A string should be enclosed in double quotes.
3. #include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout<<"Hello World!."
cout<<"This is \”Test Example\";
return 0;
}
Remarks. A string should be enclosed in double quotes.

Fundamentals of Programming 9
Lab – 1 : Introduction to C++

Lab Practice (Output): Write the C++ codes to produce following outputs.

# Output
1

Fundamentals of Programming 10
Lab – 1 : Introduction to C++

References / Resources
1. Lecture Notes : Massachusetts Institute of Technology (MIT)
2. https://www.w3schools.com/cpp/

Fundamentals of Programming 11

You might also like