C plus plus Notes

mobiprep (6).png

Basics of C plus plus

mobiprep (6).png

Basics Of OOPs

mobiprep (6).png

Classes and Objects

mobiprep (6).png

Polymorphism and Inheritence

mobiprep (6).png

Exception Handling

mobiprep (6).png

File Handling and templates

Heading

Q

1

What are the file streams?

LRM_EXPORT_207556595493866_20190724_1939

Ans

File streams allow input and output to files. In other words, file streams provide an advantage in that you can open a file on construction of a stream, and the file is closed automatically on destruction of the stream.

LRM_EXPORT_207556595493866_20190724_1939

Q

2

Explain the process of open, read, write and close files.

LRM_EXPORT_207556595493866_20190724_1939

Ans

a) Opening a file- To open an existing file, command ‘fopen’ is used.
b) Reading a file- To read an existing file, command ‘fscan or fgets’ is used.
c) Writing a file- To write to a file, command ‘fprintf and fputs’ is used.
d) Closing a file- To close a file, command ‘fclose’ is used.

LRM_EXPORT_207556595493866_20190724_1939

Q

3

Explain the role of seekg(), seekp() ,tellg() ,tellp() function in the process of random access in a file.

LRM_EXPORT_207556595493866_20190724_1939

Ans

a) seekg() is used to move the get pointer to a desired location with respect to a reference point.
b) seekp() is used to move the put pointer to a desired location with respect to a reference point.
c) tellg() is used to know where the get pointer is in a file.
d) tellp() is used to know where the put pointer is in a file.

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Explain the Standard Template Library and how it is working.

LRM_EXPORT_207556595493866_20190724_1939

Ans

programming data structures and functions such as lists, stacks, arrays, etc.
It has four components-
a. Algorithm(Sorting, searching, etc.)
b. Containers:
1. Sequence Containers(vector list, etc.)
2.Container Adaptors(queue, stack, etc.)
3. Associative Containers(multiset, map, etc.)
4. Unordered Associative Containers(unordered_set, etc.)
c. Iterators
d. Functions

LRM_EXPORT_207556595493866_20190724_1939

Q

5

Write a C++ program using function template to find the product of two integer or floating point type of data.

LRM_EXPORT_207556595493866_20190724_1939

Ans

#include <iostream>
using namespace std;
template <typename T>
T product (T x, T y)
{
return x * y;
}
int main()
{ cout<< "Product: " << product (3, 5) << endl;
cout << "Product: " <<product (3.0, 5.2) <<endl;
cout << "Product: " << product (3.24234, 5.24144) <<endl;
return 0;
}
Output- Product: 15 Product: 15.6
Product: 16.9945

LRM_EXPORT_207556595493866_20190724_1939

Q

6

Explain File operations.

LRM_EXPORT_207556595493866_20190724_1939

Ans

A file is an abstract data type. For defining a file properly, we need to consider the operations that can be performed on files like: create, write, read, reposition, delete files.

LRM_EXPORT_207556595493866_20190724_1939

Q

7

Write a C++ program involving reading and writing of classobjects in a file.

LRM_EXPORT_207556595493866_20190724_1939

Ans

#include <iostream>
#include <fstream>
using namespace std;
// Class to define the properties
class Employee
{
public:
string Name; i
nt Employee_ID;
int Salary;
};
int main()
{
Employee Emp_1;
Emp_1.Name="Aastha";
Emp_1.Employee_ID=2021;
Emp_1.Salary=20000;
//Writing this data to Employee.txt
ofstream file1;
file1.open("Employee.txt", ios::app);
file1.write((char*)&amp;Emp_1,sizeof(Emp_1));
file1.close();
//Reading data from Employee.txt
ifstream file2;
file2.open("Employee.txt",ios::in);
file2.seekg(0);
file2.read((char*)&amp;Emp_1,sizeof(Emp_1));
printf("\nName :%s",Emp_1.Name);
printf("\nEmployee ID :%d",Emp_1.Employee_ID);
printf("\nSalary %d",Emp_1.Salary);
file2.close();
return 0;
}
Output- Name: Aastha
Employee ID: 2021
Salary: 20000

LRM_EXPORT_207556595493866_20190724_1939

Q

8

Write a C++ program to update the contents of a file by accessing the contents randomly.

LRM_EXPORT_207556595493866_20190724_1939

Ans

#include&lt;fstream&gt;
#include&lt;iostream&gt;
using namespace std;
main()
{
ofstream myFile; //Object for Writing:
ifstream file; //Object for reading:
char ch[30]; //Arguments: name & size of array.
char text; //character variable to read data from file:

cout<< "Enter some Text here: "<<endl;
cin.getline(ch,30); //Getline function.

//opening file for Writing:
myFile.open("Hello.txt", ios::out);
if(myFile) //Error Checker:
{
myFile<<ch;
cout<<"Data store Successfully: \n\n"<<endl;
}
else cout<< " Error while opening file: "&<<endl;
myFile.close(); //file close:

//opening file for reading:
file.open("InfoBrother.txt", ios::in);
if(file) //Error Checker:
{
file.seekg(1, ios::beg); //skip first 1 bytes from beginning:
cout<<" The output (after skipping first 1 byte) is: ";
while(!file.eof()) //read data from file till end of file:
{
file.get(text); //read data:
cout<<text; //display data:
}
}
else cout<<"Error while Opening File: ">>endl;
file.close(); //file close:

return 0;
}

Output- Enter some Test here:
Hello Data Store Successfully:
The output (after skipping 1 byte) is: ello

LRM_EXPORT_207556595493866_20190724_1939