The document contains a series of questions and answers related to Object-Oriented Programming (OOP) concepts such as pointer operations, inheritance visibility modes, operator overloading, polymorphism, and stream classes. It also includes programming tasks demonstrating these concepts, such as overloading operators and using pointers with classes. Additionally, it discusses the 'this' pointer and provides examples for better understanding.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views
OOP IMP Que with Ans
The document contains a series of questions and answers related to Object-Oriented Programming (OOP) concepts such as pointer operations, inheritance visibility modes, operator overloading, polymorphism, and stream classes. It also includes programming tasks demonstrating these concepts, such as overloading operators and using pointers with classes. Additionally, it discusses the 'this' pointer and provides examples for better understanding.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
IMP Que.
For OOP Unit Test 2
1.Question for two marks .
1.write down program for pointer incrementation and decrementation. Ans.- 2.St ate different types of visibility mode in inheritance. Ans.-There are three visibility modes in inheritance: 1. Private 2. Public 3. Protected 3.What is Operator overloading ? Almost all arithmetic operator can be overloaded to perform arithmetic operation on userdefined data type. Operator overloading is a way of providing new implementation of existing operators to work with user-defined data types. An operator can be overloaded by defining a function to it. The function for operator is declared by using the operator keyword followed by the operator. There are two types of operator overloading in C++ • Binary Operator Overloading • Unary Operator Overloading 4.Define polymorphism. List types of polymorphism. Ans.- Polymorphism is the ability to take more than one forms. A function may behave differently for performing various tasks. Functions having same name but different number of arguments/return type in same program is called as polymorphism. Types of polymorphism: 1) Compile time polymorphism 2) Run time polymorphism 5.What is stream? • C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs. A C++ stream is a flow of data into or out of a program, such as the data written to cout or read from cin. • C++ provides standard iostream library to operate with streams. • The iostream is an object-oriented library which provides Input/Output functionality using streams.
6.What are the Advantages of Stream Classes?
Ans.-• Stream classes have good error handling capabilities. • These classes work as an abstraction for the user that means the internal operation is encapsulated from the user. • These classes are buffered and do not uses the memory disk space. • These classes have various functions that make reading or writing a sequence of bytes easy for the programmer. 7.What is static polymorphism? Ans.-In overloaded functions, when an appropriate member function is selected by matching arguments at compile time then it is known as static polymorphism. OR Linking of function call with its definition at the compile time is called as static polymorphism. 8.Give syntax of create pointer to object. Ans.-Syntax: class_name *pointer_variable, object_name; pointer_variable=&object_name; OR class_name object_name; class_name *pointer_variable=& object_name; 2.Question for Four marks . 1.What is pure virtual functions? Write down the rules used for pure virtual function. Ans.- A pure virtual function is a function which is declared in a base class and which does not have definition relative to the base class. In such cases, the compiler requires each derived class to either defined in a derived class or is re-declared as pure virtual function. A pure virtual function in base class only serves as a placeholder. Such functions are also called as ‟do-nothing‟ functions. Ex:- class ABC { public: virtual void display( )=0; }; Rules:- 1. A pure virtual function in base class does not have definition relative to base class. 2. A class containing pure virtual functions cannot be used to declare any objects of its own. 2.Difference between Runtime Polymorphism and Compile Time polymorphism . Ans.-
3.Write a Program Overloading Unary Increment and Decrement using
Member Functions. Ans.- #include<iostream> class Counter { private: int count; public: // Constructor Counter() : count(0) {} Counter(int c) : count(c) {} // Overload ++ using member function (prefix increment) Counter operator++() { ++count; return *this; } // Overload -- using member function (prefix decrement) Counter operator--() { --count; return *this; } // Display the value of count void display() const { std::cout << "Count: " << count << std::endl; } }; int main() { Counter c1(5); ++c1; // Using member function to increment c1.display(); // Output: Count: 6 --c1; // Using member function to decrement c1.display(); // Output: Count: 5 return 0; } 4. Write a C++ program to declare a class birthday having data members day, month, year. Accept this information for five objects using pointer to the array of objects Ans.- #include <iostream> using namespace std; class Birthday { public: int day; int month; int year; }; int main() { Birthday birthdays[5]; // Array of 5 Birthday objects Birthday *ptr = birthdays; // Pointer to the first element of the array for (int i = 0; i < 5; i++) { cout << "Enter day, month, and year for birthday " << i + 1 << ": "; cin >> ptr->day >> ptr->month >> ptr->year; ptr++; // Increment the pointer to point to the next object } cout << "\nBirthdays:\n"; ptr = birthdays; // Reset the pointer to the beginning for (int i = 0; i < 5; i++) { cout << "Birthday " << i + 1 << ": " << ptr->day << "/" << ptr->month << "/" << ptr->year << endl; ptr++; } return 0; } 5.Write a program for overloading of ++unary operator for inch to feet conversion. 12 inch = 1 feet. Ans.- #include<iostream.h> #include<conio.h> class abc { int i,f; public: abc(int f1,int i1) { f=f1; i=i1; } void operator ++() { while(i>11) { f++; i=i-12; } cout<<”Number of feet =”<<f<<”Number of inches:”<<i; } }; int main() { clrscr(); abc a1(2,49); ++a1; getch(); return 0; } 6.Write a program using function overloading to swap two integer number and swap two float number. Ans.- #include<iostream.h> #include<conio.h> int swap(int a,int b); float swap(float c, float d); int swap(int a,int b) { int temp; temp=a; a=b; b=temp; cout<<a<<","<<b<<endl; } float swap(float c, float d) { float temp; temp=c; c=d; d=temp; cout<<c<<","<<d<<endl; } void main() { clrscr(); int a,b,temp; float c,d; cout<<"Enter value for a & b="<<endl; cin>>a>>b; swap(a,b); cout<<"Enter value for c & d="<<endl; cin>>c>>d; swap(c,d); getch(); } 7.What is ‘this’ pointer? Give suitable example. Ans.- ‘this’ pointer: 1. C++ uses a unique keyword called „this‟ to represent an object that invokes a member function. 2. This unique pointer is automatically passed to a member function when it is invoked. 3."this" is a pointer that always point to the object for which the member function was called. 4. For example, the function call A.max () will set the pointer "this" to the address of the object A. Next time suppose we call B.max(), the pointer "this‟ will store address of object B. Consider the following example: #include<conio.h> #include<iostream> class sample { int a; public: void setdata(int x) { this ->a=x; } void putdata() { cout<<this ->a; } }; void main() { clrscr(); sample s; s.setdata(100); s.putdata(); getch(); } In the above example, this pointer is used to represent object s when setdata() and putdata() functions are called.