Professional Documents
Culture Documents
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.
Advantages:
• Data security
• Inheritance
• Code reusability
• Flexible and abstraction is also present
Examples C++ , java
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.
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.
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.
Q9]Constructor
Q10]Destructor
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:
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
Q15] Polymorphism
polymorphism as the ability of a message to be displayed in more than one
form.
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.
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
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
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.
Q17]Access specifier
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
ate at end The output position starts at the end of the file.
ofstream ios::out
Class Description
fstream A combination of ofstream and ifstream: creates, reads, and writes to files
To create a file, use either the ofstream or fstream class, and specify the name
of the file.
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:
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 :
Q21]file function
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:
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.
open() function
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
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 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.