0% found this document useful (0 votes)
51 views

PF Lab 03 2k21

The document provides information about a programming experiment on C++ basic data types and variables. It discusses the purpose and objectives of the experiment which are to introduce students to different C++ data types like integers, floats, characters, strings, and booleans and how to use variables of these types in a program. It also lists the software tools needed to complete the experiment and provides code examples demonstrating how to declare and use variables of different data types, take input, display output, and create constants in C++.
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)
51 views

PF Lab 03 2k21

The document provides information about a programming experiment on C++ basic data types and variables. It discusses the purpose and objectives of the experiment which are to introduce students to different C++ data types like integers, floats, characters, strings, and booleans and how to use variables of these types in a program. It also lists the software tools needed to complete the experiment and provides code examples demonstrating how to declare and use variables of different data types, take input, display output, and create constants in C++.
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/ 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

PROGRAMMING
FUNDAMENTALS

Experiment 3
C++ Basic Data Types and Variables

CLO 2. Use modern tools and languages for solving problems of


varying complexities
CLO 3. Construct the experiments/projects of varying complexities.
CLO 4. Demonstrate unique solution of problem under discussion.

1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Purpose:

This experiment provides an introduction to the C++ Basic Data Types and Variables.

Students will run and check different but related programs consisting of different C++ data

types and variables.

Objectives:

At the end of this experiment you will know:

1) The different data types and variables available in C++: how to use them in a C++

program.

2) Able to run and check different but related programs consisting of different C++ data

types and variables.

3) Implementation of C++ program using the right data type according to the required

logic.

Equipment and Components:

4) Dev-C++ 5.0 Beta 9.2 (4.9.9.2) 9.0 MB with Minge/GCC 3.4.2

5) Code::Blocks IDE16.01

2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

C++ Basic Input/Output


use the cin object to take input from the user, and the cout object to display output to the user
with the help of examples.

C++ Output
In C++, cout sends formatted output to standard output devices, such as the screen. We use the
cout object along with the << operator for displaying output.

Example 1: String Output

#include <iostream>
using namespace std;

int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}

Output

This is C++ Programming

How does this program work?

 We first include the iostream header file that allows us to display output.
 The cout object is defined inside the std namespace. To use the std namespace, we used
the using namespace std; statement.
 Every C++ program starts with the main() function. The code execution begins from the
start of the main() function.
 cout is an object that prints the string inside quotation marks " ". It is followed by the <<
operator.
 return 0; is the "exit status" of the main() function. The program ends with this
statement, however, this statement is not mandatory.

Note: If we don't include the using namespace std; statement, we need to use std::cout
instead of cout.

#include <iostream>

3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}

Example 2: Numbers and Characters Output

To print the numbers and character variables, we use the same cout object but without using
quotation marks.

#include <iostream>
using namespace std;

int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';

cout << num1 << endl; // print integer


cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
}

Output

70
256.783
character: A

Notes:

 The endl manipulator (that is used to change the formatting of output) is used to insert a
new line. That's why each output is displayed in a new line.
 The << operator can be used more than once if we want to print different variables, strings
and so on in a single statement. For example:

cout << "character: " << ch << endl;

C++ Input

4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

In C++, cin object takes formatted input from standard input devices such as the keyboard. We
use the cin object along with the >> operator for taking input.

Example 3: Integer Input/Output

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}

Output

Enter an integer: 70
The number is: 70

In the program, we used

cin >> num;

to take input from the user. The input is stored in the variable num. We use the >> operator with
cin to take input.

C++ Taking Multiple Inputs


#include <iostream>
using namespace std;

int main() {
char a;
int num;

cout << "Enter a character and an integer: ";


cin >> a >> num;

cout << "Character: " << a << endl;


cout << "Number: " << num;

5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

return 0;
}

Output

Enter a character and an integer: F


23
Character: F
Number: 23

C++ Variables and Data Types


Variables are containers for storing data values. Data types are declarations for variables. This
determines the type and size of data associated with variables. For example,

int age = 13;

In C++, there are different types of variables (defined with different keywords), for example:

 int - stores integers (whole numbers), without decimals, such as 123 or -123
 double - stores floating point numbers, with decimals, such as 19.99 or -19.99
 char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
 string - stores text, such as "Hello World". String values are surrounded by double
quotes
 bool - stores values with two states: true or false

C++ Fundamental Data Types


The table below shows the fundamental data types, their meaning, and their sizes (in bytes):

Data Type Meaning Size (in Bytes)


int Integer 2 or 4
float Floating-point 4
double Double Floating-point 8
char Character 1
wchar_t Wide Character 2
bool Boolean 1
void Empty 0

6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

Syntax

type variable_Name = value;

Where type is one of C++ types (such as int), and variable_Name is the name of the variable
(such as x or myName). The “=” equal sign is used to assign values to the variable.

To create a variable that should store a number, look at the following example:

Example

Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;


cout << myNum;

You can also declare a variable without assigning the value, and assign the value later:

Example

int myNum;
myNum = 15;
cout << myNum;

Example
//Demonstrate the use of different Types of Variables
#include <iostream>
using namespace std;
int main()
{
int myNum = 5; // Integer (whole number without decimals)

7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

double myFloatNum = 5.99; // Floating point number (with decimals)


char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)

cout<< "Integer Variable = " <<myNum<<endl;

cout<< "Floating Point Variable = " <<myFloatNum<<endl;

cout<< "Character Variable = " <<myLetter<<endl;

cout<< "String Variable = " <<myText<<endl;

cout<< "Boolean Variable = " <<myBoolean<<endl;

return 0;
}

C++ Constants
In C++, we can create variables whose value cannot be changed. For that, we use the const
keyword. constant", which means unchangeable and read-only. Here's an example:

Example

const int myNum = 15; // myNum will always be 15


myNum = 10; // error: assignment of read-only variable 'myNum'

A constant can also be created using the #define preprocessor directive.

//Demonstrate the use of variables and constants


#include <iostream>
using namespace std;
int main()
{

// declaring a constant. It's value cannot be changed.


const int CONST_VAL = 5;
CONST_VAL=6;
int iValue; // An integer variable
iValue = 1234; // Assigns 1234 to iValue
// Now print them out on the screen:
cout << "Integer value is: " << iValue << endl;
cout << "The constant is: " << CONST_VAL << endl;

return 0;
}

8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Operating with variables


Example:
#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;// a has updated value which is 6 now
b= a+2; // b has updated value which is 6+2 = 8
result = a - b; // result now contains 6-8= -2 value
// print out the result:
cout << result;
// terminate the program:
return 0;
}

Sizeof() Function:
Example:
#include <iostream>
using namespace std;

int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;

return 0;
}
We are also using sizeof() operator to get size of various data
types mean number of bytes used to store each data type.

Example:
#include <iostream>
using namespace std;

int main()
{
const int SIDE = 50;
int area;
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

area = SIDE*SIDE;
cout<<"The area of the square with side: " << SIDE <<" is: " << area <<
endl;

return 0;
}

Task 1:
 Create a C++ Program to Generate the Following Output. (10 Marks)

Task 02:
Write a C++ program that
 takes length, width of a rectangle of specific data type from user as input
 to calculate perimeter and area of rectangle.
 display the results.

Task 03:
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

1. Use concepts as already taught in the class and produce output as follows.

11

You might also like