The document contains the answers to 6 questions in an Object Oriented Programming midterm exam, covering topics such as function overloading, recursion, pointers, structures, classes, objects, and the features of OOP like inheritance, polymorphism, abstraction, and encapsulation. Detailed examples are provided to explain concepts like recursion termination and differences between void and simple pointers.
The document contains the answers to 6 questions in an Object Oriented Programming midterm exam, covering topics such as function overloading, recursion, pointers, structures, classes, objects, and the features of OOP like inheritance, polymorphism, abstraction, and encapsulation. Detailed examples are provided to explain concepts like recursion termination and differences between void and simple pointers.
The document contains the answers to 6 questions in an Object Oriented Programming midterm exam, covering topics such as function overloading, recursion, pointers, structures, classes, objects, and the features of OOP like inheritance, polymorphism, abstraction, and encapsulation. Detailed examples are provided to explain concepts like recursion termination and differences between void and simple pointers.
Department: IBMS (IT) Semester: 2nd Subject: Object Oriented Programming Submitted To: Mr. Faheem Niaz
MIDTERM EXAMINATION 2021
Question No. 1: What is Function
Overloading? How we can implement more than one function in the program with the same function name? Answer: Function Overloading: The declaration of more than one function of with different set of arguments and return data types is called function overloading. Examples: Int sum(int, int); Float sum(float, float, float);
We can implement more than one
function in the program with the same function name by overloaded function. An overloaded function has the same name as some other function. It must have a parameter list that's different from all other functions with the same name so the compiler has some way to tell the functions apart. When you have overloaded functions, the compiler infers which of the functions to call from the parameters you provide it.
Question No.2: What is the Recursion
function how we can stop the recursion? Explain it with the help of an example. Answer: Recursion Function: Recursion function is that function that is called itself in its body. A recursive function repeats itself several times in order to compute or return final output. Recursive functions are quite common in computer programming as they allow programmers to write efficient programs with minimal code. We can stop the recursion function by using conditions. We can modify the function to return a bool that indicates whether we have found 0 or not and modify our logic to return from the function without changing path if 0 has been found. Here's the illustration of the idea: bool Graph::findPath( Room * curRoom ) { if( curRoom -> myNumber == 0 ) { cout << "Outside.\n"; //Escape the recursion! return true; } // ... if (findPath( curRoom -> North )) return true; // ... return false; } Example: Question No.3: What is the difference between void type pointer and simple pointer? Answer:
Void Pointer Simple Pointer
It is a specific pointer type. Simple pointer is specially reserved value of a pointer.
Void itself a data type of Simple pointer suits well
size 1. for all datatypes.
This data type is alone Int, char, float, long,
supported. double are all datatypes are supported.
Void pointer is used for Simple pointer is used for
storing address of other assigning 0 to a pointer variable irrespective of its variable of any type. datatype. Question No. 4: Write a program that takes the name, age, city as an input and then displays the data from the member of the structure. Answer:
Question No. 5: What is object-
oriented programming? Also discuss the features of OOPS. Answer: Object-Oriented Programming (OOP): It is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. Features of Object Oriented Programming: Some of the main features of the object oriented programming are: Objects Classes Inheritance Polymorphism Abstraction Encapsulation Exception Handling Objects: Objects are the basic unit of OOP. They are instance of class, which have data members and use various member functions to perform tasks. Class: A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. It declares & defines what data variables the object will have and what operations can be performed on the class's object. Inheritance: Inheritance is defined as the way to reuse once written code again and again. The class which is inherited is called the Base class & the class which inherits is called the Derived class. They are also called parent and child classes. So when, a derived class inherits a base class, the derived class can use all the functions which are defined in base class, hence making code reusable. Polymorphism: Polymorphism allows us to create functions with same name but different arguments, which will perform different actions. That means, functions with same name, but functioning in different ways. Abstraction: Showing of essential features of the application and hiding the details is known as abstraction. In C+ +, classes can provide methods to the outside world to access & use the data variables, keeping the variables hidden from direct access, or classes can even declare everything accessible to everyone, or may be just to the classes inheriting it. This can be done using access specifiers. Encapsulation: Encapsulation is defined as the binding of the data variables and functions together in class. It is also known as data binding. Exception Handling: Exception handling refers to the handling of unresolved exceptions or errors produced at runtime.
Question No.6: Define class and objects.
Write a program and discuss each statement of the program in your own words. Answer: Class: A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. It declares & defines what data variables the object will have and what operations can be performed on the class's object. Objects: Objects are the basic unit of OOP. They are instance of class, which have data members and use various member functions to perform tasks.
Python Projects for Beginners: Part 4. Object-Oriented Programming (OOP). Get an introduction to OOP concepts such as classes, objects, inheritance, and polymorphism