0% found this document useful (0 votes)
50 views13 pages

OOP University Exam Paper Oct 2022

Uploaded by

pranavram2520
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views13 pages

OOP University Exam Paper Oct 2022

Uploaded by

pranavram2520
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented Programming

(2019 Pattern) (Semester-I) (210243)

University Question Paper


Oct -22

Q1) a) What is the use of ‘this’ pointer. Explain with Example. [4]
Answer:

this pointer:

• It holds the address of current object, in simple words you can say that it points to the
current object of the class.
• It can be used to pass current object as a parameter to another method.
• It can be used to refer current class instance variable.
• Friend functions do not have a this pointer, because friends are not members of a class.
Only member functions have a this pointer.

Example:
class Stud
{
private:
int rno;
float marks;
public:
void setValues(int rno, float marks)
{
this->rno =rno;
this->marks=marks;
}
void displayValues()
{
cout<<rno<<endl;
cout<<marks;
}
};

int main()
{

Stud s1,s2,s3;
s1.setValues(10, 76.50);
s1.displayValues();
s2.setValues(3,56.8);
return 0;
}

b) What are the different ways to define member functions of a class? Give Examples of
Each. [4]
Answer:
There are two ways to define functions that belongs to a class:

• Inside class definition


• Outside class definition

Inside class definition:

class Example
{
public:
void add(int a, int b) //method defined inside class
{
cout<<”Addition is:”<<a+b;
}
};
int main()
{
Example obj;
obj.add(10, 20);
return 0;
}

Output:
Addition is: 30

Outside class definition:

class Example
{
int a, b;
public:
void add(int, int); //method declared inside class
};
void Example::add(int a, int b) //method defined outside class
{
cout<<”Addition is:”<<a+b;
}
int main()
{
Example obj;
obj.add(10, 20);
return 0;
}

Output:
Addition is: 30

c) Define inline function. Write a C++program for finding the area of a triangle using inline
functions. [7]
Answer:
• Inline function is a function that is expanded in line when it is called thus saving time.
• C++ provides an inline functions to reduce the function call overhead.
• When the inline function is called whole code of the inline function gets inserted or
substituted at the point of inline function call.

Syntax:
inline return-type function-name(parameters)
{
// function code
}

C++program for finding the area of a triangle using inline functions.

#include <iostream>
using namespace std;
inline float triangle_area(float base, float height)
{
float area;
area = (0.5 * base * height);
return area;
}
int main()
{
float b, h, a;
b = 4;
h = 6;
// compiler will substitute the inline function code here.
a = triangle_area(b, h);
cout<<"\nArea = "<<a;

return 0;
}

Output:
Area = 12

Q2) a) Compare Procedure oriented programming Vs Object oriented Programming. [4]


Answer:

Procedure oriented programming Vs Object oriented Programming.

Procedure oriented programming Object oriented Programming


Emphasis is on doing things (algorithms). Emphasis is on data rather than procedure.
Large programs are divided into smaller Programs are divided into what are known as
programs known as functions. objects.
Most of the functions share global data. Data structures are designed such that they
characterize the objects.
Data move openly around the system from Functions that operate on the data of an
function to function. object are ties together in the data structure.
Functions transform data from one form to Data is hidden and cannot be accessed by
another. external function.
Employs top-down approach in program Follows bottom up approach in program
design. design.

b) What is difference between pointer and references? [4]


Answer:
Pointer:
• Normal variable is used to store the value.
• A pointer is a variable that holds the address of another variable.
• Pointers are symbolic representations of addresses.
• We can have pointer to any variable type.

Syntax of Pointer :
data_type *pointer_name;

Example:

int a = 10;
int *p = &a;
// OR
int *p;
p = &a;

References:
A reference variable is an alias, that is, another name for an already existing variable. A
reference, like a pointer, is also implemented by storing the address of an object. A
reference can be thought of as a constant pointer (not to be confused with a pointer to a constant
value!) with automatic indirection, i.e., the compiler will apply the * operator for you.

Example:
int a = 10;
int &p = a; // It is correct
// but
int &p;
p = a; // It is incorrect as we should declare and initialize references at single step

c) Write C++ code that defines a class and declares and array of objects to that class. [7]
Answer:
C++ program:

class Book
{
char title[20];
float price;
public:
void getdata()
{
cout<<”\nEnter title of the book:”;
cin>>title;
cout<<”\nEnter price of the book”;
cin>>price;
}
void putdata();
};

void Book::putdata()
{
cout<<”\n”<<title<<”\t”<<price;
}

int main()
{
Book b[5]; //array of objects

cout<<”Enter details of books:”;


for(int i=0;i<3;i++)
{
cout<<”\nBook”<<i+1;
b[i].getdata();
}

for(int i=0;i<3;i++)
{
cout<<”\nBook”<<i+1;
b[i].putdata();
}

return 0;
}

Output:
Enter details of books:
Book1
Enter title of the book: OOP
Enter price of the book: 250
Book2
Enter title of the book: FDS
Enter price of the book: 280
Book3
Enter title of the book: Maths
Enter price of the book: 250

Book1
OOP 250
Book2
FDS 280
Book3
Maths 250

Q3) a) Discuss the role of access specifiers in inheritance and show their visibility when they
are inherited as public, private and protected. [4]
Answer:
Access specifiers:
• Public - members are accessible from outside the class
• Private - members cannot be accessed (or viewed) from outside the class
• Protected - members cannot be accessed from outside the class, however, they can be
accessed from inherited classes.

Access Within In derived Outside


Specifier same class class the class

Private Yes No No
Protected Yes Yes No

Public Yes Yes Yes

Modes of Inheritance:
• Public mode: If derive a sub class from a public base class. Then the public member
of the base class will become public in the derived class and protected members of the
base class will become protected in derived class.
• Protected mode: If derive a sub class from a Protected base class. Then both public
member and protected members of the base class will become protected in derived
class.
• Private mode: If derive a sub class from a Private base class. Then both public member
and protected members of the base class will become Private in derived class.

b) Explain the void and null pointer with example [4]


Answer:

Null pointer:
• Besides memory addresses, there is one additional value that a pointer can hold: a null
value.
• A null value is a special value that means the pointer is not pointing at anything.
• A pointer holding a null value is called a null pointer.

Example:
#include <iostream>
using namespace std;
int main ()
{
int *ptr = NULL;
cout << “ptr value = " << ptr ;
return 0;
}

Output:
ptr value = 0

Void pointer:

• It holds the address of any data type, but it is not associated with any data type.
• Syntax :
void *ptr;
• The size of void pointer varies system to system. For 16 bit system it is 16-bit. For 32
bit system, it is 32-bit and for 64 bit system the size is 64-bit.
• In C++, you cannot assign the address of variable of one type to a pointer of another
type.
e.g.
int *ptr;
double d = 9;
ptr = &d; // Error: can't assign double* to int*
• To avoid it, make use of general purpose pointer i.e. void pointer.

Example:
#include <iostream>
using namespace std;
int main()
{
void* ptr;
float f = 2.3;
ptr = &f;
cout << &f << endl;
cout<<ptr;
cout<<*((float*)ptr);
return 0;
}

Output :

0xffd117ac
0xffd117ac
2.3

c) Write a C++ program to demonstrate pointers to base and derived classes. [7]
Answer:
Pointers to base and Derived classes:
• Pointers can be declared to point base or derived classes.
• Base class pointer can point to objects of base and derived class.
• Pointer to derived class object cannot point to objects of base class.

Example:
#include <iostream>
using namespace std;

class Base
{
public:
void show ()
{
cout<<"\nshow() from base class";
}
};
class Derived: public Base
{
public:
void display ()
{
cout<<"\ndisplay() from derived class";
}
};

int main()
{
Base B1;
Derived D1;
Base *base_ptr; //Base class pointer
Derived *der_ptr; //Derived class pointer

base_ptr = &B1; //Base class pointer points to object of base class


base_ptr->show();

base_ptr = &D1; //Base class pointer points to object of derived class


base_ptr->show();
//base_ptr->display(); //Error: class ‘Base’ has no member named ‘display’
static_cast<Derived*>(base_ptr)->display(); //upcasting

//der_ptr = &B1; //Derived class pointer cannot point to object of base class

der_ptr = &D1; //Derived class pointer points to object of derived class


der_ptr->show();
der_ptr->display();

return 0;
}

Output:
show() from base class
show() from base class
display() from derived class
show() from base class
display() from derived class

A derived-class reference or pointer is Upcast to a base class. To put it another way,


upcasting enables us to handle derived types as if they were their base types.

Q4) a) Explain friend function with example. [4]


Answer:

Friend function:
• 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 of the class in which they are
declared.
• To make a function as a friend of a class, it is declared inside the class either in private
or in public section with keyword friend before its declaration.
Example:
#include<iostream.h>
class Student
{
int rollno;
friend void disp(Student);
public:
void display();
};
void Student::display()
{
cout<<"\nRoll No:"<<rollno;
}
void disp(Student s)
{
cout<<"\nRoll No:"<<s.rollno;
}

int main()
{
Student s;
s.display();
disp(s);
return 0;
}

b) How are arrays represented using Pointers. Explain with example. [4]
Answer:
Accessing Array Elements Using Pointer:
Pointers are the variables that hold address. Not only can pointers store address of a single
variable, it can also store address of cells of an array.

Example:
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {5, 2, 9, 4, 1};
int *ptr = &arr[2]; // ptr=&arr[2];
cout<<"The value in the second index of the array is: "<< *ptr;
return 0;
}
Output:
The value in the second index of the array is: 9

Array represented using arrays:

c) Explain hybrid inheritance with a C++ example. [7]


Answer:
Hybrid inheritance:
The process of combining more than one type of Inheritance together while deriving subclasses
in a program is called a Hybrid Inheritance.

A real-life example of hybrid inheritance:

C++ program:
#include <iostream>
using namespace std;
class vehicle
{
public:
vehicle()
{
cout<< "This is a vehicle\n";
}
};
class Car: public vehicle
{
public:
Car()
{
cout<< "This is a car\n";
}
};

class Racing
{
public:
Racing()
{
cout<< "This is for Racing\n";
}
};
class Ferrari: public Car, public Racing
{
public:
Ferrari()
{
cout<< "Ferrari is a Racing Car\n";
}

};
int main()
{
Ferrari f;
return 0;
}

Output:
This is a vehicle
This is a car
This is for Racing
Ferrari is a Racing Car

You might also like