OOP - FINAL - Fall 2019 0.1 (Sol)
OOP - FINAL - Fall 2019 0.1 (Sol)
a).
diamond problem
Automatic :
Declared (memory allocated)
Deleted automatically when scope ends
Static:
aggregation
V. What is down-casting?
VII. Which is the condition that must be followed if the array of objects is declared
without initialization, only with size of array?
Default constructor
abstraction
IX. Identify the following code as deep copy, shallow copy, memory leak and
dangling pointer.
int * p = new int [5]; int * p = new int[10];
for (int i =0;i<5;i++) int * q=p;
p[i]=i; for (int i =0;i<10;i++)
int * q=new int [5]; p[i]=i;
for (int i =0;i<5;i++) delete [] p;
*(q+i)=p[i]; for (int i =0;i<10;i++)
delete [] p; *(q+i)=i*2;
delete [] q; delete [] q;
Source: Destination:
1 0 1 1 0 0 0 0 1
0 1 2 3 4 5 0 1 2
If i=2
Source:
1 0 1 0 0 1 1 0 0
0 1 2 3 4 5 6 7 8
Solution:
int j = 0;
temp[j] = x[j];
int k = 0;
temp[j + k] = y[k];
int m = j + k;
temp[m] = x[j];
m++;
delete[]x;
x = temp;
Consider the class Product that has following members: Product ID, expiry date, cost
and name. Write a function Sort() that takes an array of Products and its size as
parameters. The function Sort must sort all the products according to their expiry
date. In sorted order a product comes first whose expiry date is earlier. If two products
are expired on same date then one must come first which is more expansive than the
other.
Note that all data members of all the classes are private and there are no friend
functions or classes. Also, you can’t add new data members in the classes.
Product Id: 5 Product Id: 8 Product Id: 2 Product Id: 5 Product Id: 5
Expiry date: Expiry date: Expiry date: Expiry date: Expiry date:
Cost: Rs. Cost: Rs. Cost: Rs. Cost: Rs. Cost: Rs.
200 500 250 340 200
Product Id: 8 Product Id: 2 Product Id: 5 Product Id: 5 Product Id: 5
Expiry date: Expiry date: Expiry date: Expiry date: Expiry date:
Cost: Rs. Cost: Rs. Cost: Rs. Cost: Rs. Cost: Rs.
500 250 200 200 340
Solution:
class Product{
int productID;
date Expir
};
#include <iostream>
#include <conio.h>
class A {
int m;
public:
A(int a = 0);
return m;
void set(int k) {
m = k;
};
class B:public A {
int n;
public:
n = b;
if (this != &a) {
m = a.m;
return *this;
if (this != &a) {
n = a.n;
set(a.get());
return obj;
obj << "(" << b.get() << "," << b.n << ")" << endl;
return obj;
A::A(int a) {
m = a;
A A::operator*(int x) {
A obj;
obj.m = m * x;
return obj;
B B::operator*(int x) {
n = n * x;
return *this;
B *a = arr;
a[i] = a[i] * 3;
*temp = *temp * 2;
return temp;
void main() {
int size = 5;
B * p = new B[5]{ B(1, 1), B(2, 2), B(3, 3), B(4, 4), B(5, 5) };
A *x = function(p, size);
delete[] p;
delete[] x;
_getch();
X:
X*3:
X:
Zain and Umair are playing a game. In this game, there are initially N bins in a circle,
conveniently labeled 1 through N in clockwise order — for each valid i, bin i+1 is the
next bin clockwise from bin i, and bin 1 is the next bin clockwise from bin N. Also, there
is a ball in bin 1.
The game is played with fixed integer parameters R and K. Initially, the players
designate one of the bins as a special bin. Then, they alternate turns; Zain plays first.
The turns are numbered starting from 1. In each turn, the following happens:
The current player must choose an integer s between 1 and K (inclusive) and move the
ball s steps clockwise, i.e. move it to the next bin clockwise (that has not been
removed yet) s times.
Then, if the number of the current turn is not divisible by R, nothing happens.
Otherwise, let's denote the bin that currently contains the ball by b. The current player
must remove bin b from the circle.
If the removed bin is the special bin, the current player wins the game.
Otherwise (only if the number of the current turn is divisible by R), the ball is placed in
the next bin clockwise from bin b that has not been removed yet.
It is clear that the game is finite and always has a winner. For each choice of the special
bin, determine the winner of the game.
Input:
The input contains three space-separated integers N , K∧R . Constraints for the problem
are given below.
● 1 ≤ K ≤ N ≤ 1000
● 1 ≤ R ≤ 10,000
● R is odd
Output:
Print a single line containing a string with length N. For each valid i, the i-th character
of this string should be 'Z' if Zain wins or 'U' if Umair wins when bin i is special.
For input 5 1 1 the output is:
ZZZUU
#include <array>
#include <string>
#include<conio.h>
#include <iostream>
void main() {
int R = 0;
int K = 0;
int special = 5;
int s = 1;
int i = 0;
int turn = 1;
int b = 1;
v[0] = b;
cout << "Enter R such that 1=R =10,000 " << endl;
cin >> R;
cout << "Enter K such that 1=K =N =1000 " << endl;
cin >> K;
while (!over) {
cout << "Enter a number between 1 and " << R << endl;
cin >> s;
s--;
v[i] = 0;
i = (i + s) % N;
if (i == special) {
over = true;
else
else
v[i] = b;
if (turn %R == 0) {
int x = (i + 1) % N;
v[x] = b;
vector<int>::iterator p;
p = v.begin();
p = p + i;
v.erase(p);
i = x;
turn++;
_getch();