C plus plus Notes
.png)
Basics of C plus plus
.png)
Basics Of OOPs
.png)
Classes and Objects
.png)
Polymorphism and Inheritence
.png)
Exception Handling
.png)
File Handling and templates
Heading
Q
1
What are the file streams?

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.

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

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.

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

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.

Q
4
Explain the Standard Template Library and how it is working.

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

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

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

Q
6
Explain File operations.

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.

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

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*)&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*)&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

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

Ans
#include<fstream>
#include<iostream>
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
