Uploaded by Tiger Siblings

c++ lab 1

advertisement
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Getting started with Dev C++ 5.0
Objectives:
This lab has been designed to
 Guide you with Dev C++ 5.0 installation.
 Get you familiarize with the Dev C++ environment.
 Understand the Basics of C++ Programming
Dev-C++ Tutorial
Home - Dev-C++ Official Website (bloodshed.net). Click the link and follow the instructions to install
the program. The following screenshots will help you install and run the product:
Installer Language
Select English, if not selected, and then click the OK button to continue
License Agreement
Click on the "I Agree" button to continue
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Choose Components
Make sure that the type of install is Full and click the Next button to continue
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Choose Install Location
Click the Install button to continue
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Installing
Click the Yes button
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Finished
Click the Finish button to finalize the installation and run the program
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
First Time Configuration
Click the Next button to continue
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
First Time Configuration
Click the Next button to continue
First Time Configuration
Wait for the progress bar to complete
First Time Configuration
Click the OK button to finalize
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
New Project Menu
Click the File menu, then select the New menu item and click the Project menu item
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
New Project
On the top, make sure the Basic tab is selected and under the Basic tab, select "Console
Application"
Give a name to your project using the Name text box, for instance, "HelloWorld".
Important: Choose "C Project" under "Project Options" on the left
Click the OK button to create your project
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Create New Project
Give a name to your project file and click the Save button to continue
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Compile and Run
Clic"Compile & Run" menu item or the icon displayed in the below screenshot or just press F9 to compile and
run your program
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Running
Assuming you did not make any syntax errors on your code, you should see a similar output
window running your program
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Compile failed
If you try to compile a code which has syntax errors, Compiler window lists the errors with their line
numbers
You can double click the error and see the error highlighted in the code
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Above steps are the necessary steps in order to compile and run C programs with Dev C++. However,
you should make sure your code is compliant with ANSI C standard. You should change compiler
options to force strict ANSI C checking.
Follow the below steps to configure your compiler:
Compiler Options Menu Click the "Compiler Options" menu item under the Tools menu
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Compiler Options
First, select "Add the following commands when calling compiler" option. Then, write "-ansi"
in the text box without the quotation marks. Click the Settings tab on the top and goto the next
step
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Compiler Options
Select "C Compiler" on the left. Select Yes for "Support all ANSI standard C programs" on the
right
Click the OK button to complete
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
Introduction to C++
C++ is a high level language with certain low level features as well. C++ is a superset
of C. Most of what we already know about C applies to C++ also. However, there are some
differences that will prevent C programs to run under C++ compiler. C++ programs differ from
C programs in some important respects. Most of the differences have to do with taking
advantage of C++'s object-oriented capabilities. But C++ programs differ from C programs in
other ways, including how I/O is performed and what headers are included. Also, most C++
programs share a set of common traits that clearly identify them as C++ programs. Before
moving on to C++'s object-oriented constructs, an understanding of the fundamental elements
of a C++ program is required.
The New C++ Headers
When you use a library function in a program, you must include its header. This is done using
the #include statement. Standard C++ still supports C-style headers for header files that you
create and for backward compatibility. However, Standard C++ created a new kind of header
that is used by the Standard C++ library.
The new-style C++ headers are an abstraction that simply guarantee that the appropriate
prototypes and definitions required by the C++ library have been declared. Since the new-style
headers are not filenames, they do not have a .h extension. They consist solely of the header
name contained between angle brackets. For example, here are some of the new-style headers
supported by Standard C++.
<iostream> <fstream> <vector> <string>
Namespaces
When you include a new-style header in your program, the contents of that header are contained
in the std namespace. A namespace is simply a declarative region. The purpose of a namespace
is to localize the names of identifiers to avoid name collisions. Elements declared in one
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
namespace are separate from elements declared in another. Originally, the names of the C++
library functions, etc., were simply put into the global namespace (as they are in C). However,
with the advent of the new-style headers, the contents of these headers were placed in the std
namespace.
I/O Operators
cout << “This is output. \n”;
This statement introduces new C++ features, cout and <<. The identifier cout is a predefined
object that represents output stream in C++. The standard output stream represents the screen.
The operator << is called the insertion operator. It inserts (or sends) the contents of the variable
on its right to the object on its left.
Note that you can still use printf( ) or any other of C's I/O functions in a C++ program.
However, most programmers feel that using << is more in the spirit of C++. Further, while
using printf( ) to output a string is virtually equivalent to using << in this case, the C++ I/O
system can be expanded to perform operations on objects that you define (something that you
cannot do using printf( )).
The statement cin>>num1; is an input statement and causes program to wait for the user to
type in number. The identifier cin is a predefined object in C++ that corresponds to the standard
input stream. Here stream represents the keyboard. The operator >> is known as extraction or
get from operator. In general, you can use cin >> to input a variable of any of the basic data
types plus strings.
Directives
The two lines that begin the first program are directives. The first is a preprocessor directive,
and the second is a using directive.
1)
Preprocessor Directives
#include <iostream>
The preprocessor directive #include tells the compiler to insert another file into your source
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
file.
2)
Using Directives
The C++ program can divided into different namespaces. A namespace is a part of the program
in which certain names are recognized: outside of the namespace they are unknown.
using namespace std;
The second method
Std::cout <<”welcome”;
To avoid adding std:: dozens of times in programs we use the using directive instead.
Arithmetic Operators
C++ supports the following arithmetic operators for numbers: short, int, long, long char
Operator
*
Description
Multiplication
Usage
expr1 * expr2
Examples
2 * 3 → 6; 3.3 * 1.0 → 3.3
/
Division
expr1 / expr2
1 / 2 → 0; 1.0 / 2.0 → 0.5
%
Remainder (Modulus)
expr1 % expr2
5 % 2 → 1; -5 % 2 → -1
+
Addition
expr1 + expr2
1 + 2 → 3; 1.1 + 2.2 → 3.3
-
Subtraction
expr1 - expr2
1 - 2 → -1; 1.1 - 2.2 → -1.1
(treated as 8-bit signed integer), unsigned short, unsigned int, unsigned long, unsigned long,
unsigned char, float, double and long double.
All the above operators are binary operators, i.e., they take two operands. The multiplication,
division and remainder take precedence over addition and subtraction. Within the same
precedence level (e.g., addition and subtraction), the expression is evaluated from left to right.
For example, 1+2+3-4 is evaluated as ((1+2) +3)-4.
It is important to take note that int/int produces an int, with the result truncated, e.g., 1/2 → 0
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
(instead of 0.5).
Take note that C/C++ does not have an exponent (power) operator ('^' is exclusive-or, not
exponent).
Building Blocks of Programming Language:
In any language there are certain building blocks:

Constants

Variables

Operators

Methods to get input from user (scanf( ), getch( ) etc.)
Methods to display output (Format Specifier, Escape Sequences etc.) and so on.
Variables and Constants
If the value of an item can be changed in the program then it is a variable. If it will not change
then that item is a constant. The various variable types (also called data type) in C are: int,
float, char, long etc. For constants, the keyword const is added before declaration.
Operators
There are various types of operators that may be placed in three categories
Escape Sequences
Escape Sequence causes the program to escape from the normal interpretation of a string, so
that the next character is recognized as having a special meaning. The back slash “\” character
is called the Escape Character”. The escape sequence includes the following:
LAB MANUAL # 1
Course: NUMERICAL ANALYSIS AND COMPUTER
PROGRAMING (MA-204)
Session: 19-CE
A Sample C++ Program
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "This is output.\n";
// this is a single line comment /* you can still use
C style comments */
//input a number using >>
cout << "Enter a number: ";
cin >> i;
//now, output a number using <<
cout << i << " squared is " << i*i << "\n";
return 0;
}
Notice that the parameter list in main( ) is empty. In C++, this indicates that main( ) has no
parameters. This differs from C. In C, a function that has no parameters must use void in its
parameter list. However, in C++, the use of void is redundant and unnecessary. As a general
rule, in C++ when a function takes no parameters, its parameter list is simply empty.
Download