1. (TCO 1) Given a two-dimensional array that has three rows and four columns of
ID: 3558618 • Letter: 1
Question
1. (TCO 1) Given a two-dimensional array that has three rows and four columns of type int, write the single code statement that will both declare and initialize the array such that the first row is initialized to 0, 1, 2, 3; the second row is initialized to 4, 5, 6, 7; and the third row is initialized to 8, 9, 10, 11. (Points : 8)
Question 2.2. (TCO 2) Explain the difference between a parameterized constructor and a default constructor. Provide an example class that includes both types of constructors. (Points : 8)
Question 3.3. (TCO 4) If a class is derived protected from a base class, explain how this affects the inheritance of all the public, protected, and private members of the base class by the derived class. (Points : 8)
Question 4.4. (TCO 7) Discuss run-time binding and how it relates to virtual functions. (Points : 8)
Question 5.5. (TCO 8) Why are comments and comment blocks important to programming? (Points : 8)
1. (TCO 1) Given a two-dimensional array that has three rows and four columns of type int, write the single code statement that will both declare and initialize the array such that the first row is initialized to 0, 1, 2, 3; the second row is initialized to 4, 5, 6, 7; and the third row is initialized to 8, 9, 10, 11. (Points : 8)
Explanation / Answer
1) int i,j,k;
k=0;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
k++;
array[i][j]=k;
}
}
2) A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.
class Example{
public:
Example(){ } // -> default constructor
int Example(int a,int b){} // -> user defined constructor
}
3) When you derive a class, the derived class inherits class members of the base class. You can refer to inherited members (base class members) as if they were members of the derived class
1) In a public base class, public and protected members of the base class remain public and protected members of the derived class.
2) In a private base class, public and protected members of the base class become private members of the derived class.
3) In a protected base class, public and protected members of the base class are protected members of the derived class
4) The simplest form of run-time binding is polymorphism. In context of C++ polymorphism is achieved through virtual functions. The basic purpose of this is to call methods on instances of derived classes through a pointer or a reference to a base class.
5) These are important because it makes the code easier to read for other programmers because it can provide details on a particular implementation explain an algorithm etc. Also, when the code is reviewed later, with good comments its easier to debug and test the code.