Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

“..………………….

Assignment 03(oop th)………………………”

Question No: 2

a) Write a program that shows the use of read() and write() to handle file
I/O involving objects.

#include <iostream>

#include <fstream>

using namespace std;

int main(){

char text[200];

fstream file;

file.open ("example.txt", ios::out | ios::in );

cout << "Write text to be written on file." << endl;

cin.getline(text, sizeof(text));

// Writing on file

file << text << endl;

// Reding from file

file >> text;

cout << text << endl;


//closing the file

file.close();

return 0;

b) The keyword ‘virtual’ can be used for functions as well as classes in C++.
Explain the two different uses. Give an example each.

A virtual function is a member function which is declared within a base class and is re-defined
(overridden) by a derived class. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object and execute the
derived class’s version of the function. 
 Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for function call.
 They are mainly used to achieve Runtime polymorphism
 Functions are declared with a virtual keyword in base class.
 The resolving of function call is done at runtime.

Syntax with example;

#include<iostream>
using namespace std;  
base {
public:
    virtual void print()
    {
        cout << "print base class\n";
    }
  
    void show()
    {
        cout << "show base class\n";
    }
};
  
class derived : public base {
public:
    void print()
    {
        cout << "print derived class\n";
    }
  
    void show()
    {
        cout << "show derived class\n";
    }
};
  
int main()
{
    base *bptr;
    derived d;
    bptr = &d;
  
     // Virtual function, binded at runtime

     bptr->print();
  
    // Non-virtual function, binded at compile time
    bptr->show();
    
    return 0;
}

c) Explain the keyword friend at function and class level


via code example.

Example of friend function;


#include <iostream>

using namespace std;

class Distance {

private:

int meter;

// friend function

friend int addFive(Distance);


public:

Distance() : meter(0) {}

};

// friend function definition

int addFive(Distance d) {

//accessing private members from the friend function

d.meter += 5;

return d.meter;

int main() {

Distance D;

cout << "Distance: " << addFive(D);

return 0;

Example of friend classes;


#include <iostream>

using namespace std;

// forward declaration

class ClassB;

class ClassA {

private:
int numA;

// friend class declaration

friend class ClassB;

public:

// constructor to initialize numA to 12

ClassA() : numA(12) {}

};

class ClassB {

private:

int numB;

public:

// constructor to initialize numB to 1

ClassB() : numB(1) {}

// member function to add numA

// from ClassA and numB from ClassB

int add() {

ClassA objectA;

return objectA.numA + numB;

};

int main() {

ClassB objectB;

cout << "Sum: " << objectB.add();


return 0;

d) How are template functions overloaded? Explain with a suitable example.


The name of the function templates are the same but called with different arguments is known
as function template overloading.
If the function template is with the ordinary template, the name of the function remains
the same but the number of parameters differs.
When a function template is overloaded with a non-template function, the function
name remains the same but the function’s arguments are unlike.

#include <iostream>
using namespace std;
  
// Template declaration
template <class T>
  
// Template overloading of function
void display(T t1)
{
    cout << "Displaying Template: "
         << t1 << "\n";
}
  
// Template overloading of function
void display(int t1)
{
    cout << "Explicitly display: "
         << t1 << "\n";
}
  
// Driver Code
int main()
{
    // Function Call with a
    // different arguments
    display(200);
    display(12.40);
    display('G');
  
    return 0;
}

e) Define a class template Pair which can have a pair of values. The type of
the values is the parameter of the template. Define a member function
Add() which adds the two values and returns the sum.

#include <iostream>

using namespace std;

// Class template

template <class T>

class Number {

private:

// Variable of type T

T num;

public:

Number(T n) : num(n) {} // constructor

T getNum() {

return num;

};

int main() {

// create object with int type

Number<int> numberInt(7);
// create object with double type

Number<double> numberDouble(7.7);

cout << "int Number = " << numberInt.getNum() << endl;

cout << "double Number = " << numberDouble.getNum() << endl;

return 0;

You might also like