0% found this document useful (0 votes)
3 views4 pages

Lab 1 Handout Summer 2024

This lab introduces students to the basics of C++ programming, including setting up the Code Blocks IDE, creating a console application, and understanding the compilation process. Students will learn to write, modify, and execute a simple C++ program that prints messages to the console. The lab also includes tasks to enhance their program and explore the effects of certain coding practices.

Uploaded by

khushilikespak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Lab 1 Handout Summer 2024

This lab introduces students to the basics of C++ programming, including setting up the Code Blocks IDE, creating a console application, and understanding the compilation process. Students will learn to write, modify, and execute a simple C++ program that prints messages to the console. The lab also includes tasks to enhance their program and explore the effects of certain coding practices.

Uploaded by

khushilikespak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 1: The Anatomy of your first C++ Program ∗

1 Objectives
After completing this lab, students would be able to:
1. Set up the computing environment for carrying out their lab tasks by downloading and installing
the Code Blocks integrated development environment (IDE).
2. Create a simple console application using the Code Blocks IDE.
3. Compile their C++ code, link or build their object code into executable files and load and run their
C++ applications.
4. Understand the different parts of their code and make small changes to it.

2 Your First C++ Program


Computer programs are instructions for a computer that are written to solve problems using computers.
Before you start writing a computer program, you must be very clear about the problem that you want
to solve, and you must develop a sequence of steps or operations to be followed by the computer to solve
the problem. We can call this sequence of steps an algorithm. Once you have figured out the algorithm
for solving a particular problem, you can write it in simple English, but in that form it won’t be of any
good for the computer. The computer won’t be able to execute it. When you want to implement an
algorithm, i.e., when you want the algorithm to be followed (implemented or executed) by a computer,
you must write it in a language that the computer can understand. Computers are electronic, digital
devices and can only understand the machine language, which is nothing but strings of 1’s and 0’s or
binary numbers. However, it’s not very efficient for us to program or write instructions for a computer in
binary. For us, it is more efficient to program computers using a language that is similar to the
English language. Such languages are called high-level programming languages as they are closer to
the languages that we naturally use for expressing ideas, whereas the machine language is a low-level
programming language. Examples of high-level languages are C, C++, Java, Python etc. In this
course, we will learn how to program a computer using the C++ programming language.
The process of coding any algorithm involves the following activities:
1. Editing: It is done using a text editor such as Notepad or the one provided by an IDE. An IDE is
called an integrated development environment because it integrates all the softwares that are
required for editing, compiling, linking and loading your programs. So naturally, every IDE
comes with an Editor software. Editing allows you to create your C++ source files. They are
called source files because they store our C++ source code. These files are plain text files and
usually have the file extension .cpp or .cc.
2. Compiling: It is the translation of our C++ source code into machine code. The machine code
generated by the compiler is also called the object code. The object code is saved in files that
usually have the extension .o or .obj depending upon the compiler. CodeBlocks stores the object
code in a separate directory named obj. You can locate this directory

1
in your project directory. Although the object code generated by the compiler is machine code, it
cannot be executed as it is. It needs to be linked to the object code from different libraries that we
need to run an application on Windows. Moreover, before the C++ source code is compiled, it is
preprocessed by the compiler. The preprocessing of C++ source files usually involves the
replacement of certain elements in your source files with source code that is stored in other files.
The compiler is issued preprocessor directives at the very beginning of the program. This will be
discussed in more detail in the coming text.
3. Linking or Building: It is the process of linking your object code with the object code in pre-
compiled libraries. The C++ language comes with a standard library that includes pre-compiled
code for many frequently used activities, for example, interacting with the user by getting the
user’s input and displaying the program’s output. The process of linking is also called building. The
linker creates the executable file, which has the file extension .exe. This file resides in the bin
directory in the project folder and can be loaded into the computer’s memory for execution either
by double-clicking it or by selecting the Build→ Run item from the Build menu.

You can create a new project in Codeblocks using the Console Application template. When you
create a new project in Codeblocks using one of the many available templates, you are provided with the
necessary source files and startup code. You can create a new project using the Start Page by clicking
the Create a new Project hyperlink on the Start Page. If you can’t see the Start Page, you can go to the
View Menu and check the Start Page option there to enable it in your current view. You can also create
a new project by going to the File Menu and selecting the New → Project item. The startup code
provided by the Codeblocks IDE is given below. The formatting of the code given below and the one
that you would see in the Codeblocks editor may differ.
1 #i ncl ude <i ostr eam >
2 usi ng namespace std;
3
int main ()
4
{
5
6
cout << "Hello , w orl d !" << endl ;
7
retur n 0;
}

Listing 1: Your first C++ program

Line 1 of the program starts with the # symbol. Lines that begin with a # symbol are preprocessed
by the compiler before translating the source code into machine code. #include is a preprocessor directive
that instructs the C++ compiler to copy the contents of the iostream, i.e., the input-output stream, header
file in the current program. The iostream header file is required for the input and output of data from
and to the console screen, respectively. Hence, always remember to include line 1 in all your programs
whenever you intend to interact with the program’s user through the console window.
Line 2 of the program starts with the keyword using. Keywords are reserved words that cannot be
used as names for variables and functions. We’ll discuss variables and functions in greater detail in the
future. Line 2 of the program specifies that we want to use the standard or std namespace. We have to
write Line 2 the way it is written in the code above. We cannot write it as using std namespace as it
would violate the grammar of C++. The keyword namespace is used to specify the namespace that we
want to use. We need to use namespaces when we want to use external code libraries. We’ll discuss it
in greater detail later. For now, just don’t forget to include this line in your code. If you don’t want to
include this line in your code, then in Line 5 you will need to write std::cout instead of the simple cout.
Further, note that Line 2 ends with a semi-colon;, whereas Line 1 does not end with any semi-colon.
This is because Line 1 is not a C++ statement, rather it is a preprocessor directive, whereas, Line 2 is a
C++ statement. Every C++ statement must end with a semicolon ; to explicitly

2
specify the end of that statement. A consequence of this requirement is that a C++ statement can span over
multiple lines.
Line 3 of the program, i.e., int main(), is the main function or block of code of our program. Every C++
program must include at least one such function. The loader searches for this function in every program
and the execution of every program starts from the main() function. The keyword int specifies that the
main() function would return an integer value to its caller, i.e., the operating system. Hence, there must
be a return statement as the last statement of the main() function, which would return some integer
value to the caller. The return value is usually used to specify whether a program is executed
successfully or not. It must be remembered that C++ is a case-sensitive language, i.e., main and Main are
considered different. The use of Main would result in an error. Similarly, int and Int are different. The
keyword int is used to specify the type of data that can be saved in a given memory location or the type
of data that would be returned by a given function, whereas Int is not recognized by the compiler. The
user must define it before using it.
The left curly brace { at line 4 specifies the beginning of a block of code. A block of code is simply a
group of C++ statements. Here, the block of code is the function main(). The body of every function
must begin with a left curly brace {. Similarly, the right curly brace } is used to specify the termination
of a block of code. The right curly brace } at line 7 specifies the termination of the body of the main()
function. The left and right braces always occur in pairs, i.e., there must be a corresponding right brace
} for each left brace {.
Line 5 prints the text enclosed in the double quotation marks on the console screen. When you run this
program the console screen should display the text Hello, World!. The double quotation marks won’t
appear on the console screen, rather, the text within these quotation marks would be printed. The text
within the double quotation marks is referred to as a string literal or simply a string or character
string. cout refers to the standard output stream object and is used for printing text on the console
window. ≪ is the stream insertion operator. Any string that we want to print to the console screen must
appear after the stream insertion operator ≪. Line 5 is a C++ statement, which is why it is terminated
by a semi-colon. The stream insertion operator ≪ can be used multiple times in a C++ statement. In
Line 5, it is used twice, i.e., first to print the string Hello, World! on the console screen and then to
move the cursor to the next line by printing endl or end line.
We can introduce changes to the program given above.
1 #i ncl ude <i ostr eam >
2 usi ng namespace std;
3
int main ()
4
{
5
6
cout << "Hello , C o mputer Pr og r ammi ng Class of 2024! " << endl ;
7 cout << "Wel come to C omput er Pr og r ammi ng !" << endl ;
8 cout << "I hope you will enjoy l ear ni ng it." << endl ;
9 retur n 0;
}
Listing 2: Changes in your first C++ program

The modified program will now print three strings on the console screen. You will notice that all three
strings appear on separate lines. This is because, after each string, we also insert the endl on the
standard output stream, which inserts the new line character after each string. The new line character
ensures that the next string will be printed on a new line.

3 Your Tasks
1. Create your first console application, just like the way we did in our first session. Name your
project as YourNameFirstProject.
2. Make changes to your first program as shown in code listing 2. Compile, build, and run the
modified program.
3. Add three more C++ statements to your program. The first statement should introduce you by
printing something like I am Shahbaz Khan. on the console screen. Replace Shahbaz Khan with
your name. The second statement should say something about your

3
background in programming by printing a statement like This is the first time I heard the name
C++. You can be innovative here and choose words of your choice. The third statement should
say something about your excitement or otherwise about this course by printing a statement as I
look forward to learning programming.
4. Remove ≪ endl from all the statements. Save your modified source code and then compile, build,
and run it. How does the output differ from the previous output? What do you think is the effect of
≪ endl on the way the text is printed on the console screen?
5. Print your date of birth on the console screen using something similar to the following two
statements. What is the difference between the output that you see on the console screen as a
result of these two statements?

cout<<"01122001"<<endl;

cout<<01122001<<endl;

6. Add the following C++ statement to your code exactly the way it appears below and then try to
compile, build, and run your code. What happens when you try that? Can you try to explain why?

cout<<Good bye<<endl;

You might also like