You are on page 1of 20

Q1] Programming Paradigms

it is a method to solve a problem using tools and techniques that are available
to us following some approach. There are lots for programming language that
are known but all of them need to follow some strategy when they are
implemented and this methodology/strategy is paradigms. Apart from varieties
of programming language there are lots of paradigms to fulfil each and every
demand.

1.1 Imperative programming paradigm:


It is one of the oldest programming paradigm. It features close
relation to machine architecture. It is based on Von Neumann
architecture. It works by changing the program state through
assignment statements. It performs step by step task by changing
state. The main focus is on how to achieve the goal. The paradigm
consist of several statements and after execution of all the result is
stored.
Adavantages
1. Very simple to implement
2. It contains loops, variables etc.
Disadvantage:
1. Complex problem cannot be solved
2. Less efficient and less productive
3. Parallel programming is not possible
Examples C : Fortan :

1.1.1 Procedural programming paradigm –


There is no difference in between procedural and imperative approach. It has
the ability to reuse the code and it was boon at that time when it was in use
because of its reusability.
Examples C : C++ : Java :
1.1.2 Object oriented programming –
The program is written as a collection of classes and object which are meant
for communication

Advantages:
• Data security
• Inheritance
• Code reusability
• Flexible and abstraction is also present
Examples C++ , java

1.1.3 Parallel processing approach –


Parallel processing is the processing of program instructions by dividing them
among multiple processors.
C/C++ also supports because of some library function.
1.2 Declarative programming paradigm:
It is divided as Logic, Functional, Database. In computer science
the declarative programming is a style of building programs that expresses
logic of computation without talking about its control flow.

1.2.1 Logic programming paradigms –


It can be termed as abstract model of computation. It would solve logical
problems like puzzles, series etc. In logic programming we have a
knowledge base which we know before and along with the question and
knowledge base which is given to machine, it produces result.
In logical programming the main emphasize is on knowledge base and the
problem. The execution of the program is very much like proof of
mathematical statement, e.g., Prolog
1.2.2 Functional programming paradigms –
The functional programming paradigms has its roots in mathematics and it
is language independent. The key principal of this paradigms is the
execution of series of mathematical functions.
EX perl, javascript
Database/Data driven programming approach –
This programming methodology is based on data and its movement.
Program statements are defined by data rather than hard-coding a series
of steps. A database program is the heart of a functions.
business information system and provides file creation, data entry, update,
query and reporting functions.
EX SQL

Q2] loops
Q3] conditional statement
Q4] Include directive
#include is a way of including a standard or user-defined file in the program
and is mostly written at the beginning of any C/C++ program. This directive
is read by the preprocessor and orders it to insert the content of a user-
defined or system header file into the following program. These files are
mainly imported from an outside source into the current program. The
process of importing such files that might be system-defined or user-
defined is known as File Inclusion. This type of preprocessor directive tells
the compiler to include a file in the source code program. Here are the two
types of file that can be included using #include:
2. Header File or Standard files: This is a file which contains C/C++
function declarations and macro definitions to be shared between
several source files. Functions like the printf(), scanf(), cout, cin and
various other input-output or other standard functions are contained
within different header files
1. User-defined files: These files resembles the header files, except
for the fact that they are written and defined by the user itself. This
saves the user from writing a particular function multiple times.
Once a user-defined file is written, it can be imported anywhere in
the program using the #include preprocessor.

Q5] Namespace standard

Q6] Basics of oop


TABLE OF CONTENT:
1. Introduction
2. Class
3. Objects
4. Encapsulation
5. Abstraction
6. Polymorphism
7. Inheritance
8. Dynamic Binding
9. Message Passing
Object-oriented programming – As the name suggests uses objects in
programming. Object-oriented programming aims to implement real-world
entities like inheritance, hiding, polymorphism, etc in programming. The
main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.

Q7] FEATURES
There are three major features in object-oriented programming that makes
them different than non-OOP languages: encapsulation, inheritance and
polymorphism.
• Encapsulation Enforces Modularity. ...
• Inheritance Passes "Knowledge" Down. ...
• Polymorphism Takes any Shape. ...
• OOP Languages.

• Polymorphism: Polymorphism means having many forms,


In other words polymorphism means the ability of a
message to be displayed in more than one
forms.Polymorphism is one of the important features of
OOP and it can be of two types.

Method Overloading

Method Overriding.
• Inheritance: The capability of a class to derive properties
and characteristics from another class is called
Inheritance.Inheritance is one of the most important
feature of Object Oriented Programming.

• Sub Class: The class that inherits properties from another


class is called Sub class or Derived Class.

• Super Class:The class whose properties are inherited by


sub class is called Base Class or Super class.

• Encapsulation: is a process of combining data members


and functions in a single unit called class.This is to prevent
the access to the data directly, the access to them is
provided through the functions of the class.It is one of the
popular feature of Object Oriented Programming(OOPs)
that helps in data hiding.

• Abstraction: Now comes the last and very essential feature


of OOP in java that is abstraction; Hiding the
unwanted/unnecessary details while showing out the most
essential details.

• To make a class abstract,you’ll have to use abstract


keyword with class’ name or you can declare an abstract
method through which the class will be called as Abstract
class.
Q8] Identify features of program
All programs

Q9]Constructor

Q10]Destructor

Q11] New and Delete operator

New and delete operator used for dynamic memory. allocating memory at
the time of run time. The most important use is flexibility provided to
programmers. C++ supports dynamic allocation and deallocation of
objects using the new and delete operators. These operators allocate
memory for objects from a pool called the free store. The new operator
calls the special function operator new, and the delete operator calls
the special function operator delete.
you can only overload existing operators The new operator is an
operator which denotes a request for memory allocation on
the Heap. If sufficient memory is available, new operator initializes
the memory and returns the address of the newly allocated and
initialized memory to the pointer variable.
Syntax to use new operator: To allocate memory of any data type,
the syntax is:

pointer-variable = new data-type; we can use any data type

Delete operator
It is an operator. It de-allocates the memory dynamically. It should only be used
either for the pointers pointing to the memory allocated using the new operator
or for a NULL pointer. This operator calls the destructor after it destroys the
allocated memory. It is faster Using the delete operator also causes the
class destructor (if one exists) to be called.

Q12]Friend concept

Q13] Abstract and virtual class


An abstract class is a class that is designed to be specifically used as a base class. An
abstract class contains at least one pure virtual function. You declare a pure virtual function
by using a pure specifier (= 0) in the declaration of a virtual member function in the class
declaration.
• Both virtual and abtract classes allow you to extend the class (i.e. create child
classes that inherit non-private methods and variables)
• A virtual class can be instantiated directly, whereas an abstract class cannot
• Both virtual and abstract classes can contain virtual methods (virtual methods can
have a default implementation that is inherited by child classes,
whereas abstract methods can only be signatures, and must be implemented in
child classes)
• Only abstract classes may contain abstract methods
In practice, I haven't seen much practical difference between the two.
The abstract and virtual modifiers, in my mind, are more important for methods (as
opposed to classes).

Q14] Operator overloading

Polymorphism (or operator overloading) is a manner in which OO systems


allow the same operator name or symbol to be used for multiple operations.
That is, it allows the operator symbol or name to be bound to more than one
implementation of the operator. A simple example of this is the “+” sign.

. (dot) ,:: , ?:, sizeof these operator can be overload.


Important points about operator overloading
1) For operator overloading to work, at least one of the operands must be a
user defined class object.
2) Assignment Operator: Compiler automatically creates a default
assignment operator with every class. The default assignment operator does
assign all members of right side to the left side and works fine most of the
cases (this behavior is same as copy constructor). See this for more details.
3) Conversion Operator: We can also write conversion operators that can
be used to convert one type to another type. Overloaded conversion
operators must be a member method. Other operators can either be member
method or global method.
4) Any constructor that can be called with a single argument works as a
conversion constructor, means it can also be used for implicit conversion to
the class being constructed.

Q15] Polymorphism
polymorphism as the ability of a message to be displayed in more than one
form.

• Compile time Polymorphism


• Runtime Polymorphism
1)Compile time polymorphism: This type of polymorphism is achieved by
function overloading or operator overloading.

1.1) Function overloading: When there are multiple functions with same
name but different parameters then these functions are said to
be overloaded. Functions can be overloaded by change in number of
arguments or/and change in type of arguments.

1.2)Operator overloading: C++ also provide option to overload


operators. For example, we can make the operator (‘+’) for string class to
concatenate two strings. We know that this is the addition operator whose
task is to add two operands. So a single operator ‘+’ when placed between
integer operands , adds them and when placed between string operands,
concatenates them.: C++ also provide option to overload operators. For
example, we can make the operator (‘+’) for string class to concatenate two
strings. We know that this is the addition operator whose task is to add two
operands. So a single operator ‘+’ when placed between integer operands ,
adds them and when placed between string operands, concatenates them.

2)Runtime Polymorphism: This type of polymorphism is achieved by


Function Overriding.
• Function overriding: on the other hand occurs when a derived
class has a definition for one of the member functions of the base
• ( parent class ) class. That base function is said to
be overridden.

Virtual fuction = it is a member function that we declared within base


class and redefined by a derived class
Q16] Inheritance
The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important feature
of Object Oriented Programming.
Sub Class: The class that inherits properties from another class is called
Sub class or Derived Class.
Super Class:The class whose properties are inherited by sub class is called
Base Class or Super class.
Consider a group of vehicles. You need to create classes for Bus, Car and
Truck. The methods fuelAmount(), capacity(), applyBrakes() will be same for
all of the three classes. If we create these classes avoiding inheritance then
we have to write all of these functions in each of the three classes as shown
in below figure:

You can clearly see that above process results in duplication of same code 3
times. This increases the chances of error and data redundancy. To avoid
this type of situation, inheritance is used. If we create a class Vehicle and
write these three functions in it and inherit the rest of the classes from the
vehicle class, then we can simply avoid the duplication of data and increase
re-usability. Look at the below diagram in which the three classes are
inherited from vehicle class:
Using inheritance, we have to write the functions only one time instead of
three times as we have inherited rest of the three classes from base
class(Vehicle).
Modes of Inheritance
1. Public mode: If we derive a sub class from a public base class.
Then the public member of the base class will become public in the
derived class and protected members of the base class will become
protected in derived class.
2. Protected mode: If we derive a sub class from a Protected base
class. Then both public member and protected members of the
base class will become protected in derived class.
3. Private mode: If we derive a sub class from a Private base class.
Then both public member and protected members of the base class
will become Private in derived class.

Note : The private members in the base class cannot be directly accessed in
the derived class, while protected members can be directly accessed

Types of Inheritance in C++


1. Single Inheritance: In single inheritance, a class is allowed to inherit
from only one class. i.e. one sub class is inherited by one base class
only.
3. Multilevel Inheritance: In this type of inheritance, a derived class is
created from another derived class.

Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};

2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a


class can inherit from more than one classes. i.e one sub class is
inherited from more than one base classes.

Syntax:
class subclass_name : access_mode base_class1, access_mode
base_class2, ....
{
//body of subclass
};
4. Hierarchical Inheritance: In this type of inheritance, more than one sub
class is inherited from a single base class. i.e. more than one derived class
is created from a single base class.

5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by


combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:
6. A special case of hybrid inheritance : Multipath inheritance:
A derived class with two base classes and these two base classes have one
common base class is called multipath inheritance. An ambiguity can arrise
in this type of inheritance.

Q17]Access specifier

Q18]File handling in c++

What is meant by file handling in C++?


File handling in C++ is a mechanism to store the output of a program in a file
and help perform various operations on it. Files help store these data
permanently on a storage device. The term “Data” is commonly referred to as known
facts or information.

In C++, files are mainly dealt by using three classes fstream, ifstream,
ofstream available in fstream headerfile.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.

Now the first step to open the particular file for read or write operation.
We can open file by
1. passing file name in constructor at the time of object
creation
2. using the open method
Open File by using constructor
ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
ifstream fin(filename, openmode) by default openmode = ios::in
ifstream fin(“filename”);
Open File by using open method
Calling of default constructor
ifstream fin;
fin.open(filename, openmode)
fin.open(“filename”);

Modes :
Member Stands
Constant For Access

File open for reading: the internal stream buffer


in * input supports input operations.

File open for writing: the internal stream buffer


out output supports output operations.

Operations are performed in binary mode rather than


binary binary text.

ate at end The output position starts at the end of the file.

All output operations happen at the end of the file,


app append appending to its existing contents.

Any contents that existed in the file before it is open


trunc truncate are discarded.

Default Open Modes :


ifstream ios::in

ofstream ios::out

fstream ios::in | ios::out

Q19] reading file and writing to file

The fstream library allows us to work with files.

To use the fstream library, include both the


standard <iostream> AND the <fstream> header file:
There are three classes included in the fstream library, which are used to
create, write or read files:

Class Description

ofstream Creates and writes to files

ifstream Reads from files

fstream A combination of ofstream and ifstream: creates, reads, and writes to files

Create and Write To a File

To create a file, use either the ofstream or fstream class, and specify the name
of the file.

To write to the file, use the insertion operator (<<).

Read a File

To read from a file, use either the ifstream or fstream class, and the name
of the file.

Note that we also use a while loop together with the getline() function
(which belongs to the ifstream class) to read the file line by line, and to
print the content of the file:

Q20] writing objects

Given a file “Input.txt” in which every line has values same as instance
variables of a class.
Read the values into the class’s object and do necessary operations.
Theory :

The data transfer is usually done using '>>'


and <<' operators. But if you have
a class with 4 data members and want
to write all 4 data members from its
object directly to a file or vice-versa,
we can do that using following syntax :

To write object's data members in a file :


// Here file_obj is an object of ofstream
file_obj.write((char *) & class_obj, sizeof(class_obj));

To read file's data members into an object :


// Here file_obj is an object of ifstream
file_obj.read((char *) & class_obj, sizeof(class_obj));

Q21]file function

File Handling In C++


Files are used to store data in a storage device permanently. File handling
provides a mechanism to store the output of a program in a file and to perform
various operations on it.

A stream is an abstraction that represents a device on which operations of input


and output are performed. A stream can be represented as a source or
destination of characters of indefinite length depending on its usage.

In C++ we have a set of file handling methods. These include ifstream, ofstream,
and fstream. These classes are derived from fstrembase and from the
corresponding iostream class. These classes, designed to manage the disk files,
are declared in fstream and therefore we must include fstream and therefore we
must include this file in any program that uses files.

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream.

• ofstream: This Stream class signifies the output file stream and is applied to create
files for writing information to files
• ifstream: This Stream class signifies the input file stream and is applied for reading
information from files
• fstream: This Stream class can be used for both read and write from/to files.

All the above three classes are derived from fstreambase and from the
corresponding iostream class and they are designed specifically to manage disk
files.
C++ provides us with the following operations in File Handling:

• Creating a file: open()


• Reading data: read()
• Writing new data: write()
• Closing a file: close()

Moving on with article on File Handling in C++

Opening a File
Generally, the first operation performed on an object of one of these classes is to
associate it to a real file. This procedure is known to open a file.

We can open a file using any one of the following methods:


1. First is bypassing the file name in constructor at the time of object creation.
2. Second is using the open() function.

To open file use

open() function

void open(const char* file_name,ios::openmode mode);

we first create an object to class fstream and name it ‘new_file’. Then we apply the
open() function on our ‘new_file’ object. We give the name ‘new_file’ to the new file
we wish to create and we set the mode to ‘out’ which allows us to write in our file.
We use a ‘if’ statement to find if the file already exists or not if it does exist then it
will going to print “File creation failed” or it will gonna create a new file and print
“New file created”.

To read a file we need to use ‘in’ mode with syntax ios::in. In the above example,
we print the content of the file using extraction operator >>. The output prints
without any space because we use only one character at a time, we need to use
getline() with a character array to print the whole line as it is.
Close file

It is simply done with the help of close() function.

Syntax: File Pointer.close()

Q22] String function

A string is a sequence of characters. A C++ string is an object of the std::string


class. The characters are stored sequences of bytes with access to a single
character byte allowed.

C++ strings allocate memory dynamically. More memory can be allocated to the
string during run time if needed. Since there is no memory pre-allocation, no
wastage of memory. We can perform various operations on strings, including
comparisons, concatenation, conversion, etc.

std::string
The standard C++ library provides the string class which supports various string
operations. It is written as std::string.

To use this class, we must first include it into our workspace using the #include
preprocessor as shown below:

#include<string>
Next, we can declare our string using the string keyword. For example:

string name = "John";


The above statement will create a string named name to hold the value John.

String Functions:
• The strcpy() function copies one string into another.
• The strcat() function concatenates two functions.
• The strlen() function returns the length of a function.
• The strcmp() function compares two strings.
You will often want to manipulate strings. C++ provides a wide range of
functions that you can use for this. These functions are defined in the CString
class, hence, we have to include it in our code in order to use the functions. Let
us discuss some:

strcpy()
This is the string copy function. It copies one string into another string.

Syntax:
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The
function will copy the string string1 into the string 1.

strcat()
This is the string concatenate function. It concatenates strings.

Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be
concatenated. The above function will concatenate the string string2 to the end
of the string string1.

strlen()
This is the string length function. It returns the length of the string passed to it
as the argument.

Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be
determined. The above function will return the length of the string string1.

strcmp()
This is the string compare function. It is used for string comparison.

Syntax:
strcmp(string1, string2);
The above function will return 0 if strings string1 and string2 are similar, less
than 0 if string1<string2 and greater than 0 if string1>string2.

You might also like