A) Suppose you have a class called base which has been inherited by a class call
ID: 3859615 • Letter: A
Question
A) Suppose you have a class called base which has been inherited by a class called derived.
( both classes have public, protected and private members )
What can a client of derived access within the class base ?
What can the class derived access within the class base ?
B) Suppose you have a class called more which has a private object of a class called simple. ( both classes have public, protected and private members )
What can a client of more access within the class simple ?
What can the class more access within the class simple ?
C) What is a pointer variable ?
D) What is a destructor and which are its properties ?
E) What are the two differences between a dynamic variable and an ordinary variable?
F) What are the advantages of using a dynamic array instead of a regular array ?
G) When does memory leak occur ? How can you minimize it ?
H) Suppose you have an int pointer which contains the address of a dynamic variable.
What will you be able to change if you pass to a function the pointer as a value
parameter ?
How about if you pass the pointer as a reference parameter?
I) What does operator overloading allow us to do ?
J) What is composition ?
K) What is inheritance ?
L) Explain the difference between the privateand protected members of a class.
M) What is a friend function ?
N) What do class templates allow us to do ?
O) Why must the << and the >> operators be overloaded as friend functions instead of member functions, for a user defined class ?
P) What is a copy constructor ? Why and when do we need it ?
Q) What is a struct ?
R) Use the following declarations for all parts of this question:
struct date {
int day, month, year; };
date *today;
int *p;
1) Create a dynamic integer variable using pointer p and a dynamic structure variable
using pointer today.
2) Assign 10 to the dynamic integer variable and May 21, 1986 to the dynamic structure
variable.
3) Release memory for the dynamic integer variable and create a dynamic integer array
of size 50 using pointer p.
4) Release memory for the dynamic structure variable and the dynamic array, and clear
the contents of pointer p and today.
S) Consider the following two classes:
class simple class more: public simple
{ {
private: private:
int a; int x;
protected: protected:
char c; double z;
public: public:
simple ( int, char ); more ( int, char, int, double );
void read (); void set ();
void print (); void print();
}; };
Answer the following questions ( be precise ! )
1) What can a client of more access within the class more?
2) What can a client of more access within the class simple?
3) What can the class more access within the class simple?
4) The first two parameters of the constructor ofmore should be given to the constructor
of simple the last two are for x and z . Write the definition of the constructor of more.
5) Write a call to the function read of classsimple to be included in the function set -
of class more.
6) Write a call to the function print classsimple to be included in the function print -
of class more.
T) Consider the following simple class:
class Rectangle
{
private:
double length, width;
public:
Rectangle ( double l, double w ){ length = l; width = w; }
double area() { return length * width; }
double perimeter(){ return 2* ( length + width ); }
};
1) Write a copy constructor for the class.
2) Consider the following class that uses the Rectangle class defined above:
class artItem
{
private:
Rectangle canvas_frame;
Rectangle canvas;
String title;
};
a) Write a constructor which initializes the picture_frame, the canvas, and the title. The constructor
receives a string parameter to initialize the title, a Rectangle object to pass to the Rectangle
copy constructor when creating the picture_frame object, and a Rectangle object to pass to the
Rectangle copy constructor when creating the canvas object.
b) Write a print method that prints the title and prints the perimeter of picture_frame and the area
of canvas call the methods area and perimeter to get both values.
3) From the Rectangle class defined in above, derive a class named Box having an additional data
member named depth, a constructor with three parameters ( one for the depth and two for the
constructor of Rectangle) , and a volume method that returns the volume of the box
volume is length *width * depth.
U) Write the following functions:
1.Write a function, to be included in a template sorted/unsorted list class, called smaller, that will receive an xtype parameter. The function will return how many items in the list are smaller than the parameter.
2.Write a function, to be included in a template sorted/unsorted list class, called print_twice, that will print the list first forward and then backwards.
3.Write a function, to be included in a template unsorted list class, called replace_item, that will receive two xtype parameters, one called olditem, the other called newitem. The function will replace all occurrences of olditem with newitem ( if olditem exists !! ) and it will return the number of replacements done.
4.Write a function, to be included in a template sorted/unsorted list class, that will delete all occurrences of
an xtype parameter.
5.Write a function, to be included in a template sorted list class, that will delete all items that are greater
than an xtype paramter.
6.Write a function, to be included in a template sorted list class, that will return the number of occurrences of an xtype paramter.
7. Write a function, to be included in a template sorted list class that will receive two xtype parameters
called smallest and largest. The method will count and return how many values in the list are between
smallest and largest.
Explanation / Answer
A) What can a client of derived access within the class base ?
Answer : any client of a derived class can access only public member of its base class.
What can the class derived access within the class base ?
Answer : derived class can access both protected and public member of its base class.
B) What can a client of more access within the class simple ?
Answer : any client of more can access only public member of simple class.
What can the class more access within the class simple ?
Answer : Class more can access both public and private(through getters and setters) members of simple class.
C) a pointer is nothing more than an address, and a pointer variable is just a variable that can store an address.
When we store the address of a variable i in the pointer variable p, we say that p points to i.
int i;
*p = &i;
D) A destructor is a special member function of a class that are called when objects are destroyed (deallocated). Designate a function as a class's destructor by preceding the class name with a tilde (~).
Properties :
Do not accept arguments.
a) Cannot specify any return type (including void).
b) Cannot return a value using the return statement.
c) Cannot be declared as const, volatile, or static. However, they can be invoked for the destruction of objects declared as const, volatile, or static.
d) Can be declared as virtual. Using virtual destructors, you can destroy objects without knowing their type . Note that destructors can also be declared as pure virtual functions for abstract classes.
E) A dynamic variable is a variable whose address is determined when the program is run.
In contrast, an ordinary variable is like a container it can hold any value and we can change the value of ordinary variable at a time throughout the program
F) Usually arrays need to have a size when you create them. if you don't know the size or want to set it later, you need dynamically allocated arrays.
It is different from an array in that there is no preset upper limit on the number of items that it can contain.
G) Memory leak occurs when programmers create a memory in heap and forget to delete it.
To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.
/* Function without memory leak */
#include <stdlib.h>;
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
free(ptr);
return;
}
H)
Pass By Value
The local parameters are copies of the original arguments passed in
Changes made in the function to these variables do not affect originals
Pass By Reference
The local parameters are references to the storage locations of the original arguments passed in.
Changes to these variables in the function will affect the originals
No copy is made, so overhead of copying (time, storage) is saved
I) The programmer can redefine or overload most of the built-in operators available in C++. Thus a programmer can use operators with user-defined types as well.
Overloaded operators are functions with special names the keyword "operator" followed by the symbol for the operator being defined.
Example : If there are two objects of a class that contains string as its data members. You can redefine the meaning of + operator and use it to concatenate those strings.
J) Composition is simply the parts that make up the whole. A car has wheels, an engine, and seats. Inheritance is a "is a " relationship. Composition is a "has a" relationship.
Functionality of an object is made up of an aggregate of different classes.
class Engine
{
}
class Automobile
{
}
class Car extends Automobile // car "is a" automobile //inheritance here
{
Engine engine; // car "has a" engine //composition here
}
K) Inheritance is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class.
The superclass and subclass have “is-a” relationship between them.
L) private - only available to be accessed within the class that defines them.
protected - accessible in the class that defines them and in other classes which inherit from that class.
M) 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.
To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend.
N) Class templates are that class can have members that use template parameters as types.
template <class T>
class mypair {
T values [2];
public:
mypair (T first, T second)
{
values[0]=first; values[1]=second;
}
};
O) When overloaded as a member function, a << b is interpreted as a.operator<<(b), so it only takes one explicit parameter (with this as a hidden parameter).
The compiler knows that a friend function can't be part of the class, so instead of defining a member function, this "injects" the name of that function into the surrounding scope (the global scope, in this case). Even though operator<< is defined inside the class definition, it's not a member function at all -- it's a global function.
P) Copy Constructor is used to create and exact copy of an object with the same values of an existing object.
There are 2 good reasons for using a copy constructor :
a) when you have a complex object with many attributes it is much more simpler to use the copy constructor
b) if you add an attribute to your class, you just change the copy constructor to take this new attribute into account instead of changing every occurence of the other constructor.
Q) structure is another user defined data type , that allows to combine data items of different kinds.
If you don't specify public or private, members of a struct are public by default.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.