SYED SHAMS HAIDERr 203 OOP FINAL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

QUESTION 1

PART (A)
Explain the difference between the
following :Passing value by reference, Passing
value by pointers, Passing value by address

ANSWER

The reason for this is that


a const (or volatile or virtual) static method
wouldn't make sense (in the traditional sense,
see below). For example, const implies you
can't modify the object's members, but in the
case of statics, there's no object to talk about.

You could argue that a const static could apply


to other static members, but this option was
regarded as pointless.
PART (B)
Give two reasons to pass an object by
reference.
ANSWER
Passing objects by reference is cheap and it
also becomes handy when you want to
share/update the object information between
multiple clients of the object. Complex data
structures (especially those that are recursive)
require pointers. Passing objects by reference is
just a safer way of passing pointers.

PART (C)
Explain Why can’t the constant pointer this be
used inside a static method.

ANSWER
First, because static member functions are
not attached to an object, they have
no this pointer.
This is because non-static members must
belong to a class object, and static member
functions have no class object to work with.

PART(D)

Explain Why is protected accessibility needed?

ANSWER

In C++, protected members form the interface


the class exposes to its derived classes, which is
distinct from the public interface of the class,
formed by public and non-member functions,
or from its customization interface, formed by
virtual member functions.

There is also protected inheritance, which may


be used for "controlled polymorphism": within
derived classes, the derived class is a base:
references to it may be used where references
to base are expected.

PART(E)
Explain the difference between the keywords
struct and class.
ANSWER

The members and base classes of a struct are


public by default, while in class, they default to
private. A struct simply feels like an open
pile of bits with very little in
the way of encapsulation or functionality.

QUESTION 2

Write a program that has an abstract base class. This


class should have four member data variables and
a pure virtual function. It should also have a method
for setting the data variables. Derive a class from
Base
class and override a method in the base class. Write a
main function that creates an object of derived class
and sets the variables. Also write a top-level function
that will take a parameter of type base class and
return
the value of the appropriate (override) function.

SOURCE CODE:
#include <iostream>
using namespace std;
//SYED SHAMS HAIDER FA19-BEE-203/ISB
class Shape
{
protected:
double width, height, x, y;
public:
void set_data (double a, double b, double c,
double d)
{
width = a;
height = b;
x = c;
y = d;
}
virtual double area() = 0;
};

class Rectangle: public Shape


{
public:
double area ()
{
return (width * height * x * y);
}
};

class Triangle: public Shape


{
public:
double area ()
{
return (width * height * x * y )/2;
}
};

int main ()
{
Shape *sPtr;

Rectangle Rect;
sPtr = &Rect;
sPtr -> set_data (5,12,4,5);
cout << "Area of Rectangle is " << sPtr -> area()
<< endl;

Triangle Tri;
sPtr = &Tri;

sPtr -> set_data (4,7,9,3);


cout << "Area of Triangle is " << sPtr -> area()
<< endl;
return 0;
}

DEV C++
OUTPUT:

QUESTION 3
Suppose we have a class (for example name A).
Write a program which consists of the following
statements:
A* obj1 = new A(,);
obj1->member_function1 ();
obj1->member_function2();
A::member_function3(obj1);
bool name1 = obj->member_function4 ();
A *obj2 = new A();
int r = A->compare(obj2);
A obj3(obj2);
Your code should replace the following names:
A, obj1, name1, member_function 1,
member_function 2, member_function 3, obj1,
obj2, obj3. You can also pass parameters in the
functions and constructors.

SOURCE CODE:
#include <iostream>
using namespace std;
//SYED SHAMS HAIDER REG 203
class A
{
public:
char item;
A(char a = 0)
{
item = a;
}
A(const A* a)
{
item = a->item;
}
void member_function1()
{
cout << "member_function1" << endl;
}
void member_function2()
{
cout << "member_function2" << endl;
}
char member_function3()
{
cout << "member_function3" << endl;
// return a.item;
}
bool member_function4()
{
cout << "member_function4" << endl;
return 1;
}
int compare(A *temp)
{
if(item == temp->item)
{
return 1;
}
else
{
return 0;
}
}
A member_function(A *&a)
{
}
};
int main()
{
A* obj1 = new A(',');
obj1->member_function1 ();
obj1->member_function2();
// A::member_function(obj1);
bool name1 = obj1->member_function4();
A *obj2 = new A();
int r = obj1->compare(obj2);
A obj3(obj2);
return 0;
}

DEV C++
OUTPUT:
QUESTION 4

Write a program that performs exception handling. A


class should have two exception handling classes.
This program throws an exception of type both
classes (user defined data types) and they are
catched in a catch block. This class also throws an
exception of built-in data types (of your choice). Also
in your program include a catch block for all data
types.

SOURCE CODE
#include<iostream>
#include<string>
#include<stdexcept>
using namespace std;
//SYED SHAMS HAIDER REG 203
class EmptyStackException {
string msg;
public:
EmptyStackException(string t_msg)
{
msg = t_msg;
}
string getMessage() {
return msg;
}
const char* what() {
return msg.c_str();
}
};
class StackFullException{
string msg;
public:
StackFullException(string t_msg)
{
msg = t_msg;
}
string getMessage() {
return msg;
}
};
class Stack {
int arr[50];
int index;
public:
Stack()
{
index = 0;
}
void push(int temp) {
if(index >= 50)
throw StackFullException("Stack is full");
if(temp < 0)
throw invalid_argument("Negative values
are not allowed");
arr[index++] = temp;
}
int top() {
if(index <= 0)
throw EmptyStackException("Stack is
empty");
else
return arr[index-1];
}
void pop()
{
if(index <= 0)
throw EmptyStackException("Stack is
empty");
else
{
arr[index] = 0;
--index;
}
}
};
int main()
{ try {
Stack s1;
// s1.pop();
for(int i = 0; i < 50; i++)
s1.push(i);
cout<<s1.top()<<endl;
// s1.push(25);
s1.pop();
s1.push(-1);
}
catch(EmptyStackException& e) {
cout<<"Exception thrown:
"<<e.getMessage()<<endl;
}
catch(StackFullException& e) {
cout<<"Exception thrown:
"<<e.getMessage()<<endl;
}
catch(invalid_argument& exc)
{
cout<<"Exception thrown:
"<<exc.what()<<endl;
}
catch(...)
{
cout<<"Default Exception"<<endl;
}
}

DEV C++
OUTPUT:

You might also like