Syjc-Cs-1 Notes
Syjc-Cs-1 Notes
SYJC
CS-1 NOTES
2022-23
CHP-1. OPERATING SYSTEM
The requirements are changing day-by-day. Thus, hardware is becoming more and more
powerful and is able to perform complex functions. Hardware is nothing but finely designed machinery.
An operating system is a program, which acts as an interface between a user and the hardware
(i.e. all computer resources).
E.g.: it is just like our secretary. As the boss gives orders to his secretary and she does all the work for his
boss. The secretary herself decides: How to do? What to do? When to do? etc. same way, we pass our
orders/request to OS thus to make the computer system convenient to use and secondary goal is use
computer hardware in an efficient manner.
Device Driver:-
Each I/O device has its own characteristics requiring careful programming. A special program
routinewas written for each I/O device. This special routine was called ‘device driver’.
Note: A device driver is a program, which controls and monitors the working of a particular I/O
device.
E.g. our keyboard will have a separate device driver and mouse will have a separate device driver.
The compliers for COBOL, FORTAN and other languages appeared but made the computer
operationscomplex. The program execution consisted of many steps: loading the complier tape,
running the complier, unloading the complier, unloading the complier tape, etc If an error
occurred, the entire
process has to be repeated.
Note: The time spent in loading, unloading, mounting etc. is called setup time.
(iii) Timesharing OS:- This OS uses the time-sharing technique. Each active user program is given
a fair share of CPU time ( δ ), if the time elapses or an I/O operation is requested, CPU shifts over to
the next job waiting and the previous job is put to wait (if the time is over).
Note: The delay between job submission and job completion is called turnaround time.
A turnaround time does not improve the system utilization as multiprogramming does.
(iv) Real time OS:-
In real time OS, the jobs have fixed deadlines and the jobs have to be completed within their
deadlines. The system performance is measured by its ability to complete the jobs within their
specified deadlines. If a job cannot be completed within its deadline, this situation is called deadline
overrun.
(v) Multiprocessing OS:- The multiprocessing OS is capable of handling more than one processor,
as the jobs have to be executed on more than one processor. The multiprocessing OS should be
capable of load sharing in case of identical processors so that the system’s efficiency improves.
Nirmala Memorial Foundation Junior College of Commerce and Science Page 2
FUNCTIONS OF OS
The environment required for the execution of the program is provided by the operating system. The
environment provided is responsible for effective and efficient execution of the program which
depends upon the service provided by the OS.
Types of Services
(i) Program execution
(ii) Handling I/O operations
(iii) Manipulation of file system
(iv) Error detection and Handling
(v) Resource allocation
(vi) Accounting
(vii) Information and Resource Protection
2. Storage Management : Memory is a large array of word or bytes, each with its own address. The
CPU reads from and writes to specific memory address.
P1
P2
P3
As the entire partition is allocated to one job, two parts 20K, 25K of the two partitions is
wasted, as the program needs only 40K and 35K. This wastage of space is called internal
fragmentation.
Drawback: One is internal fragmentation. Second is, external fragmentation, during a
program execution, after the program has been assigned a partition a if its complied
version (pgm after compilation) is too large to fit in the partition.
(ii) Segmentation
1. Segmentation are logical divisions of programs and hence are normally of variable sizes.
2. Segmentation is a memory management scheme which support user’s view of memory.
3. Each segment has number and length.
4. Each program in executable form can be considered to be consisting of different segments such as
code, data and stack. Each of these can be further divided into new segments.
5. A program normally contains main program, some subprograms and few predefined and precompiled
functions. Each of these belongs to different segments.
6. An application programmer does not necessarily have to declare different segments in the program. If
various segments in his programs does not define explicitly, then the compiler does it by its own.
Following are the jobs of compiler:
(a) Recognize different segments in program.
(b) Number those segments.
(c) Define segment table.
(d) Produce an executable image by assigning two dimensional addresses.
7. Consider the examples an shown in figure Four segments numbered 0 to 3. The SMT (Segment Map
Table) has separate entry for each segment giving the size and base of segment.
File System
Logical IOCS
Physical IOCS
Computer Hardware
Hierarchy of Information Management Modules.
1. Physical IOCS (Input-Output Control System) is responsible for device management for ensuring
device independence ( through drivers).
2. Logical IOCS is responsible for efficient organization and access of data on I/O devices. It
provides basic capabilities for file definition, choice of data organization and access method.
3. File System is responsible for protection and controlled sharing of files.
TERMINAL HARDWARE: Terminal hardware can be divided into two parts: the keyboard, which
is an input medium and the video screen, which is an output medium.
The terminal can be dumb terminal or intelligent terminal.
The dumb terminal has a microprocessor in it on which we can run rudimentary software.
It has very limited memory.
The dumb terminal is responsible for the basic input and output of characters.
Even then, it is called ‘dumb’ because it does no processing on the input characters.
Example : normal monitor.
Q. Define “security” with respect to an operating system. Explain the different elements of
security?
Ans.
i) Security is concerned with the ability of the operating system to enforce control over the storage and
transportation of data in and between the objects that the OS supports.
ii) In multiuser operating systems, the concepts of security and protection are very important. User
programs should not interfere with one another or with the operating system.
iii) In general, secure systems are those, which control, through the use of specific security features,
access to information that only properly authorized individuals or processes operating on their behalf
will have access to read, write, create or delete.
iv) There are three main elements of security are:
Confidentiality: It ensures that information is not accessed in an unauthorized manner. It is
generally related to the ‘read’ operations.
Integrity: It ensures that the information is not amended or deleted by an unauthorized manner. It is
generally related to ‘write’ operations.
Availability: It ensures that information is available to the authorized users at right time.
Q. What is the Computer Virus? What are the different types of Computer virus?
Ans.
1. A computer virus is written with a clear intention of infecting other programs.
2. It differs from a worm, which is a complete program by itself.
3. On the other hand, the computer virus normally cannot and does not, operate independently.
4. Virus normally causes direct harm to the system.
5. It can corrupt the code as well as data whereas a worm only consumes system resources.
Types of virus:-
There are several types of viruses. They are:
1. Boot Sector Infectors -- Virus can get infected into the system memory if the machine is booted
with an infected floppy or hard disk.
2. Memory Resident Infectors – It load upon execution of an infected file. Subsequently whenever a
non-infected file is executed the virus infects it.
3. File Specific Infectors – Infection occurs when an infected file is executed. The virus then loads its
vital code into the memory.
4. Command Processor Infectors – A few viruses place their memory-resident code into allocated
to COMMAND.COM. They can cause system crash.
5. General Purpose Infectors -- It can infect exe file.
LINUX
Q. What is Linux? Explain its components?
Ans. 1. Linux looks and feels like any other UNIX system, however, it is much younger than most UNIX
system.
2. Its development began in 1991, when a Finnish student, Linus Torvalds, wrote and christened a
small but self-contained kernel for the 80386 processor, the true 32-bit processor in Intel’s range of
PC-compatible CPUs.
3. Linux development revolved largely around the central operating system kernel- the core
privileged executive that manages all system resources and the interacts directly with the hardware.
4. The basic Linux system is a standard environment for applications and for user programming but
it does not enforce any standard means of managing the available functionality as a whole.
5. A Linux distribution includes all the standard components of the Linux system, plus a
administrative tools of simplify the initial installation and upgrading of Linux.
6. A modern distribution also includes tools for managing of file system, creation and management
of user accounts, networking administration & so on.
Q. Which are the three main areas in which the operating system divides its services? Give
examples
Ans.: The O.S. divides its services in the following three main areas:
1. Information Management (IM)
2. Process Management (PM)
3. Memory Management (MM)
Q. Explain the use of Video RAM. Explain data bytes and attribute bytes.
Ans.
i) The video RAM is basically the memory that the terminal hardware itself has.
ii) Anytime all characters stored in the video RAM are displayed on the screen by the video
controller using display electronics.
iii) All particular information (attributes) are stored in video RAM.
iv) Video RAM consists of 2000 data bytes (0 to 1999) preceded by 2000 corresponding attribute
bytes (0 to 1999) as shown in the figure.
v) A typical alphanumeric screen can display 25 lines each consisting of 80 characters i.e.
25*80=2000 characters.
Q. Explain Running, Ready and Blocked process in process management. Discuss various process
states with examples. What is the difference between blocked and ready state
Ans. In order to manage switching between processes, the operating system defines three basic process
states, which are given below.
Running state: There is only one process, which is executed by C.P.U.at any given moment. This
process is called as running state.
In multiprocessor systems, with multiple C.P.Us, there are many running processes at a given
moment. The operating system keeps track of all of them.
Ready state: The process, which is not waiting for an external event such as an I/O operation, but
which is not running is said to be in ready state.
Actually, a process in ready state could have been running. But the fact that, there is only one CPU,
which is executing some other process, while this process, is waiting for CPU’s attention towards it.
Blocked state: When a process is waiting for an external event such as an I/O operation, the process
is said to be in blocked state.
The major difference between blocked and ready process is that a blocked process cannot be directly
scheduled even if CPU is free, whereas a ready process can be scheduled if the CPU is free.
Context Switching
iv) Let A and B be the two processes ready for execution and requires CPU time for execution. Let
CPU time be given to process A, which is having some instruction depending on process B or on
some external event such as I/O operation. Then, it is the job of operating system to halt the
execution of process A and give CPU time for process B.
The lose in time in turning the attention of CPU from process A to process B is known as context
switching.
Q. State the various steps involved in the allocation of a partition in case of fixed partition
memory management.
Ans. When a process is to be allocated the following partition steps takes place:
i) The long term process scheduler of the PM decides which process to be brought in to the memory.
ii) It then finds out the size of the program to be loaded by consulting the IM portion of the OS. The
complier keeps the size of the program in the header of the executable file.
iii) Then makes a request to the partition allocation routine of the MM to allocate a free partition,
with the appropriate size.
The partition description table (PDT) is useful for this procedure.
900K :
P4 Process C
1000K
Fixed partition
Basic Terminology
Data are simply value or set of values.
A data item refers to a single unit of values.
Data items that are divided into sub items are called “group items”; those that are not divided
are called “elementary items”. E.g.: name can be divided into sub items – first name,
middle name & last name but the P.F.F. number is treated as a single item.
Collections of data are organized into a hierarchy of fields, records and files.
An entity is something that has certain attributes or properties, which may be assigned values.
Example:
Attributes: Name Age Sex PFN
Values: Ruchi 21 F 1423
Entities with similar attributes (e.g. all the employees in an organization) form an entity set.
The term “information” is sometimes used for data with given attributes, or, in other words,
meaningful or process data.
The way that data is organized into the hierarchy of fields, records & files reflects the relation between
attributes, entities and entity sets.
That is, a field is elementary unit of information representing an attribute of an entity, a record is the
collection of field values of a given entity & a file is the collection of records of the entities in a given
entity set.
202 2
203 3
204 4
205 5
Loc (LA[k]) = address of the element LA[k] of the array LA. As previously noted the elements of LA
are stored in successive memory cells. Accordingly, the computer does not need to keep the track of the
address of every element of LA, but needs of keep tracks only of the address of the first elements of LA,
denoted by.
Base (LA) i.e., starting address of array
Using this address Base (LA), the computer calculates the address of any elements of LA by the
following formula:
LOC(LA[k]) = Base(LA)+w(k-lower bound)
where w is the number of words per memory cells for the array LA.
Algo using ‘for’ loop:- (Traversing a Linear Array) this algorithm traverses a linear array LA
with lower bound LB and upper bound UB.
1. Repeat for K = LB to UB:
Apply PROCESS to LA[k]
[End of loop]
2. Exit.
Let A be a collection of data elements in the memory of the computer. “Inserting” refers to the
operation of adding one more element to the collection A, and “deleting” refers to the operation of
removing one of the elements from A. This section discusses inserting and deleting when A is a
linear array.
Inserting an elements at the “end” of the linear array can be easily done provide the memory
space allocated for the array is large enough to accommodate the additional element. On the other
hand, suppose we need to insert an element in the middle of the array. Then, on the average, half of
the elements must be moved downward to new locations to accommodate the new element and keep
the order of the other elements.
Similarly, deleting an element at the “end” of an array presents no difficulties, but deleting an
element somewhere in the middle of the array would require that each subsequent element be moved
one location upward in order to “fill up” the array.
INSERT(LA, N,K,ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K N.
This algorithm inserts an elements ITEM into the Kth position in LA.
1. Set J = N //Initialize countes
2. Repeat Steps 3 and 4 while J =K.
3. Set LA // Move Jth element downward
[J+1]: = LA[J]
4. Set: =J-1. // Decrease counter
[End of step 2 loop.]
5. Set LA [k]: = ITEM. // Insert element
6. Set N: = N+1 // Reset N
7. Exit.
The following algorithm deletes the Kth element from a linear array LA and assigns it to a variable
ITEM.
Bubble sort:
Suppose the list of numbers A[1], A[2], ............. A[N] is in memory. The bubble sort algorithmic
works as follows:
Step1: Compare A[1] and A[2] and arrange them in the desired order, so that A[1]<A[2]. Then
compare A[2] and A[3] & arrange them so that A[2]<A[3]. Then compare A[3] and A[4] and arrange
them so that A[3]<A[4]. Continue until we compare A[N-1] with A[N] and arrange them A[N-
1]<A[N].
Note: During step 1, the largest element is “bubbled up” to the nth position or “sinks” to the nth
position.
Step2: Repeat step 1 with one less comparison, that is, now we stop after we compare and
possible rearrange A[N-2] and A[N-1].
Step3: Repeat step 1 with two fewer comparisons, that is, we stop after we compare and possible
rearrange A[N-3] and A[N-2].
........................................................................................................................................................
.........................................................................................................................................................
Step N-1: Compare A[1] with A[2] and arrange them so that A[1]<A[2].
The process of sequentially traversing through all or part of a list is frequently called a “pass”
so each of the above step is called a pass.
Example:
Suppose the following numbers are stored in an array A
32,51,27,85,66,23,13,57
Now, we will apply bubble sort to the array
27,33, 51,23,66,13,57,85
27,33, 51,23,13,66,57,85
27,33,51,23,13,57,66,85
At the end of pass 2, the second largest number, 66, has moved to its way down next to the last
position.
Pass3: 27,33,23,51,13,57,66,85
27,33,23,13,51,57,66,85
Pass4: 27,23,33,13,51,57,66,85
27,23,13,33,51,57,66,85
Pass5: 23,27,13,33,51,57,66,85
23,13,27,33,51,57,66,85
Pass6: 13,23,27,33,51,57,66,85
Pass7: Finally, there is no comparison now, as we have sorted the elements of the array A.
Algorithm: (imp)
1) 11 22 30 33 40 44 55 60 66 77 80 88 99
2) 11 22 30 33 40 44 55 60 66 77 80 88 99
3) 11 22 30 33 40 44 55 60 66 77 80 88 99
[Successful ]
Linear arrays:
Definition : An array can be defined as the collection of the sequential memory locations, which can
be referred to a single name along with a number, known as index, to access a particular field or data.
When the elements need to be referred by more than one index, it is known as Multidimensional
arrays.
There are data structures other than arrays, linked lists and trees. Some of these structures are
as follows:
a) Stack: A stack, also called a last-in-first-out (LIFO) system, is a linear list in which insertion and
deletion can take place only at one end, called the top. This structure is similar in its operation to
a stack of dishes or glasses placed at one above another and you can take out the dishes or glass
only from one end, that is , top.
b) Queue: A queue, also called a first-in-first –out (FIFO) system, is a linear list in which deletion
can take place only at one end of the list “front”, and insertions can take place only at the other
end of the list “rear”. For example, Queue in the bus stop, the first person in the queue gets in the
bus first and to join the queue the person has to come in the last.
c) Graph: Data sometimes contain a relationship between pairs of elements, which is not
hierarchical in nature. For Example, suppose an airline flies only between the cities connected by
lines. The data structure that reflects this type of relationship is called a graph.
Module A
Module B
Module C
Condition? Condition?
IF condition, then:
[Module A]
Else:
[Module B]
[End of If structure]
The logic of this structure is pictured in Fig (b), above.
(c) Multiple alternative: This structure has the form
If condition (1), then:
[Module A]
Else if condition (2), then :
[Module B]
:
Else:
[Module C]
[End of If structure]
This logic of this structure allows only one of the modules to be executed.
3. Iteration Logic (Repetitive Flow):
This type of logic refers with a repeat statement involving loops. Each type begins with a Repeat
statement and is followed by a module, called the body of the loop
K=R
Is K>S?
K=K+T
Ans.
An algorithm is a well-defined list of steps for solving a particular task or problem.
One major purpose of complexity is to develop efficient algorithm for the processing of or data.
The time and space it uses are two major measures of the efficiency of an algorithm.
∴ The complexity of an algorithm is the function which gives the running time / space in terms of
the input size.
The following two complexities are the important factor for complexity of an algorithm.
Definition:
(i) Space complexity: The space complexity of an algorithm is the amount of memory it needs to run
or to complete an algorithm.
(ii) Time complexity: The time complexity of an algorithm is the amount of computer time it needs to
run or to complete an algorithm.
Collection of data is frequently organized into hierarchy of field i.e. records. A file is nothing but
collection of records.
(i) A record is a collection of fields, while an array is list of homogeneous data elements.
(ii) A record may contain non-homogeneous data i.e. data elements may be of different data types. An
array always contains homogeneous data.
(iii) In a record, natural ordering of elements is not possible. Array elements can be naturally ordered.
(iv) Elements of record are referenced by level number, while those of array can be referenced by an
index set consisting of n consecutive numbers.
Data processing involves storing and processing data organized into lists.
One-way store such data is by means of arrays.
The linear relationship between the data elements of an array is reflected by the physical relationship of
the data in memory not by any information contained in the data elements themselves.
This makes easy to compute the address of an element in an array.
Disadvantages: It is relatively expensive to insert and delete elements in an array.
Also, since an array usually occupies a block of memory, one cannot simply double or triple the size of
the array.
Link or Pointer
Another way of storing a list in memory is to have each element in the list contain a field, called list
or pointer, which contains the address of the next element in the list.
It does not occupy the adjacent space in memory, this will make it easier to insert and delete
elements in the list.
1 Sita 7
Start
2
3 Karan 11
5
4 Juliet 0
5 Vishal 3
7 Ruchi 4
8 Mihir 1
9 Nita
10
11 Sahil 8
12 Nilesh
1
2
3 O 6
Start 4 T 0
5
6 11
9 10
7 X
8
9 N 3
10 I 4
11 E 7
Q. What are linked lists? Show a liked list with suitable example having six nodes with a
properly labelled diagram.
OR
With suitable example, show labelled diagram for link between two nodes having theinformation
part and next pointer field.
Ans. i) A linked list is a linear collection of data elements, called nodes, where the linear order is
maintained with the help of pointers.
ii) Linked list is also called as one-way list.
iii) Each node in the linked list is divided into two parts. First part is called as INFO part, called as
LINK part, which is next pointers field i.e., it contains the address of next node in the list.
iv) e.g.
Start
10
Info Link (Pointer to next node)
A 13 B 19 C 14 D 12 E 18
F X
18
(a) The above figure shows a linked list having six nodes.
(b) The left part of the node is the Info part, which contains information of the element, while the
right part is Link part, which is next pointers field i.e., it points to next node.
Binary Tree
We know the linear types of data structures: strings, arrays, lists, stacks and queues.
Data structures also define nonlinear data structure called a tree.
The tree structure is mainly used to represent data containing a hierarchical
relationship between elements: example: family trees, records etc.
Binary tree is a tree, which can be easily maintained in the computer.
B C
D E F G
H I J
E /
+
-
*
e
a
b
c d
Q. How binary tree is represented in memory?
Ans. A tree T can be maintained in memory be means of a linked representation which uses three
parallel arrays INFO , LEFT and RIGHT and a pointer variable ROOT.
Each node N of T will correspond to a location K such that:
1) INFO[K] contains the data at the node N.
2) LEFT[K] contains the location of the left child of node N.
3) RIGHT[K] contains the location of the right child of node N.
1 0
2 Sita 0 12
3 Guru 0 0
ROOT 4 Girish 2 0
5 1
6 Bhawna 0 0
7 Leeta 3 10
14 8 11
9 Chandu 6 4
10 Ruby 0 0
11 13
12 Amit 0 0
13 5
14 Heena 9 7
Heena
Chandu Leeta
Amit
a) The value ROOT = 14 indicates that Heena is the root of the tree.
LEFT[14]=9 indicates that Chandu is the left child of Heena, and RIGHT[14]=7 indicates
that Leeta is the right child of Heena.
(Repeat the step for each node in the diagram)
Preorder: (NLR)
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
The three algorithms are sometimes called, respectively, the node-left-right (NLR) traversal,
the left-node-right (LNR) traversal and the left-right-node(LRN) traversal.
PUSH
65 65
24 24
43 43
12 12
11 11
Suppose if we want to delete 43 from the stack, so we have to first delete the top element
i.e. 65, then the second, then third, until we pop the required element.
POP
65
POP
24
65 POP
24 24 43
43 43 43
12 12 12 12
11 11 11 11
11 12 43 24
front rear
Inserting element 65.
11 12 43 24 65
front
rear
(inserted the element)
Now, if we want to delete the element 11.
12 43 24 65
front rear
(deleting element 11)
Q. What are pointer arrays?
Ans. i) An array is called pointer array, if each element of that array is a pointer.
ii) The variable is called as pointer variable, it points to another variable i.e. it
contains the memory address of other variable.
iii) consider an organization, with divides its employee list into four groups,
depending on certain conditions. Following figure shows the list of 4 groups.
There are 15 employes and groups contains 2, 5, 4 and 4 employes respectively as
Group 1 Group 2 Group 3 Group 4
Omprakash
iv) If these groups are to be represented in memory, the most efficient way is to use 2
vi) Each element of Group array is a pointer, which holds the starting addresses of
different groups. Hence, it is called as pointer array.
Ans. : C++ is an object oriented programming language. Initially C++ was named as “C with classes”.
C++ was developed by Bjarne Stroutstrup at AT & T Bell Laboratories, USA, in the early eighties. Advantages of
C++:-
1. C++ is an incremented version of C. It is a superset of “C”. Almost all C programs can also run in
C++ compiler.
2. The important facilities are added in C++ such as classes, function overloading & operator
overloading.
3. C++ allows user to create abstract data types, to inherit properties from existing data types.
4. C++ supports polymorphism.
5. Any real life application system such as editor, complier, and database communication systems can
be built by C++.
6. Object oriented libraries can be built by C++.
7. C++ programs can be easily implemented, maintained and expanded.
Structure Array
Union void Function
Integral Floating Pointer
Class
type type
Enumeration
1. C++ provides three data types which are user-defined data type, built-in data type and derived
data type.
2. User-defined data type provides structure, union and class while derived data type provides
arrays, function and pointer.
3. Built-in data type provides three types which are integral type, void and floating type.
4. Integral includes integer and character (string) while floating type includes float and double.
Q. Enlist the basic data types used in C++ with size of data in terms of bytes for each.
Ans. : Built-in type
Or
Basic data type
There are three basic data types which are integral data type, void and floating data type.
Include files
Class Declaration
Class functions
and definitions
Main functions
program
1. The class declaration is placed in header file and the definition of the member function is placed
in other file.
2. This above approach enables the programmer to separate the abstract of the interface from the
implementation details.
3. Finally, the main program that uses the class is placed in third file, which includes the previous two
files as well as any other files required.
Header file is a file in which the in-built functions are defined, included and declared.
Arrays
Types of Arrays
Single (one) dimensional array – Refer to notes of data structure
Two-Dimensional Array
Multi-Dimensional Array
#include<isotream.h>
#include<conio.h>int main()
{
int A[3][3],B[3][3], C[3][3], r, c, ch, ans;
clrscr();
cout<<”Enter first matrix”;
for (r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cin>>A[r][c];
}
}
cout<<”Enter second matrix”;
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cin>>B[r][c];
}
}
cout<<”Matrix A is”<<endl;
for (r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<A[r][c];
}
}
cout<<”Matrix B is”<<endl;
for (r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<B[r] [c];
Principles of OOP’s
Q. What is object oriented programming? Enlist the features of object oriented programming.
Ans. : “Object oriented programming is an approach that provides a way of modularizing programs by
creating partitioned memory area for both data and functions that can be used as templates for creating
copies of such modules on demand.”
The features of OOP’s are:
i. Emphasis is on data rather than procedure.
ii. Programs are divided into what are known as objects.
iii. Data structures are designed such that they characterize the objects.
iv. Follows bottom-up approach in program design.
v. Data is hidden and cannot be accessed by external functions.
vi. Objects may communicate with each other through functions.
vii. New data and functions can be easily added whenever necessary.
viii. Functions that operate on the data of an object are tied together in data structure.
1. Objects:-
i. Objects are the basic runtime entities in an object oriented system. Eg: they may represent a person,
place, a bank account or any item that the program must handle.
ii. Programming problem is analyzed in terms of objects & the nature of communication between them.
iii. Program objects should be chosen such that they match closely with the real-world objects.
OBJECT:
Student
DATA:
Name Way of representing objects
Marks
FUNCTION:
Total()
Avg()
show()
2. Classes:-
i. Class is a way to bind data and its associated functions together.
ii. An object is nothing but a variable, whose data type is class.
iii. Once a class has been defined, user can define any number of objects belonging to that class.
iv. A class is a collection of objects of similar type.
v. Example: mango, orange & apple are members of the class fruit.
3. Data Abstraction:-
i. Abstraction refers to the act of representing essential features.
ii. Classes use the concept of abstraction and are defined as a list of abstract attributes such as
size, weight & cost, and functions to operate on these attributes.
ii. The attributes are sometimes called data members because they hold information.
5. Inheritance:-
i. Inheritance is the process by which objects of one class acquire the properties of objects of
another class.
ii. In OOP’s, inheritance stands for reusability. This means that the additional features can be
added to an existing class without modifying it.
6. Polymorphism:-
i. It is another important OOP concept. Polymorphism means the ability to make one form.
Eg- if the 2 operands are string, then it would (the operation) produce a third string.
ii. It plays an important role in allowing objects having different internal structures to share the
external interface.
Shape
Draw()
7. Dynamic Binding:-
i. Binding refers to the linking of a procedure call to the code to be executed in response to the
call.
ii. Dynamic binding means that the code associated with a given prodecure call is not known
until the time of the call at run time.
iii. It is associated with polymorphism & inheritance.
8. Message Passing:-
i. The process of communication in OOP’s involves following steps.
a. Creating classes that define objects and their behavior.
b. Creating objects from class definition.
c. Establishing communication among objects.
Eg- employee salary (name);
Class classname
{ private:
Declaration of data members;
Declaration of member functions;
public:
Declaration of data members;
Declaration of member functions;
};
The body of a class is enclosed with braces and terminated with a semicolon. The data members and
member functions are grouped together under private and public. The members, declared as private can be
accessed only within the class and public can be accessed from outsidethe class also. It’s a key feature of
OOP’s .
Class item
{ private:
int number;
float cost;
public:
void getdata(int a, int b);
void putdata();
};
2. Private members-
They are the members that are hidden from the outside world. The private members implement theOOP’s
concept of data hiding. They can be used only by member functions and friends.
Eg- class x
{
private: a;
Q. What is an object ? Describe how members of a class be accessed using object of that class?
Ans. : 1) An object is a variable whose datatype is class.
2) User can have more than one object for a class. The objects can be declared as :
class_name object1, object2.
Accessing members of class using objects.
1. The private data of a class can be accessed only through the member function of that class.
2. To use a member function, the dot operator (.) connects the object name & the member function.
The dot operator is called as class member access operator.
3. Syntax: objectname. functionname (actual argument)
4. Eg : class student
{
int a;
public: getdata(int a)
{
cout << “Please enter the number”;
}
display ()
{
cout << “The number is”<< a;
}
};
void main ()
{
student a; //object is created
a.getdata(); //getdata() member function is accessed through object ‘a’
a.display(); //display() is accessed through object ‘a’
}
Here, student is a class with object name a and the dot operator is used to access the data by
a.getdata(); function.
count
Static Member function
A member function is that access only the static members of a class may be declared as static. This can be
done by putting keyword “static” before the function declaration in the class definition.
Eg- class x
{ static int count;
static void show (void)
By just passing the keyword static in the class definition, you have declared the function static.
A static member function is different from other member function in various ways:
1. A static member function can access only static members (functions or variables) of the same class.
2. A static member function is invoked by using the class name instead of its objects as given below
classname :: function-name
Example:
class A
{ static int a = 0;
int b;
public : void getdata(int c)
{
b = c;
a = a + 1;
}
void putdata()
{
cout<<c;
cout<< “counter:”<<a;
}
};
void main()
{ A a1, a2;
a1.getdata(5);
a1.putdata();
a2.getdata(10);
a2.putdata();
}
In above example, static ‘a’ keeps the counter track for both a1 and a2.
}
};
void main ()
{ item order[10]; //Array of order to contain 10 objects of item type.
for (i = 0; i <=9; i++)
{
cout<<order[i].getdata();
cout<<order[i].putdata();
}
}
The array order contains 10 objects, namely, order [0], order [1], order [2] …& so on. To access data
member of item no 3 object in the array , we will give
order 2. itemno
Similary , to invoke putdata() for 7th object in the array order, we will give
order 6.putdata();
Any class which contains many constructors with different parameters is called multiple constructor.
Ans. : A constructor for a class is needed so that the compiler automatically initializes an object as soon as
it is created. A class constructor, if defined, is called whenever a program creates an object of that
class.
Operator Overloading
Q. What is operator overloading? Explain with example.
Ans. :
i) The mechanism of giving some special meaning to an operator is called as operator overloading.
ii) C++ permits to add two variables of user defined data types with the same syntax as the basic types.
This means that C++ has the ability to provide operators with a special meaning for a data type. This
is nothing but operator overloading.
iii) Operator overloading provides a flexible option for the creation of new definitions for most of the
C++ programs.
Q. What is Operator Overloading? State the three steps involved in operator overloading.
Ans. :
i) The mechanism of giving special meanings to an operator is known as operator overloading.
ii) Operator overloading provides a flexible option for the creation of new definitions for most of the
C++ operators.
iii) To define an additional task to an operator, a special function called operator function is used.
iv) The process of overloading involves the following steps:
1) First create a class that defines the data type that is to be used in the overloading operation.
2) Declare the operator function operation op( ) in the public part of the class. It may be either a
member function or a friend function.
3) Define the operator function to implement the required operations.
vi) Unary operators, overloaded by means of a friend function take one reference argument.
vii) Binary operators overloaded through a member function takes one explicit argument.
viii) Binary operators overloaded through a friend function takes two explicit arguments.
ix) When using binary operators overloaded through a member function, the left hand operator must be
an object of the relevant class.
x) Binary arithmetic operators such as +, -, * and / must explicitly return a value. They must not
attempt to change their own arguments.
Q. Write a program in C++ to overload unary minus operator, so that unary minus operator
when applied to an object should change the sign of each of its data items.
Ans. : #include<iostream.h>
#include<conio.h> class space
{
private: x, y, z;
public: space(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void display (void);
void operator –( ); //overloaded unary -
};
void space::display ( )
{
cout<<x<<”\t” <<y<<”\t”<<z;
}
Explanation:
In the above program, the operator ‘-’ is overloaded. When the main function goes to ‘-s’ (where
s is object of class space), it activities and overloads the function and the operator.
Output:
Enter three numbers10 -20
30
-s: -10 20 -30
Q. Write a program in C++ to overload binary + operator for addition of two complex numbers.
Ans. : #include<iostream.h>
#include<unio.h>
class complex
{
private: float x; //real no.
float y ; //imaginary no.
public:
complex (float, float);
complex operator + (complex);
void display (void);
};
c1. display ( );
cout<< “ c2 =”;
c2. display ( );
cout<< “c3=”;
c3.display ( );
}
Virtual Function
Q What is polymorphism? Explain runtime and compile time polymorphism.
OR
What does polymorphism do in C++? How is the same achieved at (i) Compile time (ii)
Runtime
Ans. :
1. “Polymorphism refers to identically named methods(member functions) that have
different behaviour depending on the type of object they refer.”
2. Polymorphism simply means “one name, multiple forms”
3. The type of polymorphism and their examples are shown in the following figure.
Q. Explain the difference between static and dynamic binding with example.
Ans. :
1. In static binding, object is bound to its function call at compile time. While in dynamic binding
selection of the appropriate function is done dynamically at runtime.
2. In static binding, compiler knows the function information (argument type, number etc) at the
compile time itself so as to select appropriate function for a particular call (also called early binding).
In dynamic binding, function is linked with a particular class much later after the compilation (also
known as late binding).
3. Function overloading and operator overloading are the examples of static binding. Virtual functions
In above example. compiler does not know which show( ) function is executed either of base class or
derived class. So compiler defers this decision and at the run time select appropriate function using concept
of virtual function.
In dynamic binding, classes are defined as :-
class A
{ int x;
public: virtual void show ( )
{
cout<<” Base class”;
}
};
class B: public A
{ int y;
public: void show( )
{
cout<<” Derived class”;
}
};
Q. State any eight basic rules for virtual functions that satisfy the compiler requirements.
Ans. :
When virtual functions are created for implementing late binding, we should observe following basicrules that
satisfy the compiler requirements:
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it is not used.
6. The prototypes of the base class version of virtual function & all derived class version must be
identical. If two functions have different prototypes, then C++ considers them as overloaded
functions & not as virtual functions.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. A base pointer can point to any type of derived object, the reverse is not true. i.e. we cannot use a
pointer to derived class to access an object of the base type.
9. When base pointer is only relative to its base type.
10. Virtual functions are defined in base class; they need not be redefined in derived class.
Inheritance
Q. Explain Inheritance in detail.
Ans. :
Inheritance is the process of creating new classes from an existing class. The existing class is known as the
base class and the newly created class is known as the derived class. The derived classinherits all capabilities
of the base class. It can also add some more features to this class . The base class is unchanged by the process.
The advantages of inheritance are as follows.
The idea of reusability of the code. Inheritance allows the additional features to an existing class
without modifying it.
It increases the reliability of the code.
The major reason behind using inheritance is the capability to express the inheritance relationship
that ensures the closeness with the real-world objects.
One reason is transitive nature of inheritance. For example, C is a class which is derived from base
class B which itself is a derived class of A, that is, C can also access A (inheritance is transitive)
Syntax:
Class derived classname : visibility mode base classnameExample:
class B
{
int a;
A Base class
B Derived class
ii) Multiple Inheritance:
When a derived class inherits from multiple base class, it is known as multiple inheritance.
Base class
A B
C Derived class
A Base class
B C D
Derived class
A Base class
B Derived class of A
Base class of C
C Derived class of B
A
Hierarchical
B C
Multiple
Example:class A
{
int a;
public: int b;
void getdata( )
{
cout<<”Enter the value of a”;
cin >>a;
}
};
class B: public A
{
public: void putdata( )
{
cout<<”Enter the value of b”;
cin>>b;
}
};
int main ( )
{
B b1;
b1.getdata();
b1.putdata();
return 0;
}
In the program, as we have defined public A for derived class B which means it can access only the
public members of class A and not the private members i.e. it can access getdata( ), int b but not int a
2. Private visibility mode:
The private derivation means the derived class can access the public members of the base class privately.
The public and protected member of the base class becomes private members of the derived class. The
inherited members become private in the derived class, they cannot be to be thebase class of any other class.
Example:
class super
{ private: int x;
void check(int);
Nirmala Memorial Foundation Junior College of Commerce and Science Page 69
public: int y;
void display(void);
protected: int z;
void getval ( );
};
class sub : private super
{
private: int a;
void getdata ( );
public: int b;
protected:int c;
void putdata( );
};
#include<iostream.h>
#include<conio.h>
class base
{ public: int a; };
class D1: public base //D1 inherits base
{ public : int b; };
class D2: public base //D2 also inherits base
{ public: int c; };
As both D1 and D2 inherits base, they both have copies of base. However, D3 inherits both D1 and
D2. This means that there are 2 copies of base present in an object of type D3. Therefore, in an
expression like
ob.a = 25;
which “a” is being referred to the one in D1 or the one in D2? Because there are 2 copies of base
present in the object ob, there are 2 ob.a. Thus, the statement is inherently ambiguous.
So there are two ways of remedy.
The first one is to apply the scope resolution operator (::) to “a” and manually select one “a”.
Example:
int main ( )
{
D3 ob;
ob.D1::a = 25; //scope resolution used D1’s “a”
ob.b = 50;
ob.c = 75;
ob.total = ob.D1::a + ob.b + ob.c;
cout<<ob.D1::a<< “/t” <<ob.b << “/t”<<ob.c<< “/t” << ob.total << “/n”
Example:
#include<iostream.h>
#include<conio.h>
class base
{ public: int a; };
class D1: public virtual base //D1 inherits base as virtual
{ public: int b; };
25 50 75 150
In the above program, both D1 and D2 have inherited base as virtual, any multiple
inheritance involving them will cause only one copy of base to be present. Therefore, in D3, there is
only one copy of base, therefore ob.a=10 are perfectly valid and unambiguous. Another thing is that
even though both D1 & D2 specify base as virtual, base is still present in any objects of either type.
Input stream
Data program
files
Example:
ofstream object.put (character variable)
ofstream outf;
outf.open(“xyz”);
char c = ‘A’;
outf.put (c);
2. get( ):-
This function is used to read character from a file, specified by object ifstream. Syntax:
3. write( ) :-
This function is used with object of ofstream & it is used to store data into file in binary mode, that is,
general user cannot read data of file by using ‘type’ command. Here all the variables of different type are
first of all converted into (char*) i.e. pointer to character and second parameter isnumber of bytes required
to store given variable.
Syntax:
write ((char*)& variable, size of (variable);
Example:
struct s;
{
char n[20];
int t;
};
The above declaration will create a variable ptr, which is a pointer variable and which will point to a
variable, whose data type is integer.
1) The data type of ptr is not integer, but data type of variable which ptr will point is integer.
2) Advantages of using pointers are as under:
i) It allows passing variables, arrays, functions, strings, structures, and objects as function arguments.
ii) It allows to return structured variables from functions
iii) It supports dynamic allocation and deallocation of memory segments.
iv) By using pointers, variables can be swapped, without physically moving them
Q. What is call by reference? What is the advantage of call by reference over call by value?
Ans. : A function can be called by two methods:
i) Call by value
ii) Call by Reference
1) When a function call passes arguments by value (call by value) the called function creates a new set
of variables and copies the values of arguments into them.
2) The function does not have access to the actual variables in the calling program and can only work on
the copies of values.
3) Provision of reference variables in C++ permits to pass parameters to the function by reference.
4) When pass arguments by reference (call by reference) the formal arguments in the called function
become alises to the actual arguments in the calling function. This means that when the function is
working with its own arguments, it is actually working on the original data.
5) The mechanism of call by value is good, if the function does not need to alter the values of the
original variables in the calling program.
#include<iostream.h>
void swap(int*, int*)
void main( )
{
int a,b;
cout<< “Enter 2 nos.”;
cin>>a>>b;
swap(&a, &b); //call by reference
cout<< “a=” <<a;
cout<< “b=” <<b;
}
void swap (int *a, int *b) //function definition
{
int t;
t = *a; //assigns the value at address a to t.
*a = *b; //put the value at b into a.
*b = t; //put the value at t into b.
}
#include<iostream.h> class A
{
public: int area(int s)
{
return(s*s);
}
int area (int l, int b)
Q. What are default arguments? Give the advantages of using default arguments.
Ans. :
i) C++ allows to call a function without specifying all its arguments. In such case, the function assigns
a default value to the parameter, which does not have a matching argument in the function call.
ii) Default values are specified when the function is declared.
iii) Consider the following function:
float area(int r, float Pi = 3.14);
The above function declares default value 3.14 to the arguments Pi. Suppose, a subsequent function
call like:
A = area(7); //one argument is missing
At this time the value of Pi will be 2.5 and not the default value 3.14.
iv) Advantages of using default arguments:
1) They are useful in situations, where some arguments have same values.
2) It provides better flexibility to programmers by allowing to use particular arguments that are
meaningful to particular solution.
3) Use default arguments to add new parameters to the existing functions.
4) Default arguments can be used to combine similar functions into a single function.
Q. What is HTML?
Ans:
1. It is the simplest, text oriented programming language.
2. HTML Stands for HyperText Mark-Up Language.
3. Hypertext is an ordinary text and adding text with extra features such as formatting, images,
multimedia and links to other documents.
4. Markup is the process of taking ordinary text and adding extra symbols, such as editor’s proof
reading symbols. Each of the symbols used for markup in HTML is command that tells the browser
how to display the text.
5. Markup Languages are special type of computer languages. They are concerned with only parts of
documents according to their functions.
6. HTML is not a programming language in real sense.
<HEAD>
<TITLE>
</TITLE>
</HEAD>
<BODY>
Actual Document
</BODY>
</HTML>
P Tag:
<p align = left>
<p align = right>
<p align = center>
<pre> the preformatted text tag
This tag will display the text in the same manner as it has been typed in Notepad
</pre>
<pre></pre>
Heading Tag:
<h1></h1>
<h2></h2>
<h3></h3>
<h4></h4>
<h5></h5>
<h6></h6>
These tags are used for giving size to the text. <h1> is largest size and <h6> is lowest size.
Marquee Tag:
<marquee>
<marquee bgcolor=”red”>
<marquee direction=right></marquee>
<marquee direction=left> </marquee>
<marquee direction=up></marquee>
<marquee direction=down></marquee>
</marquee>
LIST
Lists are everywhere on the refrigerator for groceries, on a scratch pad for the to-do lists, in front of
the books for the table of contents etc. hence list are the one of the most natural ways to organize
information.
Syntax :
Unorder list <ul></ul>
List item <li></li>
Order list type <ol type =A> (Starts the list from A, B, C…)
Order list type <ol type = a> (Starts the list from a, b, c…)
Order list type <ol type = i> (starts the list from i, ii, iii…)
Order list type <ol type = I> (Starts the list from I, II,III…)
Order list start <ol start =any number> (Starts the list from any number)
Tables
The tables can be used to enhance the layout of the text on your page. Tables are used to arrange data in
Row and Column format. In the world of the web , HTML tables have become, one the most useful and
powerful tools for formatting web pages.
<table>--</table>
<tr>--</tr> (The table row element creates a horizontal row of cells and contains the table headings and table
data.)
<th>--</th> (The Table Heading element creates the heading cells)
<td>--</td> (The Table Data element creates individual cell.)
In similar manner if you want to align the data within a cell or to the entire column
The syntax is
<td align=center>
Or
<tr align=center>
Note: If alignment is specified in the <tr>, then the data of all the cells in that <tr> i.e Table Row will be
aligned in that <tr> i.e Table Row will be aligned in the specified position
Vertical Alignment:
The tag <valign=”top/bottom/middle>
It will vertically align the data in the cell according to the attribute specified in the tag
Table. Html
E.g
<html>
<head>
<title> Table Spanning </title>
</head>
<body>
<table bgcolor ="red"border=”1” width=100%>
<caption> Marklist </caption>
<tr>
<th> Names </th>
<th> English </th>
<th> Maths</th>
</tr>
<tr>
<td > Reena </td>
<td > 50</td>
<td > 40 </td>
</tr>
<tr>
<td> Seema</td>
Hypertext is the very essence and heart of the World Wide Web. Without Hyperlinks, the web would
nothing but a bunch of separate, unrelated files. Linking different files into a single unit can be seen on a
small scale in the composition of on HTML page, on a larger scale in the construction of a web site.
USES OF HYPERLINKS:
Images
VB SCRIPT
Q. What is script? Explain VB script. Give one
example. Ans. :
1. A script means a series of commands that will be executed by host environment (Server).
2. Scripting enables us to set and store variables, perform operations on variable .
3. By integrating script, it makes static HTML page active or dynamic, so that is called
asDHTML i.e. dynamic hyper text mark up language.
4. Scripting languages are special programming languages. These are used on web pages
tocontrol different elements of the page including control frames and browser
interface.
5. There are two famous scripting languages:
i) V.B. Script
ii) Java Script
6. VB Script allows web author to write small scripts, that will be executed on users.
7. Another importance of VB script is increased functionality introduced to the web authors
in the form of applet, plugs-in, active x-command and objects. Each of these things can be
usedto add extra functions and interactivity to the web pages.
8. Examples:-
<HTML>
<HEAD>
<TITLE> Hello World </TITLE>
<SCRIPT> Hello World</TITLE>
<SCRIPT LANGUAGE = “VB SCRIPT”>
SUB can ( )
Msg BOX “Hello World”END
Q. What is <SCRIPT>
tag?Ans. :
1. The VB SCRIPT can be placed in <SCRIPT> start tag and </SCRIPT> end tag. It contains attribute
LANGUAGE = “VB SCRIPT” as shown:
<SCRIPT LANGUAGE = “VB Script>
<Scripting code>
</SCRIPT>