Skip to content
Ananda Technologies Pvt. Ltd.

Object Oriented Programming using C++ practicals — MSBTE

All 26 practicals of MSBTE K-scheme 313304 Object Oriented Programming using C++ (Sem 3), solved. Every question has 3 different programs with the real output, a line-by-line explanation and viva questions. Choose the answer you like for each practical and print your own practical file.

✅ Solved by Ananda TechnologiesDiplomaSemester 3MSBTE K-schemeCode 313304CO, IF, CM, CW, SE, IHQuestion papers →
Build your own practical file in 3 steps
  1. 1Tick one answer (A, B or C) for the questions you need — one practical or all.
  2. 2Press Generate PDF at the bottom. First time: free account (asked only once).
  3. 3Choose Save as PDF in the print window. Print again any time.

💡 Explain buttons are for your understanding — explanations are never printed.

Practical 1Compulsory

Evaluate an expression using input / output functions

Aim: Read values with cin, evaluate an expression and print the result with cout.

QWrite a program to evaluate any expression using cin and cout.

Showing answer A of 3 · tap A, B or C to see the others

Output

Enter principal, rate and time: 10000 8.5 3
Simple interest = 2550
Total amount    = 12550
#include <iostream>
using namespace std;

int main()
{
    float p, r, t, si;
    cout << "Enter principal, rate and time: ";
    cin >> p >> r >> t;
🎤 Viva questions (5)
Q. What is cin and cout?
A. Objects of the iostream library: cin reads from the keyboard, cout prints on the screen.
Q. What are << and >> called?
A. << is the insertion (put-to) operator, >> is the extraction (get-from) operator.
Q. What is the use of endl?
A. It inserts a new line and flushes the output buffer.
Q. Which header file is needed for cin/cout?
A. iostream
Q. What does using namespace std mean?
A. All names of the std namespace can be used without writing std::.

Practical 2Compulsory

Scope resolution operator, memory management operators and manipulators

Aim: Use the :: operator, allocate and free memory with new and delete, and format output with manipulators.

Q1Program 1: use the scope resolution operator (::).

Showing answer A of 3 · tap A, B or C to see the others

Output

Local  m = 20
Global m = 10
Global m after change = 15
#include <iostream>
using namespace std;

int m = 10;          // global

int main()
{
    int m = 20;      // local

Q2Program 2: use the memory management operators new and delete.

Showing answer A of 3 · tap A, B or C to see the others

Output

Enter an integer: 42
Value at p = 42
Value at q = 4.75
Memory released.
#include <iostream>
using namespace std;

int main()
{
    int *p = new int;
    float *q = new float(4.75f);

Q3Program 3: use manipulators to format the output.

Showing answer A of 3 · tap A, B or C to see the others

Output

Subject        Marks
Maths             78
Physics           64
C++               91
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << setw(12) << left << "Subject" << setw(8) << right << "Marks" << endl;
    cout << setw(12) << left << "Maths"   << setw(8) << right << 78 << endl;
🎤 Viva questions (5)
Q. What is the scope resolution operator?
A. :: — it accesses a global variable hidden by a local one and defines member functions outside a class.
Q. Difference between malloc() and new?
A. new is an operator, needs no size or type cast and calls the constructor; malloc() is a function that does none of these.
Q. How do you free an array created with new?
A. With delete[] arrayName;
Q. What is a manipulator?
A. A function used with << to format output, e.g. endl, setw, setprecision.
Q. Which header file is needed for setw()?
A. iomanip

Practical 3

Implicit and explicit type casting

Aim: See how C++ converts types automatically and convert them yourself with a cast.

QWrite programs to show implicit and explicit type conversion.

Showing answer A of 3 · tap A, B or C to see the others

Output

i + f   = 9.5
c + 1   = 66
i / 2   = 3
#include <iostream>
using namespace std;

int main()
{
    int i = 7;
    float f = 2.5f;
    char c = 'A';
🎤 Viva questions (4)
Q. What is implicit type conversion?
A. Automatic conversion done by the compiler, e.g. int to float in a mixed expression.
Q. What is explicit type casting?
A. Conversion written by the programmer, e.g. (float)x or static_cast<float>(x).
Q. Does int(9.8) round the value?
A. No, it truncates and gives 9.
Q. Name the C++ cast operators.
A. static_cast, dynamic_cast, const_cast, reinterpret_cast.

Practical 4

Classes and objects — member function defined inside the class

Aim: Create a class with data members and member functions written inside the class, and use it through objects.

QWrite a program using a class whose member functions are defined inside the class.

Showing answer A of 3 · tap A, B or C to see the others

Output

Enter roll, name and percentage: 21 Aman 78.5
Roll: 21  Name: Aman  Percentage: 78.5
#include <iostream>
using namespace std;

class Student
{
    int roll;
    string name;
    float per;
🎤 Viva questions (5)
Q. What is a class?
A. A user-defined data type that binds data members and member functions together.
Q. What is an object?
A. An instance (variable) of a class.
Q. What is the default access specifier in a class?
A. private
Q. Difference between class and structure in C++?
A. Members of a class are private by default, members of a struct are public by default.
Q. Functions defined inside the class are treated as?
A. Inline functions.

Practical 5Compulsory

Member function defined outside the class

Aim: Declare member functions inside the class and write their body outside it using the scope resolution operator.

QWrite a program in which member functions are defined outside the class.

Showing answer A of 3 · tap A, B or C to see the others

Output

Enter id, name and basic salary: 101 Neha 20000
Id: 101  Name: Neha  Net salary: 34000
#include <iostream>
using namespace std;

class Employee
{
    int id;
    string name;
    float basic;
🎤 Viva questions (4)
Q. How do you define a member function outside the class?
A. returnType ClassName::functionName(arguments) { ... }
Q. Which operator is used for it?
A. The scope resolution operator ::
Q. Is a function defined outside the class inline?
A. No, unless the keyword inline is written.
Q. Can private data be used in a function defined outside?
A. Yes, it is still a member function of the class.

Practical 6Compulsory

Inline function

Aim: Write small functions as inline so the compiler can put their code at the place of the call.

QWrite a program to demonstrate inline functions.

Showing answer A of 3 · tap A, B or C to see the others

Output

Enter a number: 6
Square = 36
Cube   = 216
#include <iostream>
using namespace std;

inline int square(int x) { return x * x; }
inline int cube(int x) { return x * x * x; }

int main()
{
🎤 Viva questions (4)
Q. What is an inline function?
A. A function whose code is inserted at the place of each call instead of jumping to it.
Q. Advantage of inline functions?
A. Saves the overhead of a function call, so small functions run faster.
Q. When is inline ignored by the compiler?
A. For functions with loops, switch, recursion, static variables or very large bodies.
Q. Inline function vs macro?
A. Inline is checked by the compiler for type and syntax; a macro is plain text replacement.

Practical 7Compulsory

Friend function

Aim: Let a non-member function read the private data of one or two classes by declaring it as a friend.

Q1Program 1: friend function shared by two different classes.

Showing answer A of 3 · tap A, B or C to see the others

Output

Largest = 72 (class B)
#include <iostream>
using namespace std;

class B;   // forward declaration

class A
{
    int x;

Q2Program 2: external (non-member) function as a friend of one class.

Showing answer A of 3 · tap A, B or C to see the others

Output

Enter marks of 3 subjects: 67 82 74
Average = 74.3333
#include <iostream>
using namespace std;

class Marks
{
    int m1, m2, m3;
public:
    void read()
🎤 Viva questions (5)
Q. What is a friend function?
A. A non-member function that is allowed to access the private and protected members of a class.
Q. Is a friend function a member of the class?
A. No; it is called without an object and dot operator.
Q. Why is forward declaration needed?
A. When a friend of two classes is declared, the second class must be known before the first uses its name.
Q. Can a friend be declared in the private section?
A. Yes, the access specifier does not affect a friend declaration.
Q. Disadvantage of friend functions?
A. They break data hiding to some extent.

Practical 8Compulsory

Static data member and static member function

Aim: Share one data member among all objects of a class and use a static function to read it.

QWrite a program to demonstrate a static data member and a static member function.

Showing answer A of 3 · tap A, B or C to see the others

Output

Students admitted: 1
Students admitted: 3
#include <iostream>
using namespace std;

class Student
{
    int roll;
    static int count;
public:
🎤 Viva questions (5)
Q. What is a static data member?
A. A data member with only one copy shared by all objects of the class.
Q. Why must it be defined outside the class?
A. Memory for it is allocated only once, by that definition.
Q. What is its default value?
A. Zero, if no value is given.
Q. Can a static member function use non-static data?
A. No; it has no this pointer, so it can use only static members.
Q. How is a static member function called?
A. ClassName::functionName();

Practical 9Compulsory

Array of objects

Aim: Store many objects of a class in an array and process them in a loop.

QWrite a program using an array of objects.

Showing answer A of 3 · tap A, B or C to see the others

Output

Roll, name, percentage: 1 Asha 81.5
Roll, name, percentage: 2 Vikas 76
Roll, name, percentage: 3 Pooja 88.25

Roll	Name	Percent
1	Asha	81.5
2	Vikas	76
3	Pooja	88.25
Topper: 3	Pooja	88.25
#include <iostream>
using namespace std;

class Student
{
    int roll;
    string name;
    float per;
🎤 Viva questions (4)
Q. What is an array of objects?
A. An array whose every element is an object of the same class.
Q. How do you call a member function of the 2nd object of array s?
A. s[1].functionName();
Q. Which constructor is needed for Student s[3]; ?
A. A default constructor (or no constructor at all).
Q. Where is the array of objects stored?
A. In consecutive memory locations, like any array.

Practical 10Compulsory

Default, parameterized and copy constructor; multiple constructors in a class

Aim: Initialise objects automatically with constructors and use more than one constructor in the same class.

Q1Program 1: default, parameterized and copy constructor.

Showing answer A of 3 · tap A, B or C to see the others

Output

Default constructor
Parameterized constructor
Copy constructor
Roll 0, Percentage 0
Roll 12, Percentage 84.5
Roll 12, Percentage 84.5
#include <iostream>
using namespace std;

class Student
{
    int roll;
    float per;
public:

Q2Program 2: more than one constructor in one class (constructor overloading).

Showing answer A of 3 · tap A, B or C to see the others

Output

0 + 0i
4.5 + 0i
3 + 7i
#include <iostream>
using namespace std;

class Complex
{
    float real, imag;
public:
    Complex()                 { real = 0; imag = 0; }
🎤 Viva questions (5)
Q. What is a constructor?
A. A special member function with the class name and no return type that runs automatically when an object is created.
Q. Types of constructors?
A. Default, parameterized, copy (and constructors with default arguments).
Q. Why is the copy constructor argument passed by reference?
A. Passing by value would itself need a copy and cause endless calls.
Q. What is constructor overloading?
A. Having more than one constructor with different arguments in the same class.
Q. Can a constructor be virtual?
A. No.

Practical 11

Single level and multilevel inheritance

Aim: Derive a new class from an existing class (single) and a class from a derived class (multilevel).

Q1Program 1: single level inheritance.

Showing answer A of 3 · tap A, B or C to see the others

Output

Roll: 5  Name: Tanvi
Total: 163  Percentage: 81.5
#include <iostream>
using namespace std;

class Student
{
protected:
    int roll;
    string name;

Q2Program 2: multilevel inheritance.

Showing answer A of 3 · tap A, B or C to see the others

Output

Roll 17: 71.5 + 64 = 135.5
#include <iostream>
using namespace std;

class Student
{
protected:
    int roll;
public:
🎤 Viva questions (5)
Q. What is inheritance?
A. Creating a new (derived) class from an existing (base) class so that it reuses its members.
Q. What is single inheritance?
A. One derived class from one base class.
Q. What is multilevel inheritance?
A. A class derived from a class which is itself derived (A → B → C).
Q. Difference between private and protected?
A. Both hide members from outside; protected members can be used in derived classes, private cannot.
Q. In which order are constructors called?
A. Base class first, then derived class.

Practical 12Compulsory

Multiple inheritance

Aim: Derive one class from two or more base classes.

QWrite a program to implement multiple inheritance.

Showing answer A of 3 · tap A, B or C to see the others

Output

Marks: 76 + 81, Sports: 9
Grand total = 166
#include <iostream>
using namespace std;

class Marks
{
protected:
    int m1, m2;
public:
🎤 Viva questions (5)
Q. What is multiple inheritance?
A. A class derived from two or more base classes.
Q. Syntax?
A. class D : public B1, public B2 { ... };
Q. What is the ambiguity problem?
A. When two base classes have a member with the same name, the derived class cannot tell which one is meant.
Q. How is it solved?
A. With the scope resolution operator, e.g. obj.B1::show(); or by overriding the function in the derived class.
Q. Order of constructor calls?
A. In the order the base classes are listed in the derivation.

Practical 13

Hierarchical inheritance

Aim: Derive several classes from one common base class.

QWrite a program to implement hierarchical inheritance.

Showing answer A of 3 · tap A, B or C to see the others

Output

Rectangle area = 40
Triangle area  = 20
Circle area    = 28.26
#include <iostream>
using namespace std;

class Shape
{
protected:
    float a, b;
public:
🎤 Viva questions (4)
Q. What is hierarchical inheritance?
A. Two or more classes derived from one base class.
Q. Give a real example.
A. Shape → Circle, Rectangle, Triangle; Account → Savings, Current.
Q. Can derived classes of the same base use each other's members?
A. No, they are separate; they share only the base class part.
Q. Which members of the base class are not inherited?
A. Constructors, destructor and friend functions (private members are inherited but not accessible).

Practical 14Compulsory

Virtual base class

Aim: Avoid two copies of a grandparent class in hybrid (diamond) inheritance by making it a virtual base class.

QWrite a program to demonstrate a virtual base class.

Showing answer A of 3 · tap A, B or C to see the others

Output

Roll 11: marks 152 + sports 8 = 160
#include <iostream>
using namespace std;

class Student
{
protected:
    int roll;
public:
🎤 Viva questions (4)
Q. What is a virtual base class?
A. A base class declared with the keyword virtual so that only one copy of it is inherited in multipath inheritance.
Q. Which problem does it solve?
A. The diamond problem — duplicate copies and ambiguity of grandparent members.
Q. Where is the keyword virtual written?
A. In the derivation of the intermediate classes: class B : virtual public A.
Q. What is hybrid inheritance?
A. A combination of two or more types of inheritance, e.g. hierarchical + multiple.

Practical 15

Constructors in derived class

Aim: Pass values from a derived class constructor to the base class constructor and see the order of calls.

QWrite a program to use constructors in a derived class.

Showing answer A of 3 · tap A, B or C to see the others

Output

Person constructor
Student constructor
Om, age 19, roll 23
#include <iostream>
using namespace std;

class Person
{
protected:
    string name;
    int age;
🎤 Viva questions (4)
Q. If the base class has a parameterized constructor, what must the derived class do?
A. Call it in the initialisation list of its own constructor.
Q. Order of constructor calls?
A. Base class first, then derived.
Q. Order of destructor calls?
A. Derived first, then base — reverse of constructors.
Q. What is an initialisation list?
A. The part after : in a constructor that initialises base classes and members before the body runs.

Practical 16Compulsory

Pointer to object and the this pointer

Aim: Reach object members through a pointer and use the this pointer inside member functions.

Q1Program 1: pointer to an object.

Showing answer A of 3 · tap A, B or C to see the others

Output

Code 501, Price Rs 249.5
Code 501, Price Rs 249.5
#include <iostream>
using namespace std;

class Item
{
    int code;
    float price;
public:

Q2Program 2: use the this pointer.

Showing answer A of 3 · tap A, B or C to see the others

Output

Roll 14 : Kavya
#include <iostream>
using namespace std;

class Student
{
    int roll;
    string name;
public:
🎤 Viva questions (5)
Q. What is a pointer to an object?
A. A pointer variable that stores the address of an object.
Q. Which operator is used with it?
A. The arrow operator ->
Q. What is the this pointer?
A. A hidden pointer in every non-static member function that points to the object that called the function.
Q. When is this used?
A. When member and argument names are the same, and to return the calling object (return *this).
Q. Does a static member function have this?
A. No.

Practical 17Compulsory

Pointer to derived class in single and multilevel inheritance

Aim: Use a base class pointer to point to derived class objects and see which function gets called.

QWrite a program to use a pointer to a derived class in single and multilevel inheritance.

Showing answer A of 3 · tap A, B or C to see the others

Output

Base show(): b = 10
Derived show(): b = 10, d = 20
#include <iostream>
using namespace std;

class Base
{
public:
    int b;
    void show() { cout << "Base show(): b = " << b << endl; }
🎤 Viva questions (4)
Q. Can a base class pointer point to a derived object?
A. Yes.
Q. Can a derived class pointer point to a base object?
A. Not directly; it needs an explicit cast and is unsafe.
Q. Through a base pointer, which members can be used?
A. Only the members inherited from the base class.
Q. Without virtual, which function does bp->show() call?
A. The base class version, because the pointer type decides at compile time.

Practical 18

Function overloading

Aim: Write several functions with the same name that differ in the number or type of arguments.

QWrite a program to demonstrate function overloading.

Showing answer A of 3 · tap A, B or C to see the others

Output

Circle (r = 5)        : 78.5
Rectangle (8 x 6)     : 48
Triangle (b=10, h=7)  : 35
#include <iostream>
using namespace std;

float area(float r)          { return 3.14f * r * r; }
int area(int l, int b)       { return l * b; }
double area(double b, double h) { return 0.5 * b * h; }

int main()
🎤 Viva questions (4)
Q. What is function overloading?
A. Using the same function name for different functions that differ in number or type of arguments.
Q. Is it compile time or run time polymorphism?
A. Compile time (static binding).
Q. Can functions be overloaded only by return type?
A. No.
Q. Advantage?
A. Easy to remember one name for similar tasks; code is more readable.

Practical 19Compulsory

Overload a unary operator using a member function and a friend function

Aim: Give a meaning to operators like -, ++ and -- for objects of your own class.

Q1Program 1: overload a unary operator using a member function.

Showing answer A of 3 · tap A, B or C to see the others

Output

Before: (4, -7)
After : (-4, 7)
#include <iostream>
using namespace std;

class Point
{
    int x, y;
public:
    Point(int a, int b) { x = a; y = b; }

Q2Program 2: overload a unary operator using a friend function.

Showing answer A of 3 · tap A, B or C to see the others

Output

a = 15, b = -20
a = -15, b = 20
#include <iostream>
using namespace std;

class Number
{
    int a, b;
public:
    Number(int x, int y) { a = x; b = y; }
🎤 Viva questions (5)
Q. What is operator overloading?
A. Giving an extra meaning to an existing operator for user-defined types.
Q. How many arguments does a unary operator take as a member? as a friend?
A. Member: none. Friend: one.
Q. How is postfix ++ told apart from prefix ++?
A. Postfix has a dummy int argument: operator++(int).
Q. Which operators cannot be overloaded?
A. :: , . , .* , ?: and sizeof.
Q. Is operator overloading compile time polymorphism?
A. Yes.

Practical 20

Overload a binary operator using a member function and a friend function

Aim: Use operators like +, -, * and == between two objects of your class.

Q1Program 1: overload a binary operator using a member function.

Showing answer A of 3 · tap A, B or C to see the others

Output

c1 = 3 + 4i
c2 = 2.5 - 1.5i
c3 = 5.5 + 2.5i
#include <iostream>
using namespace std;

class Complex
{
    float re, im;
public:
    Complex(float r = 0, float i = 0) { re = r; im = i; }

Q2Program 2: overload a binary operator using a friend function.

Showing answer A of 3 · tap A, B or C to see the others

Output

c1 - c2 = 5 - 3i
#include <iostream>
using namespace std;

class Complex
{
    float re, im;
public:
    Complex(float r = 0, float i = 0) { re = r; im = i; }
🎤 Viva questions (4)
Q. How many arguments does a binary operator take as a member? as a friend?
A. Member: one. Friend: two.
Q. In c3 = c1 + c2 with a member operator, which object calls the function?
A. c1 (the left operand).
Q. When must a friend be used?
A. When the left operand is not an object of the class, e.g. 2 * obj.
Q. Which operators cannot be overloaded with a friend?
A. =, (), [] and ->.

Practical 21Compulsory

Virtual function

Aim: Make a base class pointer call the function of the actual derived object (run-time polymorphism).

QWrite a program to demonstrate a virtual function.

Showing answer A of 3 · tap A, B or C to see the others

Output

show() of Derived
display() of Base (not virtual)
#include <iostream>
using namespace std;

class Base
{
public:
    virtual void show() { cout << "show() of Base" << endl; }
    void display() { cout << "display() of Base (not virtual)" << endl; }
🎤 Viva questions (5)
Q. What is a virtual function?
A. A member function declared with virtual in the base class that is redefined in derived classes and chosen at run time.
Q. What is run time polymorphism?
A. Deciding which function to call while the program runs, based on the actual object (late / dynamic binding).
Q. What is needed for it to work?
A. A base class pointer or reference pointing to a derived object.
Q. Can a constructor be virtual?
A. No, but a destructor can (and should be in a base class).
Q. What is a vtable?
A. A hidden table of virtual function addresses that the compiler uses for late binding.

Practical 22

Pure virtual function

Aim: Create an abstract base class with a pure virtual function that every derived class must define.

QWrite a program to demonstrate a pure virtual function.

Showing answer A of 3 · tap A, B or C to see the others

Output

Rectangle area = 60
Circle area    = 153.86
#include <iostream>
using namespace std;

class Shape
{
public:
    virtual float area() = 0;
};
🎤 Viva questions (4)
Q. What is a pure virtual function?
A. A virtual function with no body, declared with = 0.
Q. What is an abstract class?
A. A class that has at least one pure virtual function; its objects cannot be created.
Q. What if a derived class does not define the pure virtual function?
A. The derived class also becomes abstract.
Q. Can we make a pointer to an abstract class?
A. Yes, and it is used to point to derived objects.

Practical 23Compulsory

Read and write a file using the constructor and the open() function

Aim: Open files in two ways — by passing the name to the stream constructor and by calling open() — then write and read data.

Q1Program 1: open a file using the constructor of the stream class.

Showing answer A of 3 · tap A, B or C to see the others

Output

Data written to student.txt
Reading the file:
Roll: 12
Name: Sakshi
Branch: CO
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream fout("student.txt");
    fout << "Roll: 12" << endl;

Q2Program 2: open a file using the open() function.

Showing answer A of 3 · tap A, B or C to see the others

Output

1. Nanded
2. Pune
3. Nagpur
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream fout;
    fout.open("cities.txt");
🎤 Viva questions (5)
Q. Which header file is needed for file handling?
A. fstream
Q. Name the file stream classes.
A. ifstream (read), ofstream (write), fstream (both).
Q. Two ways of opening a file?
A. Using the constructor of the stream class, or using the open() member function.
Q. What does ios::app do?
A. Opens the file in append mode — writing starts at the end.
Q. Why close a file?
A. To save the data written in the buffer and free the file for other use.

Practical 24Compulsory

Copy the contents of one file into another using formatted I/O

Aim: Read data from a source file and write it into a target file.

QWrite a program to copy the content of one file into another.

Showing answer A of 3 · tap A, B or C to see the others

Output

3 lines copied to copy.txt
Ananda Technologies
C++ practical 24
File copy demo
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream make("source.txt");
    make << "Ananda Technologies" << endl << "C++ practical 24" << endl << "File copy demo" << endl;
🎤 Viva questions (4)
Q. Which functions copy a file character by character?
A. get() to read and put() to write.
Q. Difference between >> and getline() for reading?
A. >> stops at spaces; getline() reads the whole line.
Q. What is formatted I/O?
A. Reading and writing with >> and << (and manipulators), which convert data to and from text.
Q. How do you detect the end of a file?
A. The read operation fails (e.g. while (in >> x) or while (getline(...)) stops), or eof() returns true.

Practical 25

Sequential input and output operations on a file

Aim: Write records one after another into a file and read them back in the same order.

QWrite a program for sequential input and output on a file.

Showing answer A of 3 · tap A, B or C to see the others

Output

How many employees? 3
Id, name, salary: 1 Ganesh 28000
Id, name, salary: 2 Rekha 31500
Id, name, salary: 3 Salim 26000

Records in file:
1  Ganesh  28000
2  Rekha  31500
3  Salim  26000
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int n;
    cout << "How many employees? ";
🎤 Viva questions (4)
Q. What is sequential access?
A. Reading or writing data in order from the beginning of the file to the end.
Q. What is random access?
A. Moving directly to any position using seekg() / seekp().
Q. What do tellg() and tellp() return?
A. The current position of the get (read) and put (write) pointers.
Q. Which functions do character-wise sequential I/O?
A. get() and put().

Practical 26

Input / output operations on binary files

Aim: Store whole objects in a binary file with write() and read them back with read().

QWrite a program to perform input and output on a binary file.

Showing answer A of 3 · tap A, B or C to see the others

Output

Roll	Name	Percent
1	Anil	74.5
2	Bhakti	88
3	Chetan	69.25
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class Student
{
    int roll;
🎤 Viva questions (5)
Q. Difference between a text file and a binary file?
A. A text file stores data as readable characters; a binary file stores the exact bytes from memory.
Q. Which functions are used for binary I/O?
A. write() and read().
Q. Why is (char *) used in write()?
A. write() and read() take a char pointer to a block of bytes.
Q. What do seekg() and seekp() do?
A. Move the get (read) and put (write) pointers to a given position.
Q. Which mode flag is used for binary files?
A. ios::binary

Tick the answers you want in your file

YOUR DIPLOMA, STEP BY STEP

Save this page — everything till your final year is here

Free practicals, micro projects and question papers for your semesters — and when your final year comes, ready projects with report, setup and viva help.

📲 Your class needs this too

Send it to your class WhatsApp group — practical file done in minutes for everyone, free.