Вы находитесь на странице: 1из 22

Paradigms of Computer Programming

and C++
Subject Code-CST-152
UNIT-I
Chapter-2
Topic Imperative Programming

University Institute of Engineering


Course Objectives
• To understand the concept of the various Programming Paradigms.
• To apply different programming languages for modeling real world
problems.

Imperative Programming Objectives

•To understand the basics of Imperative Programming.


•To apply programming languages for modeling real world problems.

University Institute of Engineering


Contents
• Introduction to Imperative Programming
• Data types
• Data Input and output streams (cin, cout)
• Introduction to namespace.

27-04-2019 University Institute of Engineering


Imperative Programming

Imperative programming is a paradigm of computer programming


in which the program describes a sequence of steps that change the
state of the computer.
It is also called procedural programming.
Imperative programming is a paradigm of computer programming
in which the program describes a sequence of steps that change the
state of the computer.

University Institute of Engineering 1


Data Types

When variables are declared of a particular data type then the variable
becomes the place where the data is stored and data types is the type of
value(data) stored by that variable.

Data can be of many types such as character, integer, real etc.

27-04-2019 University Institute of Engineering 2


Data Types

FUNDAMENTAL DATA
TYPES

DATA TYPE MODIFIERS

DATA TYPES
DERIVED DATA TYPES

USER DEFINED DATA


TYPES
27-04-2019 University Institute of Engineering 3
Fundamental Data Types

INTEGER

CHARACTER

FUNDAMENTAL DATA TYPES FLOAT

Fundamental data types are DOUBLE


those that are not composed
of other data types VOID
27-04-2019 University Institute of Engineering 4
Data Type Modifiers

Integer type modifiers

Data type Character type modifiers


modifiers

They change some


properties of the data Floating-point modifiers
type

27-04-2019 University Institute of Engineering 5


Integer Type Modifiers

TYPE APPROXIMATE SIZE MINIMAL RANGE

SHORT 2 -32768 to 32767


UNSIGNED SHORT 2 0 to 65,535
SIGNED SHORT 2 Same as short
INT 2 -32768 to 32767
UNSIGNED INT 2 0 to 65,535
SIGNED INT 2 Same as int
LONG 4 -2,147,483,648 TO 2,147,483,647
UNSIGNED LONG 4 0 to 4,294,967,295
SIGNED LONG 4 Same as long

27-04-2019 University Institute of Engineering 6


Character Type Modifiers

TYPE APPROXIMATE MINIMAL RANGE


SIZE
CHAR 1 -128 to 127

UNSIGNED CHAR 1 0 to 255

SIGNED CHAR 1 Same as char

27-04-2019 University Institute of Engineering 7


Floating Point Type Modifiers

TYPE APPROXIMATE DIGITS OF


SIZE PRECISION
FLOAT 4 7
LONG DOUBLE 8 15
LONG DOUBLE 10 19

27-04-2019 University Institute of Engineering 8


Derived Data Types

ARRAYS

FUNCTIONS

POINTERS
DERIVED DATA TYPES

REFERENCES

CONSTANTS

27-04-2019 University Institute of Engineering 9


User-defined Data Types

CLASS

STRUCTURE
USER DEFINED DERIVED DATA
TYPES
UNION

ENUMERATION

27-04-2019 University Institute of Engineering 10


Data Input and output streams (cin, cout)

In C++, I/O is performed by using streams. A stream is a “stream of


data” in which character sequences are “flow into” or “flow out off.” A
stream is an object with properties that are defined by a class. Global
objects are predefined for the standard I/O channels.

The header file iostream must be included to make use of the


input/output (cin/cout) operators.

University Institute of Engineering


27-04-2019 11
Standard Output (cout)
By default, the standard output of a program points at the screen. So with
the cout operator and the “insertion” operator (<<) you can print a message
onto the screen.
Example:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}

University Institute of Engineering


27-04-2019 12
Standard input (cin)

In most cases the standard input device is the keyboard. With the cin
and >> operators it is possible to read input from the keyboard.
example:
#include<iostream>
using namespace std;
int main()
{ char MY_CHAR;
cout << "Press a character and press return: ";
cin >> MY_CHAR;
cout << MY_CHAR;
return 0; }
University Institute of Engineering
27-04-2019 13
Namespace
Namespace is used to define a scope where identifiers like variables, functions,
classes, etc are declared. The main purpose of using a namespace is to prevent
ambiguity that may occur when two identifiers have same name
A namespace definition begins with the keyword namespace followed by the
namespace name as follows −
Syntax:
namespace namespace_name
{ // code declarations }

To call the namespace-enabled version of either function or variable, prepend (::)
the namespace name as follows −
name::code; // code could be variable or function.

University Institute of Engineering


27-04-2019 14
The using declaration

When we use using directive, we import all the names in the


namespace and they are available throughout the program, that is they
have global scope.
But with using declaration, we import one specific name at a time
which is available only inside the current scope.
Example:
namespace X
{ int x;
class Check
{
int i; }; }
University Institute of Engineering
27-04-2019 15
The using directive

using keyword allows you to import an entire namespace into your


program with a global scope. It can be used to import a namespace
into another namespace or any program.

This directive tells the compiler that the subsequent code is making
use of names in the specified namespace. The namespace is thus
implied for the following code −

University Institute of Engineering


27-04-2019 16
The using directive Example
#include <iostream>
using namespace std;
namespace first_space
{
void func()
The using
{ cout << "Inside first_space" << endl;
}}
using namespace first_space;
int main ()
{
directive
// This calls function from first name space.
func();
return 0; }
University Institute of Engineering
27-04-2019 17
References
• https://www.tutorialspoint.com/cplusplus/cpp_operators.htm
• http://www.studytonight.com/cpp/operators-and-their-types.php
• http://www.cplusplus.com/doc/tutorial/operators/

University Institute of Engineering


Unit Course Outcomes

•Understand the various paradigms of computer programming


• Identify the strengths and weaknesses of different programming paradigms
I-III
•To provide in-depth knowledge of various concepts of programming
paradigms

Object Oriented Programming Outcomes


•Use the basics of Imperative Programming.
• Be familiar with using C++ functions and the concepts related to program design.
•To provide in-depth knowledge of various concepts of programming paradigms as data
types, I/O functions.

University Institute of Engineering

Вам также может понравиться