Professional Documents
Culture Documents
Lab Experiment # 06: Introduction To C++
Lab Experiment # 06: Introduction To C++
INTRODUCTION TO C++
OBJECTIVES:
Run the INSTALL program from DISK 1. To start the installation, change
your current drive to the one that has the install program on it and
type INSTALL. You will be given instructions in a box at the bottom of
the screen for each prompt. For example, if you will be installing from
drive A:, type:
A:
INSTALL
At this point, the INSTALL program will appear with menu selections
and descriptions to guide you through the installation process.
THEORY:
main() This marks the point where the C programs begins execution.
Required for all programs.
() Must appear immediately after main. Information that will be used by the program
is usually contained within that braces.
{} The braces are required in all C programs. They indicate the beginning and the end
of the program instruction.
Cin Function:
The cin function allows your program to get user input from the keyboard. A typical
structure for this funcion is
cin>>variable;
where >> is extractor operator. It is used to extract informaton from the input stream.
Example:
#include<iostream.h>
main()
{
float vol,cur,res;
cout<<”enter the value of the current”;
cin>>cur;
cout<<”enter the value of the resistance”;
cin>>res;
vol = cur * res;
cout<<”voltage is “<<vol;
}
Observe the output.
OUTPUT:
Cout Function:
The cout function is used to write information to standard output(normally your monitor).
It can be used to print numbers, string or characters. A typical structure of this function is
Where the << symbol represents the insertor function. This function is used to insert
Characters or numbers in to the standard output stream. Function prototype for cout
Statement is in the iostream.h header file, so it must be include in to the program.
Example:
#include<iostream.h>
main()
{
cout<<2; // DISPLAY NUMBER.
cout<<”HELLO”; // DISPLAY STRING.
cout<<’a’; // DISPLAY CHARACTER.
}
Observe the output.
Output:
COMMENTS
C++ supports single-line and multi-line comments. All characters available inside any
comment are ignored by C++ compiler. C++ comments start with /* and end with */. For
example:
/* This is a comment */
A comment can also start with //, extending to the end of the line. For example:
#include <iostream>
void main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
When the above code is compiled, it will ignore // prints Hello World and final executable
will produce the following result:
Hello World
ESCAPE CHARACTERS
There are certain characters in C++ when they are preceded by a backslash they will have
special meaning and they are used to represent like newline (\n) or tab (\t). Here, you have a
list of some of such escape sequence codes:
Escape sequence Meaning
\\ \ character
\a Alert or bell
\b Backspace
\t Horizontal tab
\n Newline
#include <iostream>
void main()
{
cout << "Hello\tWorld\n\n";
return 0;
}
When the above code is compiled and executed, it produces the following result:
Hello World
VARIABLES:
Computer programs manipulate (or process) data. A variable is used to store a piece of
data for processing. It is called variable because you can change the value stored.
More precisely, a variable is a named storage location, that stores a value of a particular
data type. In other words, a variable has a name, a type and stores a value.
• A variable has a name (or identifier), e.g., radius, area, age, height. The name is
needed to uniquely identify each variable, so as to assign a value to the variable
(e.g., radius=1.2), and retrieve the value stored (e.g., area = radius*radius*3.1416).
• A variable has a type. Examples of type are,
int: this type declares a signed integers (whole numbers) such as 123 and -
456;
double: this type declares floating-point or real numbers such as 3.1416, -
55.66, having a decimal point and fractional part.
Char: this type declares a variable whose data value represents a character
such as char lock.
• A variable can store a value of that particular type. It is important to take note that a
variable in most programming languages is associated with a type, and can only
store value of the particular type. For example, a int variable can store an integer
value such as 123, but NOT real number such as 12.34, nor texts such as "Hello".
The following diagram illustrates two types of variables: int and double. An int variable
stores an integer (whole number). A double variable stores a real number.
Example
Try the following example where a variable has been declared at the top, but it has been
defined inside the main function:
#include <iostream>
void main()
// Variable definition:
int a, b;
int c;
float f;
// actual initialization
a = 10;
b = 20;
c = a + b;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}
When the above code is compiled and executed, it produces the following result:
30
23.3333
IDENTIFIERS:
An identifier is needed to name a variable (or any other entity such as a function or a class).
C++ imposes the following rules on identifiers:
• An identifier is a sequence of characters, of up to a certain length (compiler-dependent,
typically 255 characters), comprising uppercase and lowercase letters (a-z, A-Z),
digits (0-9), and underscore "_".
• White space (blank, tab, new-line) and other special characters (such as +, -, *, /, @, &,
commas, etc.) are not allowed.
• An identifier must begin with a letter or underscore. It cannot begin with a digit.
Identifiers beginning with an underscore are typically reserved for system use.
• An identifier cannot be a reserved keyword or a reserved literal
(e.g.,int, double, if, else, for).
• Identifiers are case-sensitive.
A variable name is a noun, or a noun phrase made up of several words. The first word is in
lowercase, while the remaining words are initial-capitalized, with no spaces between words.
Forexample, thefontSize, roomNumber, xMax, yMin, xTopLeft and thisIsAVeryLongVaria
bleName. This convention is also known as camel-case.
Recommendations
Local variables:
In C, all variables must be declared before they can be used. In fact, some variables must be
declared at the beginning of the block in which they appear-these are called local variables.
In C, a block is any subsection of a program beginning with the open brace character, { and
ending with the closing brace character,}.
For example: in the following program echo is the local variable.
#include<stdio.h>
int main()
{
Int echo;
Scanf(“%d”,&echo);
Printf(“%d\n”, echo);
}
Global variables:
In contrast to local variables, which can only be accessed within the block in which they are
declared, global variables can be accessed throughout the program.
For example: in the following program globalvar is the global variable.
#include<stdio.h>
Int globalvar=2;
int main()
{
Int localvar=3;
Printf(“global %d local %d\n,globalvar,localvar);
}
Lab Assignment:
Task#1: Write a program in C++ to display the first alphabet of your name?
Task#2: Write a program that will compute the area of a circle. The user must enter the
radius of the circle. Use the following formula for area A = 3.14 R2
Task#3: Write a program that will solve for the power dissipation of a resistor when the
voltage across the resistor and the current in the resistor are known as I=5A and
R=16ohm. The relationship for power dissipation is :
P=I2x R
Where
P = Power dissipated
I = resistor current.
V= resistor voltage.
Task#4: Next, you should modify the program so that it produces output similar to the
following (see the steps below for how to do this):
a)
*
* *
* *
* *
* *
* *
*
b)
--------------- c)
| | $
| | $ $
| | $ $
| | $ $
| | $ $
---------------