Anda di halaman 1dari 14

Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

International Islamic University, Islamabad

EXPERIMENT # 01: INTRODUCTION TO C++ PROGRAMING AND DEV C++

Name of Student: ..
Roll No.:
Date of Experiment: ..
Report submitted on: ..

Marks obtained:

Remarks:

Instructors Signature: ...

International Islamic University Islamabad Page 1 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

General Guidelines for Lab usage

It is very important that you observe the following rules for the lab usage. These rules
are very important in order to maintain an environment in the lab that is conducive to
learning.
Please dont make noise in the lab. The lab is being used by a lot of students at
the same time. Please make sure that your behavior does not become a source
of inconvenience for anybody else.
Please make sure that you have seated yourself in the lab before it starts.
Attendance is compulsory for all the students and nobody will be allowed to
enter the lab after 15 minutes.
In case you want to leave the lab, raise your hand and seek the permission from
the Instructors present in the lab. Only one student is allowed outside the lab at
a given time
Please turn off your cell phones during the lab. You are not allowed to use your
cell phone during the lab.
Use of internet is not allowed during lab hours unless explicitly instructed by the
instructor
Please follow the deadlines for your lab/assignment submissions. No grades
would be awarded in case of late submission.
Please make sure that you delete your code from the lab PC after the lab is over,
so that it cannot be misused by someone else.
Plagiarism would not be tolerated under any circumstances. You will be given a
zero in such cases and your case would be forwarded to Disciplinary Committee
(DC) for further action.

International Islamic University Islamabad Page 2 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

C++ Programming Environment

To program in a language, we need software called an IDE (Integrated Development


Environment). An IDE consists of an editor to edit code and a compiler/linker to convert
your code to executable machine code and a debugger to find errors in a program.
Additionally, it may have a help system and other useful features.

The layout of C++ Programs:

The general form of a C++ program is as follows:


Preprocessor directives
Global declarations
main ()
{
Local variables to function main;
Statements associated with function main;
}

Listing 1.1
First Program - Hello World

#include<iostream>
Using namespace std;
int main()
{
Cout<<"Hello World";
return 0;
}

#include<iostream>
This tells the compiler to first include standard C++ library named iostream to our
project. IOSTREAM is an example of a header file (sometimes called an include file). Its
concerned with basic input/output operations, and contains declarations that are
needed by the cout identifier and the << operator .Without these declarations, the
compiler wont recognize cout and will think << is being used incorrectly.

int main()
This statement declares the main function. C++ program can contain many functions but
must always have one main function. A function is a self-contained module of code that
can accomplish some task. The "int" specifies the return type of main. In this case, 0 is
returned to the operating system.
{

International Islamic University Islamabad Page 3 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

This opening bracket denotes the start of the main function.

cout <<"Hello World";

cout is a function from a standard C++ library that is used to print strings to the standard
output, normally your screen. The compiler links code from these standard libraries to
the code you have written to produce the final executable.

Return 0;
The keyword return is a special statement which is used to exit a function and return a
value. This statement exits the function main indicating its completion and returns the
value 0 to the operating system indicating that the program successfully ran to
completion.
}
This closing bracket denotes the end of the main function.

Cout
On most program environments, the standard output by default is the screen, and the
C++ stream object defined to access it is cout.
For formatted output operations, cout is used together with the insertion operator,
which is written as << (i.e., two "less than" signs).
#include <iostream>
using namespace std;
void main ()
{
int x = 3;
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
}

International Islamic University Islamabad Page 4 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Escape Sequences
Character combinations consisting of a backslash (\) followed by a letter or by a
combination of digits are called "escape sequences." To represent a newline character,
single quotation mark, or certain other characters in a character constant, you must use
escape sequences. An escape sequence is regarded as a single character and is therefore
valid as a character constant. Escape sequences are used to format our output.
The following escape sequences can be used to print out special characters.
Escape Sequence Description
\n Newline

\t Horizontal tab

\\ Backslash

\' Single quote

\" Double quote


Listing 1.2
This programs shows the use of Newline Escape Sequence (\n)

#include<iostream>
int main()
{
cout<<"This\nis\na\ntest\n\nShe said, How are you?\n";
return 0;
}

Output
This
is
a
test
She said, How are you?
Listing 1.3
This programs shows the use of Horizontal tab Escape Sequence (\t)

#include<iostream>
int main()
{
Cout<<"This is a test \t She said, How are you?\n";
return 0;
}

Output
This is a test She said, How are you?

The gap between the sentences is due to Horizontal tab escape sequence.

International Islamic University Islamabad Page 5 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Listing 1.4

This programs shows the use of Newline Escape Sequence (\n) with Double quote
escape sequence (\)

#include<iostream>
int main()
{
cout<<"This is a test \nShe said,\"How are you?\"" ;
return 0;
}

Output:
This is a test
She said, How are you?

The purpose of the above programs is to give you an idea that escape sequences can be
used in any combination to format your output.

Using Directive:
A C++ program can be divided into different namespaces. A namespace is a part of the
program in which certain names are recognized; outside of the namespace theyre
unknown. The directive using namespace std; says that all the program statements that
follow are within the std namespace. Various program components such as cout are
declared within this namespace. If we didnt use the using directive, we would need to
add the std name to many program elements. e.g.

std::cout << "Welcome to this course\n";


To avoid adding std:: dozens of times in programs we use the using directive instead.

Comments

Comments are an important part of any program. They help the person writing a
program, and anyone else who must read the source file, understand whats going on.
The compiler ignores comments, so they do not add to the file size or execution time of
the executable program. Comments start with a double slash symbol (//)
and terminate at the end of the line. Theres a second comment style available in C++:
/* this is an old-style comment */
Arithmetic Operators:
There are following arithmetic operators supported by C++ language: Assume variable
A holds 10 and variable B holds 20, then:

International Islamic University Islamabad Page 6 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Show Examples

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by de-numerator B / A will give 2

Modulus Operator and remainder of


% B % A will give 0
after an integer division

Increment operator, increases integer


++ A++ will give 11
value by one

Decrement operator, decreases integer


-- A-- will give 9
value by one

International Islamic University Islamabad Page 7 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Listing 1.5:
#include <iostream>
using namespace std;

main()
{
int a = 21;
int b = 10;
int c ;

c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;

Output:
When the above code is compiled and executed, it produces the following result:

Line 1 - Value of c is :31


Line 2 - Value of c is :11
Line 3 - Value of c is :210

International Islamic University Islamabad Page 8 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Line 4 - Value of c is :2
Line 5 - Value of c is :1
Line 6 - Value of c is :21
Line 7 - Value of c is :22

Introduction to DEV C++

How to create a small program in Visual studio 2010.

Step 1:
First of all click on Start Menu All Programs Program files SoftwareBloodshed
Dev C++

This would launch the following window. Then closed the pop up window.

Step 2:

Go to File Menu New Project click

International Islamic University Islamabad Page 9 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

This would launch the following window. Remember this step is very important so you
have to be careful. Click on the Windows Console Application and then click on the
project name and write Lab1. Then click OK.

Step 3:

International Islamic University Islamabad Page 10 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

After clicking the OK in the step 2. You will see the following window.

Click on save. After that you will see another window.

Now start coding at the window.


Step 4:

International Islamic University Islamabad Page 11 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Write the following program in the window.

#include<iostream>
int main()
{
cout<<"Hello World";
return 0;
}

International Islamic University Islamabad Page 12 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Step 5:
Now execute the program by clicking on EXECUTECompile

Step 6:
After pressing compile the following window appears.

Omce there is no compilation error , go to Execute >Run following output window


appears .

International Islamic University Islamabad Page 13 of 14


Lab 01: INTRODUCTION TO C++ PROGRAMING , DEV C++

Your tasks

TASK 1
Write a program to print following information on screen. (Perform this task with cout )

Task 2
Write a program to draw shape of a boat on console.

International Islamic University Islamabad Page 14 of 14

Anda mungkin juga menyukai