Professional Documents
Culture Documents
SYED SHAMS HAIDERr 203 OOP FINAL
SYED SHAMS HAIDERr 203 OOP FINAL
SYED SHAMS HAIDERr 203 OOP FINAL
PART (A)
Explain the difference between the
following :Passing value by reference, Passing
value by pointers, Passing value by address
ANSWER
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)
ANSWER
PART(E)
Explain the difference between the keywords
struct and class.
ANSWER
QUESTION 2
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;
};
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;
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
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: