MidQPaper_OOP_Solution
MidQPaper_OOP_Solution
(i) State the difference between private and public access specifiers.
Private members can be accessed only within the class
Public members can be accessed from outside the class as well
b. Find the errors in the following C code. Remove the errors and find the
output.
#include<stdio.h>
int main()
{
int i=3, *j, k;
j = i;
print("%d\n", i**j*i+*j);
return 0;
}
#include<stdio.h>
int main()
{
int i=3, *j, k;
j = &i;//error remove
print("%d\n", i**j*i+*j);
return 0;
}
Output 30
OR
(B) What are the different types of functions in C++? Explain pass by [04]
value and pass by reference with respect to functions in C++ with a
suitable example.
Inline functions
Friend functions
Overloaded functions
Virtual functions
Functions with arguments and return values
Functions with arguments and without return values
Functions without arguments and with return values
Functions without arguments and without return values
Call by Value
In call by value, the actual value that is passed as argument is not changed after
performing some operation on it. When call by value is used, it creates a copy
of that variable into the stack section in memory. When the value is changed, it
changes the value of that copy, the actual value remains the same.
#include<iostream>
using namespace std;
void my_function(int x) {
x = 50;
cout << "Value of x from my_function: " << x << endl;
}
void main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 10
Call by Reference
In call by reference the actual value that is passed as argument is changed after
performing some operation on it. When call by reference is used, it creates a
copy of the reference of that variable into the stack section in memory. Is uses
a reference to get the value. So when the value is changed using the reference it
changes the value of the actual variable.
#include<iostream>
using namespace std;
main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output:
Value of x from my_function: 10
Value of x from main function: 50
Q-3 (A) Create a class time with members hours and minutes. Write a member function
display() to print the time. Write a main program and create 2 objects of class
time and display the time.
#include<iostream>
Using namespace std;
class time
{
int hours, minutes;
public:
void getdata()
{
cout<<”Enter time in hours and minutes form”;
cin>>hours;
cin>>minutes;
}
Void display()
{
Cout<<”Time is”<<hours<<”Hours and”<<minutes<<”Minutes”;
}
};
Int main()
{
time t1, t2;
t1.getdata();
t2.getdata();
t1.display();
t2.display();
return 0;
}
(B) What do you mean by function overloading? List the rules and explain with a [04]
suitable example.
C++ provides function overloading which allows to use multiple
functions sharing the same name. Function overloading is an example
of compile time polymorphism.
However, the two functions with the same name must differ in at least
one of the following,
a) The number of arguments
b) The data type of arguments
c) The order of appearance of arguments
d) Function overloading does not depend on return type.
Example:
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;
}
int main()
{
int a = 5; float b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}
OR
(B) Create a Class shape with members length and breadth. Write member [04]
functions to initialize the members and display the members. Write a function
area() to find the area of a rectangle using functions with default arguments.
class shape
{
public:
int length, breadth;
void input()
{
cout<<”Enter length and breadth of a Rectangle:”
cin>>length>>breadth;
}
void display()
{
cout<<”The length and Breadth of a Rectangle are:”;
cout<<length<<breadth;
}
int area(length=10, breadth=5)
{
return length*breadth;
}
};
int main(){
shape s;
s.area();
s.area(4);
s.area(5,8);
return 0;
}