Object Oriented Programming 2076

Tribhuwan University
Institute of Science and Technology
2076
Bachelor Level / Second Semester / Science
Computer Science and Information Technology ( CSC161 )
( Object Oriented Programming )
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to give their answers in their own words as far as practicable.
The figures in the margin indicate full marks.

Long answer question:

Group A

Attempt ant two questions:(2 x 10 = 20)

1. Write a program according to the specification given below:

  •  Create a class Teacher with data members tid & subject and ember functions for reading and displaying data members.
  • Create another class Staff with data members sid & position, and member function for reading and displaying data members.
  • Derive a class Coordinator from Teacher and Staff and the class must have its own data member department and member functions for reading and displaying data members.
  • Create two object of Coordinator class and read and display their details.
10 marks view

#include <iostream>

using namespace std;

class Teacher

{

  int tid;

  string subject;

  public:

   void TeacherRead()

   {

       cout<<"Enter teacher's id:"<<endl;

       cin>>tid;

       cout<<"Enter teacher's subject:"<<endl;

       cin>>subject;

   }

   void TeacherDisplay()

   {

       cout<<"Teacher's Id:"<<tid<<endl;

       cout<<"Teacher's subject:"<<subject<<endl;

   }

};

class Staff

{

  int sid;

  string position;

  public:

   void StaffRead()

   {

       cout<<"Enter staff's id:"<<endl;

       cin>>sid;

       cout<<"Enter staff's position:"<<endl;

       cin>>position;

   }

   void StaffDisplay()

   {

       cout<<"Staff's id:"<<sid<<endl;

       cout<<"Staff's position:"<<position<<endl;

   }

};

class Coordinator: public Teacher, public Staff

{

    string department;

    public:

     void CoordinatorRead()

     {

        cout<<"Enter department:"<<endl;

        cin>>department;

     }

     void CoordinatorDisplay()

     {

        cout<<"Department:"<<department<<endl;

     }

};

int main()

{

    Coordinator c1, c2;

    c1.TeacherRead();

    c1.CoordinatorRead();

    c1.TeacherDisplay();

    c1.CoordinatorDisplay();

    c2.StaffRead();

    c2.CoordinatorRead();

    c2. StaffDisplay();

    c2.CoordinatorDisplay();

    return 0;

}

Output:


2. Explain the concept of operator overloading? List the operators that cannot be overloaded. Write programs to add two object of distance class with data members feet and inch by using by using member function and friend function.

10 marks view

In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a user-defined data type, this ability is known as operator overloading. For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are  Distance which has data member feet and inch, Complex Number etc.

Operator that cannot be overloaded are as follows:

  • Scope operator (::)
  • Sizeof
  • member selector(.)
  • member pointer selector(*)
  • ternary operator(?:)

Program

#include <iostream>

using namespace std;

class Distance

{

    private:

        int feet, inch;

    public:

        void readDistance(void)

        {

            cout << "Enter feet: ";

            cin >>feet;

            cout << "Enter inch: ";

            cin >>inch;

        }

        void displayDistance(void)

        {

            cout << "Feet:" << feet << "\\t" << "Inches:" << inch << endl;

        }

        friend Distance operator+(Distance, Distance);

};


Distance operator+(Distance d1, Distance d2)

{

    Distance temp;   

    temp.inch = d1.inch + d2.inch;

    temp.feet  = d1.feet + d2.feet + (temp.inch/12);

    temp.inch = temp.inch%12;

    return temp;

}

        

int main()

{

    Distance d1,d2,d3;

    

    cout << "Enter first  distance:" << endl;

    d1.readDistance();

    cout << endl;

    

    cout << "Enter second distance:" << endl;

    d2.readDistance();

    cout << endl;

     

    d3=d1+d2;

     

    cout << "The sum of two distance is:" << endl;

    d3.displayDistance();

 

    return 0;

}

3. Explain types of polymorphism briefly. Write down roles of polymorphism. How can we achieve dynamic polymorphism briefly. Write down foles of polymorphism. How can we achieve dynamic polymorphism? Explain with example.

10 marks view

Group B

Short answer questions:

Attempt any eight questions:(8 x 5 = 40)

4. How object oriented programming differs from object based programming language? Discuss benefits of OOP.

5 marks view

5. What is the use of new and delete operators? Illustrate with example. What are advantages of new malloc.


5 marks view

6. What is meant by return by reference? How can we return values by reference by using reference variable? Illustrate with examples.

5 marks view

7. What is destructor? Write a program to show the destructor call such that it prints the message "memory is released".

5 marks view

Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope. Destructors are declared in a program to release memory space for future utilization.

Program:

#include <iostream>

using namespace std;

class MyClass

{

   private:

      int m, n;

   public:

      MyClass( )   // constructor 

      {

         m = 10;

         n = 5;

      }

      ~MyClass( )   //destructor

      {

          cout<<"Memory is released!!";

      }

      void display()

      {

          cout<<"The sum is:"<<(m+n)<<endl;

      }

};

int main()

{

    MyClass obj;

    obj.display();

    return 0;

}

Output


When an object is created the constructor of that class is called. The object reference is destroyed when its scope ends, which is generally after the closing curly bracket } for the code block in which it is created.

8. What is this pointer? How can we use it for name conflict resolution?Illustrate with example.

5 marks view

We can use keyword "this" to refer to this instance inside a class definition. One of the main usage of keyword this is to resolve ambiguity between the names of data member and function parameter. For example,

class Circle {

private:

   double radius;                 // Member variable called "radius"

   ......

public:

   void setRadius(double radius)   // Function's argument also called "radius"

   { 

      this->radius = radius;

         // "this.radius" refers to this instance's member variable

         // "radius" resolved to the function's argument.

   }

   ......

}

In the above codes, there are two identifiers called radius - a data member and the function parameter. This causes naming conflict. To resolve the naming conflict, we could name the function parameter r instead of radius. However, radius is more approximate and meaningful in this context. We can use keyword this to resolve this naming conflict. "this->radius" refers to the data member; while "radius" resolves to the function parameter. "this" is actually a pointer to this object.

9. How can you define catch statement that can catch any type of exception? Illustrate the use of multiple catch statement with example.

5 marks view

The catch block defines the action to be taken, when an exception occur. We can use the catch statement with three dots as parameter (...) so that it can catch any types of exceptions. For example:

#include <iostream>
using namespace std;

void func(int a) {
   try {
      if(a==0) throw 23.33;
      if(a==1) throw 's';
   } catch(...) {
      cout << "Caught Exception!\\n";
   }
}
int main() {
   func(0);
   func(1);
   return 0;
}

Multiple Catch Statements

Multiple catch statements are used when we have to catch a specific type of exception out of many possible type of exceptions i.e. an exception of type char or int or short or long etc. For example:

 #include<iostream.h>
       #include<conio.h>
       void main()
       {
            int a=2;
              try
              {

                  if(a==1)
                      throw a;                  //throwing integer exception

                  else if(a==2)
                      throw 'A';                //throwing character exception

                  else if(a==3)
                      throw 4.5;                //throwing float exception

              }
              catch(int a)
              {
                  cout<<"\\nInteger exception caught.";
              }
              catch(char ch)
              {
                  cout<<"\\nCharacter exception caught.";
              }
              catch(double d)
              {
                  cout<<"\\nDouble exception caught.";
              }
              cout<<"\\nEnd of program.";
       }
   Output :
              Character exception caught.
              End of program.

10. Which functions can be used for reading and writing object? Describe briefly. Write a program that read values of two objects of student class(assume data members are sid , sname, and level) and display the data in monitor.

5 marks view

12. Write short notes on:

  • Cascading of IO operators
  •  Pure Virtual Function
5 marks view