Solved Paper Object Oriented Programming - FINAL TERM EXAMINATION SPRING 2019

2019-07-04 13:39:45 - Adil Khan

UNIVERSITY OF HARIPUR

DEPARTMENT OF INFORMATION TECHNOLOGY

FINAL TERM EXAMINATION SPRING 2019

Class: MCS 2nd                        Date: 20-06-2019

Subject: Object Oriented Programming      Instructor: Mr. Tahir Javed

Total Time Allowed: 120 Minutes            Max Marks: 50

 

SECTION-B

Note: Give SHORT answer to the following question.

Q2.    What do you know about encapsulation?                                                   [Marks 3]

Ans:    Encapsulation is a process of combining data members and functions in a single unit called class. This is to prevent the access to the data directly, the access to them is provided through the functions of the class. It is one of the popular feature of Object Oriented Programming (OOPs) that helps in data hiding.

Q3.    What is meant by the term Function overloading? How a function can be overloaded in C++? Describe it with the help of an example program.                           [Marks 6]

Ans:    Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++.

Example Program:

#include
using namespace std;
void display(int);
void display(float);
void display(int, float);
int main() {
   int a = 5;
   float b = 5.5;
   display(a);
   display(b);
   display(a, b);
   return 0;
}
void display(int var) {
   cout << "Integer number: " << var << endl;
}
void display(float var) {
   cout << "Float number: " << var << endl;
}
void display(int var1, float var2) {
   cout << "Integer number: " << var1;
   cout << " and float number:" << var2;
}
Q4:     Write a C++ program implementing a class with the name constDest having constructor and destructor in its body.                                                                      [Marks 6]
Ans:    #include
class constDest{
Private:
           int a, b, c;
Public:
           constDest(){
           a=0;
           b=0;
           c=0;
           cout<< “Hi! This is a constructor.”<
}
~constDest(){
           cout<< “Hi! This is a destructor.”<
}
};
void main(){
           constDest obj;
}


Q5:     How many visibility mode/Access specifiers are use in C++? Explain each one. [Marls 5]

Ans:    There are 3 different access specifiers in C++.

           i) Public                      ii) Protected                iii) Private

  1. Public: The members declared as Public are accessible from outside the Class through an object of the class.
  2. Protected: The members declared as Protected are accessible from outside the class BUT only in a class derived from it.
  3. Private: These members are only accessible from within the class.

Example Program:

class MyClass
{
   public:
       int a;
   protected:
       int b;
   private:
       int c;
};
int main()
{
   MyClass obj;
   obj.a = 10;    //Allowed
   obj.b = 20;    //Not Allowed, gives compiler error
   obj.c = 30;    //Not Allowed, gives compiler error
}

 

 

SECTION C

Note: Give BRIEF answer to the following question.

Q6.    What is inheritance? Explain its types with the help of an example program.   [Marks 7]

Ans:   Inheritance:

           In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones and forming them into a hierarchy of classes.

Types of Inheritance:

           C++ supports six types of inheritance as follows:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
  6. Multipath Inheritance

Single Inheritance:

           A derived class with only one base class is called single inheritance

Multilevel Inheritance:

           A derived class with one base class and that base class is a derived class of another is called multilevel inheritance.

Multiple Inheritance:

           A derived class with multiple base class is called multiple inheritance.

Hierarchical Inheritance:

           Multiple derived classes with same base class is called hierarchical inheritance

Hybrid Inheritance:

           Combination of multiple and hierarchical inheritance is called hybrid inheritance.

Multipath Inheritance

           A derived class with two base classes and these two base classes have one common base class is called multipath inheritance

Programing Example:

#include
using namespace std;
class A{
            protected:
            int x;
            public:
            void setX(int a){
                        x = a;
            }
};
class B:public A{
            public:
            void getX(){
                        cout<<"x = "<
            }
};
int main(){
             B obj;
            obj.setX(4);
            obj.getX();
            return 0;
            }


Q7:     What is operator overloading? Explain unary-operator with the help of an example program?                                                                                                [Marks 6]

Ans:    Operator Overloading:

Operator overloading allows you to redefine the way operator works for user-defined types only (objects, structures). It cannot be used for built-in types (int, float, char etc.).

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.

Programming Example:

#include
 using namespace std;
 class Numbers
 {
  Private:
        int x, y, z;
    Public:
        void accept()
        {
                cout<<" Enter Three Numbers";
                cout<<" --------------------------";
                cout<<" First Number   :  ";
                cin>>x;
                cout<<" Second Number  :  ";
                cin>>y;
                cout<<" Three Number   :  ";
                cin>>z;
                cout<<" --------------------------";
        }
        void display()
        {
                cout<<" ";
                cout<        }       
      void operator-()
        {
                x=-x;
                y=-y;
                z=-z;
        }
 };
int main()
 {
        Numbers num;
        num.accept();
        cout<<" Numbers are : ";
        num.display();
        -num;    //Overloaded Unary (-) Operator
        cout<<" Negated Numbers are : ";
        num.display();
        return 0;
 }


Q8:     What is constructor and destructor?                                                            [Marks 2]

Ans:   Constructor:

           A constructor is a special type of member function that initializes an object automatically when it is created. Compiler identifies a given member function is a constructor by its name and the return type. Constructor has the same name as that of the class and it does not have any return type.

Destructor:

           Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. ... A destructor is a member function with the same name as its class prefixed by a ~ (tilde).

 

Download: Solved Paper Object Oriented Programming - FINAL TERM EXAMINATION SPRINT 2019 _ 0.pdf

More Posts