Examination 2016 OBJECT ORIENTED PROGRAMMING (2015 Pattern) Nov / Dec 2016 Time : 2 Hours Maximum Marks : 50
Total Page:16
File Type:pdf, Size:1020Kb
S.E Computer (First Semester) Examination 2016 OBJECT ORIENTED PROGRAMMING (2015 Pattern) Nov / Dec 2016 Time : 2 Hours Maximum Marks : 50 Q.1 a) Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP) 4M Procedure Oriented Programming Object Oriented Programming In POP, program is divided into small In OOP, program is divided into parts Divided Into parts called functions. called objects. In POP,Importance is not given to data In OOP, Importance is given to the data Importance but to functions as well as sequence of rather than procedures or functions actions to be done. because it works as a real world. Approach POP follows Top Down approach. OOP follows Bottom Up approach. Access OOP has access specifiers named POP does not have any access specifier. Specifiers Public, Private, Protected, etc. In OOP, objects can move and In POP, Data can move freely from Data Moving communicate with each other through function to function in the system. member functions. To add new data and function in POP is OOP provides an easy way to add new Expansion not so easy. data and function. In OOP, data can not move easily from In POP, Most function uses Global data function to function,it can be kept public Data Access for sharing that can be accessed freely or private so we can control the access from function to function in the system. of data. POP does not have any proper way for OOP provides Data Hiding so provides Data Hiding hiding data so it is less secure. more security. In OOP, overloading is possible in the Overloading In POP, Overloading is not possible. form of Function Overloading and Operator Overloading. Example of POP are : C, VB, Example of OOP are : C++, JAVA, Examples FORTRAN, Pascal. VB.NET, C#.NET. Q.1 b) What is friend function? 2M Ans. A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows: class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); }; Q.1 c) Define class number which has inline function mult() and cube( ) for calculating the multiplication of two double numbers given and cube of the integer number given? 4M Ans c) #include<iostream.h> #include<conio.h> class number { public: inline float mul(double x,double y) { return(x*y); } inline float cube(int x) { return(x*x*x); } }; void main() { number obj; double val1,val2; int val3; clrscr(); cout<<"Enter two double values and one integer value:"; cin>>val1>>val2>>val3; cout<<"\Multiplication value is:"<<obj.mul(val1,val2); cout<<"\n\nCube value is :"<<obj.cube(val3); getch(); } Output: Enter two values: 5.5 7.5 4 Multiplication Value is: 41.25 Cube Value is : 64 Q.1 d) What is use of this pointer? 2M Ans . i. The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ii. ‘this’ pointer is a constant pointer that holds the memory address of the current object. Use : 1) When local variable’s name is same as member’s name class Test { private: int x; public: void setX (int x) { // The 'this' pointer is used to retrieve the object's x hidden by the local //variable 'x' this->x = x; } void print() { cout << "x = " << x << endl; } }; 2) To return reference to the calling object Test& Test::func () { // Some processing return *this; } Or Q.2 a) Discuss the various ways in which inheritance promotes software reuse, saves time during program development and helps prevent errors. [4] 1) Inheritance allows developers to create subclasses that reuse code declared already in a superclass. 2) Avoiding the duplication of common functionality between several classes by building a class inheritance hierarchy can save developers a considerable amount of time. 3) Similarly, placing common functionality in a single superclass, rather than duplicating the code in multiple unrelated classes, helps prevent the same errors from appearing in multiple source-code files. If errors occur in the common functionality of the superclass, the software developer needs to modify only the superclass’s. Q.2 b) Define a class string. use overloaded == operator to compare two strings 5M Ans. #include<conio.h> #include<iostream.h> #include<string.h> class string { public: char *s; void getstring(char *str) { strcpy(s,str); } void operator==(comp); }; void comp::operator==(comp ob) { if(strcmp(s,ob.s)==0) cout<<"\nStrings are Equal"; else cout<<"\nStrings are not Equal"; } void main() { comp ob, ob1; char *string1, *string2; clrscr(); cout<<"Enter First String:"; cin>>string1; ob.getstring(string1); cout<<"\nEnter Second String:"; cin>>string2; ob1.getstring(string2); //Call Equality Operator ob==ob1; getch(); } Q.2 c) Explain abstract class with an example 3M Ans. Abstract class in C++ programming is a class that contains at least one pure virtual function. (Pure virtual function in C++ is a virtual functions with no implementation). It may also contain non-virtual functions and member variables. Pure virtual functions in abstract class is implemented by its derived classes. Main important point of an abstract class is that we cannot create an object of it, but we create a pointer. And, in this pointer, we assign the object of derived classes. /Abstract base class class AbstractBase { public: virtual void Display() = 0; //Pure Virtual Function declaration }; //Derived class that will inherit the abstract base class // and implement pure virtual function. class Derived:public AbstractBase { public: void Display() { cout << "pure virtual function implementation"; } }; //-------------- TEST-Abstract Class --------------- int main() { AbstractBase *basePointer = new Derived(); basePointer->Display(); // OR AbstractBase * bPtr; Derived dObj; bPtr = &dObj; bPtr->Display(); //Abstract base class object can't be created. //AbstractBase obj; // Compiler will flash an error. } Q.3 a) How smart pointers avoid the problem of memory leak? 4M Ans. 1. Smart pointers are an improved, more reliable way to manage resources such as dynamically allocated memory and file handles. 2. A smart pointer is a "wrapper" that contains a pointer within it but also performs other special actions. This data type causes an internal reference count to be kept for the associated memory object. The count is equal to the total number of pointers that refer to the object. When the count reaches zero, the object is destroyed, and the memory is released. For Eg. #include<iostream> using namespace std; class SmartPtr { int *ptr; // Actual pointer public: // Constructor explicit SmartPtr(int *p = NULL) { ptr = p; } // Destructor ~SmartPtr() { delete(ptr); } // Overloading dereferencing operator int &operator *() { return *ptr; } int *operator ->() //Overloading Access operator { return ptr; } }; int main() { SmartPtr ptr(new int()); *ptr = 20; cout << *ptr; // We don't need to call delete ptr: when the object // ptr goes out of scope, destructor for it is automatically // called and destructor does delete ptr. return 0; } Q.3 b) Differentiate compile time and run-time polymorphism ? 4M Ans. Compile time Polymorphism Run time Polymorphism In Compile time Polymorphism, call is In Run time Polymorphism, call is not resolved by the compiler. resolved by the compiler. It is also known as Static binding, Early It is also known as Dynamic binding, Late binding and overloading as well. binding and overriding as well. Overloading is compile time polymorphism Overriding is run time polymorphism where more than one methods share the having same method with same parameters same name with different parameters or or signature, but associated in a class & its signature and different return type. subclass. It is achieved by function overloading and It is achieved by virtual functions and operator overloading. pointers. It provides slow execution as compare to It provides fast execution because known early binding because it is known at early at compile time. runtime. Compile time polymorphism is less flexible Run time polymorphism is more flexible as as all things execute at compile time. all things execute at run time. Q.3 c) Explain virtual base class and virtual function with example. 4M Ans Virtual Base Class An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class. C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class. When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class’ name with the word virtual. Consider the following example : class A { public: int i; }; class B : virtual public A { public: int j; }; class C: virtual public A { public: int k; }; class D: public B, public C { public: int sum; }; int main() { D ob; ob.i = 10; //unambiguous since only one copy of i is inherited. ob.j = 20; ob.k = 30; ob.sum = ob.i + ob.j + ob.k; cout << “Value of i is : ”<< ob.i<<”\n”; cout << “Value of j is : ”<< ob.j<<”\n”; cout << “Value of k is :”<< ob.k<<”\n”; cout << “Sum is : ”<< ob.sum <<”\n”; return 0; } Virtual Functions : A virtual function is a member function that is declared within a base class and redefined by a derived class.