Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Computer operating system architecture, Lecture notes of Operating Systems

Ranchi university https://www.docsity.com/en/documents Computer operating system architecture

Typology: Lecture notes

2017/2018

Uploaded on 07/02/2018

Kumar1994pk
Kumar1994pk 🇮🇳

5

(2)

2 documents

1 / 104

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to C++
C++, as we all know is an extension to C language and was developed by Bjarne stroustrup
at bell labs. C++ is an intermediate level language, as it comprises a confirmation of both
high level and low level language features. C++ is a statically typed, free form,
multiparadigm, compiled general-purpose language.
C++ is an Object Oriented Programming language but is not purely Object Oriented. Its
features like Friend and Virtual, violate some of the very important OOPS features,
rendering this language unworthy of being called completely Object Oriented. Its a middle
level language.
Benefits of C++ over C Language
The major difference being OOPS concept, C++ is an object oriented language whereas C
language is a procedural language. Apart form this there are many other features of C++
which gives this language an upper hand on C laguage.
Following features of C++ makes it a stronger language than C,
1. There is Stronger Type Checking in C++.
2. All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance etc makes
it more worthy and useful for programmers.
3. C++ supports and allows user defined operators (i.e Operator Overloading) and
function overloading is also supported in it.
4. Exception Handling is there in C++.
5. The Concept of Virtual functions and also Constructors and Destructors for Objects.
6. Inline Functions in C++ instead of Macros in C language. Inline functions make
complete function body act like Macro, safely.
7. Variables can be declared anywhere in the program in C++, but must be declared
before they are used.
What we will cover in Basics of C++
OOPS concepts basic
Basic Syntax and Structure
Data types and Modifiers
Variables in C++
Operators in C++
sizeof and typedef in C++
Decision Making
Loop types
Storage Classes
Functions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Computer operating system architecture and more Lecture notes Operating Systems in PDF only on Docsity!

Introduction to C++

C++, as we all know is an extension to C language and was developed by Bjarne stroustrup

at bell labs. C++ is an intermediate level language, as it comprises a confirmation of both

high level and low level language features. C++ is a statically typed, free form,

multiparadigm, compiled general-purpose language.

C++ is an Object Oriented Programming language but is not purely Object Oriented. Its

features like Friend and Virtual, violate some of the very important OOPS features,

rendering this language unworthy of being called completely Object Oriented. Its a middle

level language.

Benefits of C++ over C Language

The major difference being OOPS concept, C++ is an object oriented language whereas C

language is a procedural language. Apart form this there are many other features of C++

which gives this language an upper hand on C laguage.

Following features of C++ makes it a stronger language than C,

1. There is Stronger Type Checking in C++.

2. All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance etc makes

it more worthy and useful for programmers.

3. C++ supports and allows user defined operators (i.e Operator Overloading) and

function overloading is also supported in it.

4. Exception Handling is there in C++.

5. The Concept of Virtual functions and also Constructors and Destructors for Objects.

6. Inline Functions in C++ instead of Macros in C language. Inline functions make

complete function body act like Macro, safely.

7. Variables can be declared anywhere in the program in C++, but must be declared

before they are used.

What we will cover in Basics of C++

 OOPS concepts basic

 Basic Syntax and Structure

 Data types and Modifiers

 Variables in C++

 Operators in C++

 sizeof and typedef in C++

 Decision Making

 Loop types

 Storage Classes

 Functions

Object Oriented Programming

Object Oriented programming is a programming style that is associated with the concept of

Class, Objects and various other concepts revolving around these two, like Inheritance,

Polymorphism, Abstraction, Encapsulation etc.

In the video below, we have explained the basic concepts of Object Oriented Programming

with help of a very easy to understand example. If you want to skip the video, everything is

covered below as well.

https://youtu.be/xoL6WvCARJY,

https://www.youtube.com/watch?v=xoL6WvCARJY#action=share

Let us try to understand a little about all these, through a simple example. Human Beings are

living forms, broadly categorized into two types, Male and Female. Right? Its true. Every

Human being(Male or Female) has two legs, two hands, two eyes, one nose, one heart etc.

There are body parts that are common for Male and Female, but then there are some specific

body parts, present in a Male which are not present in a Female, and some body parts present

in Female but not in Males.

All Human Beings walk, eat, see, talk, hear etc. Now again, both Male and Female, performs

some common functions, but there are some specifics to both, which is not valid for the other.

For example : A Female can give birth, while a Male cannot, so this is only for the Female.

Human Anatomy is interesting, isn't it? But let's see how all this is related to C++ and OOPS.

Here we will try to explain all the OOPS concepts through this example and later we will

have the technical definitons for all this.

Class

Here we can take Human Being as a class. A class is a blueprint for any functional entity

which defines its properties and its functions. Like Human Being, having body parts, and

performing various actions.

OOPS Concept Definitions

Now, let us discuss some of the main features of Object Oriented Programming which you

will be using in C++(technically).

1. Objects

2. Classes

3. Abstraction

4. Encapsulation

5. Inheritance

6. Overloading

7. Exception Handling

Objects

Objects are the basic unit of OOP. They are instances of class, which have data members and

uses various member functions to perform tasks.

Class

It is similar to structures in C language. Class can also be defined as user defined data type

but it also contains functions in it. So, class is basically a blueprint for object. It declare &

defines what data variables the object will have and what operations can be performed on the

class's object.

Abstraction

Abstraction refers to showing only the essential features of the application and hiding the

details. In C++, classes can provide methods to the outside world to access & use the data

variables, keeping the variables hidden from direct access, or classes can even declare

everything accessible to everyone, or maybe just to the classes inheriting it. This can be done

using access specifiers.

Encapsulation

It can also be said data binding. Encapsulation is all about binding the data variables and

functions together in class.

Inheritance

Inheritance is a way to reuse once written code again and again. The class which is inherited

is called the Base class & the class which inherits is called the Derived class. They are also

called parent and child class.

So when, a derived class inherits a base class, the derived class can use all the functions

which are defined in base class, hence making code reusable.

Polymorphism

It is a feature, which lets us create functions with same name but different arguments, which

will perform different actions. That means, functions with same name, but functioning in

different ways. Or, it also allows us to redefine a function to provide it with a completely new

definition. You will learn how to do this in details soon in coming lessons.

Exception Handling

Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced

at runtime.

Basics of C++

In this section we will cover the basics of C++, it will include the syntax, variable, operators,

loop types, pointers, references and information about other requirements of a C++ program.

You will come across lot of terms that you have already studied in C language.

Syntax and Structure of C++ program

Here we will discuss one simple and basic C++ program to print "Hello this is C++" and its

structure in parts with details and uses.

First C++ program

#include <iostream.h> using namespace std; int main() { cout << "Hello this is C++"; }

Header files are included at the beginning just like in C program. Here iostream is a header

file which provides us with input & output streams. Header files contained predeclared

function libraries, which can be used by users for their ease.

Data Types in C++

They are used to define type of variables and contents used. Data types define the way you

use storage in the programs you write. Data types can be built in or abstract.

Built in Data Types

These are the data types which are predefined and are wired directly into the compiler. eg:

int, char etc.

User defined or Abstract data types

These are the type, that user creates as a class. In C++ these are classes where as in C it was

implemented by structures.

Basic Built in types

char for character storage ( 1 byte )

int for integral number ( 2 bytes )

float single precision floating point ( 4 bytes )

double double precision floating point numbers ( 8 bytes )

Example :

char a = 'A'; // character type int a = 1; // integer type float a = 3.14159; // floating point type double a = 6e-4; // double type (e is for exponential)

Other Built in types

bool Boolean ( True or False )

void Without any Value

wchar_t Wide Character

Enum as Data type

Enumerated type declares a new type-name and a sequence of value containing identifiers

which has values starting from 0 and incrementing by 1 every time.

For Example :

enum day(mon, tues, wed, thurs, fri) d;

Here an enumeration of days is defined with variable d. mon will hold value 0, tue will have 1

and so on. We can also explicitly assign values, like, enum day(mon, tue=7, wed);. Here,

mon will be 0, tue is assigned 7, so wed will have value 8.

Modifiers

Specifiers modify the meanings of the predefined built-in data types and expand them to a

much larger set. There are four data type modifiers in C++, they are :

1. long

2. short

3. signed

4. unsigned

Below mentioned are some important points you must know about the modifiers,

 long and short modify the maximum and minimum values that a data type will hold.

 A plain int must have a minimum size of short.

 Size hierarchy : short int < int < long int

 Size hierarchy for floating point numbers is : float < double < long double

 long float is not a legal type and there are no short floating point numbers.

 Signed types includes both positive and negative numbers and is the default type.

 Unsigned, numbers are always without any sign, that is always positive

What are Variables

Variable are used in C++, where we need storage for any value, which will change in

program. Variable can be declared in multiple ways each with different memory requirements

and functioning. Variable is the name of memory location allocated by the compiler

depending upon the datatype of the variable.

Basic types of Variables

Each variable while declaration must be given a datatype, on which the memory assigned to

the variable depends. Following are the basic types of variables,

bool For variable to store boolean values( True or False )

char For variables to store character types.

int for variable with integral values

float and double are also types for variables with large and floating point values

lifetime. But even if they are declared and initialized at the same time outside the main()

function, then also they can be assigned any value at any point in the program.

Example : Only declared, not initialized

include using namespace std; int x; // Global variable declared int main() { x=10; // Initialized once cout <<"first value of x = "<< x; x=20; // Initialized again cout <<"Initialized again with value = "<< x; }

Local Variables

Local variables are the variables which exist only between the curly braces, in which its

declared. Outside that they are unavailable and leads to compile time error.

Example :

include using namespace std; int main() { int i=10; if(i<20) // if condition scope starts { int n=100; // Local variable declared and initialized } // if condition scope ends cout << n; // Compile time error, n not available here }

Some special types of variable

There are also some special keywords, to impart unique characteristics to the variables in the

program. Following two are mostly used, we will discuss them in details later.

1. Final - Once initialized, its value cant be changed.

2. Static - These variables holds their value between function calls.

Example :

#include <iostream.h> using namespace std; int main() { final int i=10; static int y=20; }

Operators in C++

Operators are special type of functions, that takes one o

value. For example : addition (+), substraction (

Operators are used to perform various operations on variables and constants.

Types of operators

1. Assignment Operator

2. Mathematical Operators

3. Relational Operators

4. Logical Operators

5. Bitwise Operators

6. Shift Operators

7. Unary Operators

8. Ternary Operator

9. Comma Operator

Assignment Operator ( = )

Operates '=' is used for assignment, it takes the right

the left-hand side (called lvalue). Assignment operator is the only operator which can be

overloaded but cannot be inherited.

Mathematical Operators

There are operators used to perform basic mathematical operations. Addition (+) ,

(-) , diversion (/) multiplication (*) and modulus (%) are the basic mathematical operators.

Modulus operator cannot be used with floating

C++ and C also use a shorthand notation to perform an operation and assignment at same

type. Example,

Operators are special type of functions, that takes one or more arguments and produces a new

value. For example : addition (+), substraction (-), multiplication (*) etc, are all operators.

Operators are used to perform various operations on variables and constants.

Operates '=' is used for assignment, it takes the right-hand side (called rvalue) an

hand side (called lvalue). Assignment operator is the only operator which can be

overloaded but cannot be inherited.

There are operators used to perform basic mathematical operations. Addition (+) ,

) , diversion (/) multiplication (*) and modulus (%) are the basic mathematical operators.

Modulus operator cannot be used with floating-point numbers.

C++ and C also use a shorthand notation to perform an operation and assignment at same

r more arguments and produces a new

), multiplication (*) etc, are all operators.

hand side (called rvalue) and copy it into

hand side (called lvalue). Assignment operator is the only operator which can be

There are operators used to perform basic mathematical operations. Addition (+) , subtraction

) , diversion (/) multiplication (*) and modulus (%) are the basic mathematical operators.

C++ and C also use a shorthand notation to perform an operation and assignment at same

Shift Operators

Shift Operators are used to shift Bits of any variable. It is of three types,

1. Left Shift Operator <<

2. Right Shift Operator >>

3. Unsigned Right Shift Operator >>>

Unary Operators

These are the operators which work on only one operand. There are many unary operators,

but increment ++ and decrement -- operators are most used.

Other Unary Operators : address of &, dereference *, new and delete, bitwise not ~, logical

not !, unary minus - and unary plus +.

Ternary Operator

The ternary if-else? : is an operator which has three operands.

int a = 10; a > 5? cout << "true" : cout << "false"

Comma Operator

This is used to separate variable names and to separate expressions. In case of expressions,

the value of last expression is produced and used.

Example :

int a,b,c; // variables declaration using comma operator a=b++, c++; // a = c++ will be done.

sizeof operator in C++

sizeOf is also an operator not a function, it is used to get information about the amount of

memory allocated for data types & Objects. It can be used to get size of user defined data

types too.

sizeOf operator can be used with and without parentheses. If you apply it to a variable you

can use it without parentheses.

cout << sizeOf(double); //Will print size of double int x = 2; int i = sizeOf x;

typedef Operator

typedef is a keyword used in C language to assign alternative names to existing types. Its

mostly used with user defined data types, when names of data types get slightly complicated.

Following is the general syntax for using typedef,

typedef existing_name alias_name

Lets take an example and see how typedef actually works.

typedef unsigned long ulong;

The above statement define a term ulong for an unsigned long type. Now this ulong identifier

can be used to define unsigned long type variables.

ulong i, j;

typedef and Pointers

typedef can be used to give an alias name to pointers also. Here we have a case in which use

of typedef is beneficial during pointer declaration.

In Pointers * binds to the right and not the left.

int* x, y ;

By this declaration statement, we are actually declaring x as a pointer of type int, whereas y

will be declared as a plain integer.

typedef int* IntPtr ; IntPtr x, y, z;

But if we use typedef like in above example, we can declare any number of pointers in a

single statement.

x is greater than y

if...else statement

The general form of a simple if...else statement is,

if( expression ) { statement-block1; } else { statement-block2; }

If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped

and 'statement-block2' is executed.

Example :

void main( ) { int x,y; x=15; y=18; if (x > y ) { cout << "x is greater than y"; } else { cout << "y is greater than x"; } }

Output :

y is greater than x

Nested if....else statement

The general form of a nested if...else statement is,

if( expression ) { if( expression1 ) { statement-block1; } else { statement-block2; } } else

statement-block3; }

if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to

perform the test for 'expression 1'. If the 'expression 1' is true the 'statement-block1' is

executed otherwise 'statement-block2' is executed.

Example :

void main( ) { int a,b,c; clrscr(); cout << "enter 3 number"; cin >> a >> b >> c; if(a > b) { if( a > c) { cout << "a is greatest"; } else { cout << "c is greatest"; } } else { if( b> c) { cout << "b is greatest"; } else { printf("c is greatest"); } } getch(); }

else-if ladder

The general form of else-if ladder is,

if(expression 1) { statement-block1; } else if(expression 2) { statement-block2; } else if(expression 3 ) { statement-block3; }

Looping in C++

In any programming language, loops are used to execute a set of statements repeatedly until a

particular condition is satisfied.

How it works

A sequence of statement is executed until a specified condition is true. This sequence of

statement to be executed is kept inside the curly braces { } known as loop body. After every

execution of loop body, condition is checked, and if it is found to be true the loop body is

executed again. When condition check comes out to be false, the loop body will not be

executed.

There are 3 type of loops in C++ language

1. while loop

2. for loop

3. do-while loop

while loop

while loop can be address as an entry control loop. It is completed in 3 steps.

 Variable initialization.( e.g int x=0; )

 condition( e.g while( x<=10) )

 Variable increment or decrement ( x++ or x-- or x=x+2 )

Syntax :

variable initialization ; while (condition) { statements ; variable increment or decrement ; }

for loop

for loop is used to execute a set of statement repeatedly until a particular condition is

satisfied. we can say it an open ended loop. General format is,

for(initialization; condition ; increment/decrement) { statement-block; }

In for loop we have exactly two semicolons, one after initialization and second after

condition. In this loop we can have more than one initialization or increment/decrement,

separated using comma operator. for loop can have only one condition.

Nested for loop

We can also have nested for loop, i.e one for loop inside another for loop. Basic syntax is,

for(initialization; condition; increment/decrement) { for(initialization; condition; increment/decrement) { statement ; } }

do while loop

In some situations it is necessary to execute body of the loop before testing the condition.

Such situations can be handled with the help of do-while loop. do statement evaluates the

body of the loop first and at the end, the condition is checked using while statement. General

format of do-while loop is,

do { .... .....