PracticeFinalTest
PracticeFinalTest
struct RAM {
int size;
};
Using the structure “RAM” above, complete the following class named Memory . Your
class represents a set of RAMS in a Memory and your design meets the following
specifications. The class has
two attributes:
- m_memory — a pointer to a dynamically allocated array of RAMS.
- m_size — the number of RAMS in the Memory.
a constructor with one parameter for number of RAMS. This constructor
dynamically allocates an array of RAMS to the number RAMS received through
the parameter and keeps its address in the m_memory pointer.
An overload the += operator for a int as the right operand. This will increase the
size of each RAM in the Memory by the amount of the int operand.
An overloaded << operator so Memory can be printed using cout.
If invalid data is received you will set the object to a safe empty state where
applicable.
Part B:
Create an Interface named Employee. This class has no data members and includes
two functions: a modifier named setData() and a query named displayData().
Part C:
Derive a class named Doctor from your Employee class, with the following
properties/member functions:
• A char array that holds the specialization (The length is unknown).
• An integer that holds an employee identifier.
• A public constructor that takes two arguments, an integer and the address of an
unmodifiable char array. These arguments should be used to initialize the values of the
doctor’s specialization and identifier.
• A public setData() member function. This function asks the user for the doctor’s
identifier and specialization. For this function only, you may assume that the user will
only enter up to 100 characters.
• A public displayData() query that displays the doctor’s specialization and identifier.
• Make sure a Doctor can be assigned to another Doctor but cannot be copied.
• Make sure there is no memory leak after a Doctor object goes out of scope.
Part D:
Consider the following function, which calculates and returns the multiplication of the set of values
pointed to by x.
Write a function template that extends this definition to any fundamental type.
Part E:
Identify one problem in each of the following code (Logical/runtime or Compile Error). By making
changes in only one line, the problem in the whole code should be fixed and obtain the expected
output. For full marks present your answer as follows:
Please note that the line number stated in the answer must correctly point to the problem location. If
the line number does not match the problem, the mark for the answer will be zero.
E.1
E.2
E3.
E4.
Part F:
Using your own words, briefly answer the following questions:
1- Explain the difference between declaring the data members as private vs public
vs protected.
3- Explain shallow and deep copying in classes. Identify the type of the class that is
suitable or each
4- Explain the concept of dynamic function binding in the context of inheritance. In your
explanation, you should mention some class names and a member function name to
demonstrate your understanding. In brief, use some C++ code in your explanation.
6- When implementing a binary operator overload (such as +=), what is the significance of
having it be it member operator versus a helper operator?
7- With the concept of class hierarchies and abstract base classes in mind, how can the use of an
array of pointers improve the ability to access different portions of the hierarchy? Explain your
reasons.
Part G:
Consider the following code. Answer questions 1 to 4.
Box(const Box& b) {
nc = b.nc;
for (int i = 0; i < nc; i++)
ch[i] = b.ch[i];
cout << "E" << endl;
}
1- Explain what the operator Box& operator+=(const Chocolate& c) does.
2- How can you improve the performance the show() function in this program. Explain
the effect of your upgrade in one or two sentences.
3- Explain in one or two sentences the meaning of the keyword const in the display()
functions?
4- Describe in one or two sentences the effect of removing the default parameter value in
the definition of the Chocolate() constructor.
Part H:
1- What is the exact output of the following code
2- What is the exact output of the following code
3- What is the exact output of the following code
}
4- What is the exact output of the following code