C++ answering question problems.

Q1: Explain why the two calls to function g produces different results in each case:

#include
using namespace std;
class X {
int x;
public:
X() { x = 10; }
void f() { cout << "f called " << x << endl; } }; class Y : public X { int y; public: Y() { y = 20; } void g() { cout << "g called" << y << endl; } }; int main() { X* a = new X; Y* b = new Y; ((Y*)a)->g();
a = b;
((Y*)a)->g();
}

Q2:

Given the following two classes, Point and Circle:
class Point {

public:

void name() {

cout << "Point" << endl; } }; class Circle: public Point { public: void name() { cout << "Circle" << endl; } }; Which polymorphic treatment can you apply to the above code to make the following code snippet print "Circle"? Point* p = new Circle; p->name();

Q3:

Assuming polymorphism has been applied to the previous Point and Circle class hierarchy, what does the following statement print? And why?
Point p = Circle();

p.name();

Q4:

Given the following:
ClassA a(“C++”);

ClassB b(a);

ClassB *pB = &a;

the invocation

pB -> eval();

invokes the ClassA instance of eval(), where as

b.eval();

invokes the ClassB instance.

Why is that?

Q5:

Given the following class declaration for Pasadena with its set of class members, in which pstrC addresses a dynamic character array:
class Pasadena {

public:

~Pasadena();

private:

char* ptrC;

int iVal;

float fVla;

};

Implement the appropriate destructor outside of the Pasadena class.

For the following 7 questions you are asked to identify instance(s) of ClassA at run time in the following code snippet:
ClassA global;

ClassA func(ClassA param) {

ClassA local = param;

ClassA *heap = new ClassA(global);

*heap = global;

ClassA arr[4] = { local, *heap };

return *heap;

}

Q6-12 CHOOSE TRUE/FALSE

6.ClassA global; // this creates an instance of ClassA

7.ClassA func(ClassA param) {…} // this creates an instance of ClassA

8.ClassA local = param; // this creates an instance of ClassA

9.*heap = new ClassA(global); // this creates an instance of ClassA

10.*heap = global; // this creates an instance of ClassA

11.ClassA arr[4] = { local, *heap }; // this creates instance(s) of ClassA

12.return *heap; // this creates an instance of ClassA

Q13:

In your previous lab assignment and quiz you were given the following class Matrix and implemented its operator[ ] method that makes the following statements possible:
Matrix m;

cout << m[0]; // prints 0 cout << m[1]; // prints 1 ... cout << m[9]; // prints 9 Suppose that you now have this Matrix class with a dynamically allocated 2D array: class Matrix { public: Matrix() { _2DArray = newint*[10]; for (inti = 0; i < 10; ++i) { _2DArray[i] = newint[10]; for (intj = 0; j < 10; ++j) _2DArray[i][j] = j; } } ~Matrix() { for (inti = 0; i < 10; ++i) { delete[] _2DArray[i]; } delete[] _2DArray; } private: int** _2DArray; }; Write (implement) an overloaded operator function [ ][ ] (you may have to think creatively for this) such that the following is possible. Note calling m[row][col] is the same as if _2DArray[row][col] is called: Matrix m; cout << m[0][0]; // prints 0 cout << m[0][1]; // prints 1 ... cout << m[0][9]; // prints 9 ... cout << m[9][0]; // prints 0 ... cout << m[9][9]; // prints 9 HINTS: Generally speaking to provide multidimensional array access namely to implement a 3D array access a[i][j][k] = x, operator[] has to return a reference to a 2D plane, which has to have its own operator[] which returns a reference to a 1D row, which has to have operator[] that returns a reference to the element. You get the picture. This question asks for implementation of a 2D array access namely a[i][j] = x, involving slightly less complicated logic. Q14: What is wrong with the following code snippet? Please explain the mistake if any and its correction. int size; cin >> size;

int* arrs = new int[size];

for (int i=0; i> arrs[i];

}

delete arrs;

Q15:

What is wrong with the following code snippet from the logical standpoint (not syntax standpoint)?
class Point {

private:

int x, y;

public:

Point(int u, int v) : x(u), y(v) {}

int getX() { return x; }

int getY() { return y; }

};

int main() {

Point* p = new p(5, 3);

std::cout << p->x << " " << p->y;

return 0;

}

Q16:

Implement a single constructor for class Point that, if called with 0 argument, initializes an instance of Point to the origin, or (0, 0). If the constructor is called with two arguments x and y, it creates a Point instance located at (x, y).

Hint: You will need to use default arguments

Q17:

Given a class Point, what would happen if its constructor were private?

Q18:
What is wrong with the following header file from syntax standpoint? Please explain the mistake if any and its correction.

#ifdef _CPPLIST_APPLY_H#define _CPPLIST_APPLY_H
class ApplyFunction {protected:
virtual int function( int x ) const = 0;public:
void apply() const;
virtual ~ApplyFunction() {}
};
#endif //_CPPLIST_APPLY_H

Order now

C++ answering question problems

Q1:
Explain why the two calls to function g produces different results in each case:

#include
using namespace std;
class X {
int x;
public:
X() { x = 10; }
void f() { cout << "f called " << x << endl; } }; class Y : public X { int y; public: Y() { y = 20; } void g() { cout << "g called" << y << endl; } }; int main() { X* a = new X; Y* b = new Y; ((Y*)a)->g();
a = b;
((Y*)a)->g();
}

Q2:

Given the following two classes, Point and Circle:
class Point {

public:

void name() {

cout << "Point" << endl; } }; class Circle: public Point { public: void name() { cout << "Circle" << endl; } }; Which polymorphic treatment can you apply to the above code to make the following code snippet print "Circle"? Point* p = new Circle; p->name();

Q3:

Assuming polymorphism has been applied to the previous Point and Circle class hierarchy, what does the following statement print? And why?
Point p = Circle();

p.name();

Q4:

Given the following:
ClassA a(“C++”);

ClassB b(a);

ClassB *pB = &a;

the invocation

pB -> eval();

invokes the ClassA instance of eval(), where as

b.eval();

invokes the ClassB instance.

Why is that?

Q5:

Given the following class declaration for Pasadena with its set of class members, in which pstrC addresses a dynamic character array:
class Pasadena {

public:

~Pasadena();

private:

char* ptrC;

int iVal;

float fVla;

};

Implement the appropriate destructor outside of the Pasadena class.

For the following 7 questions you are asked to identify instance(s) of ClassA at run time in the following code snippet:
ClassA global;

ClassA func(ClassA param) {

ClassA local = param;

ClassA *heap = new ClassA(global);

*heap = global;

ClassA arr[4] = { local, *heap };

return *heap;

}

Q6-12 CHOOSE TRUE/FALSE

6.ClassA global; // this creates an instance of ClassA

7.ClassA func(ClassA param) {…} // this creates an instance of ClassA

8.ClassA local = param; // this creates an instance of ClassA

9.*heap = new ClassA(global); // this creates an instance of ClassA

10.*heap = global; // this creates an instance of ClassA

11.ClassA arr[4] = { local, *heap }; // this creates instance(s) of ClassA

12.return *heap; // this creates an instance of ClassA

Q13:

In your previous lab assignment and quiz you were given the following class Matrix and implemented its operator[ ] method that makes the following statements possible:
Matrix m;

cout << m[0]; // prints 0 cout << m[1]; // prints 1 ... cout << m[9]; // prints 9 Suppose that you now have this Matrix class with a dynamically allocated 2D array: class Matrix { public: Matrix() { _2DArray = newint*[10]; for (inti = 0; i < 10; ++i) { _2DArray[i] = newint[10]; for (intj = 0; j < 10; ++j) _2DArray[i][j] = j; } } ~Matrix() { for (inti = 0; i < 10; ++i) { delete[] _2DArray[i]; } delete[] _2DArray; } private: int** _2DArray; }; Write (implement) an overloaded operator function [ ][ ] (you may have to think creatively for this) such that the following is possible. Note calling m[row][col] is the same as if _2DArray[row][col] is called: Matrix m; cout << m[0][0]; // prints 0 cout << m[0][1]; // prints 1 ... cout << m[0][9]; // prints 9 ... cout << m[9][0]; // prints 0 ... cout << m[9][9]; // prints 9 HINTS: Generally speaking to provide multidimensional array access namely to implement a 3D array access a[i][j][k] = x, operator[] has to return a reference to a 2D plane, which has to have its own operator[] which returns a reference to a 1D row, which has to have operator[] that returns a reference to the element. You get the picture. This question asks for implementation of a 2D array access namely a[i][j] = x, involving slightly less complicated logic. Q14: What is wrong with the following code snippet? Please explain the mistake if any and its correction. int size; cin >> size;

int* arrs = new int[size];

for (int i=0; i> arrs[i];

}

delete arrs;

Q15:

What is wrong with the following code snippet from the logical standpoint (not syntax standpoint)?
class Point {

private:

int x, y;

public:

Point(int u, int v) : x(u), y(v) {}

int getX() { return x; }

int getY() { return y; }

};

int main() {

Point* p = new p(5, 3);

std::cout << p->x << " " << p->y;

return 0;

}

Q16:

Implement a single constructor for class Point that, if called with 0 argument, initializes an instance of Point to the origin, or (0, 0). If the constructor is called with two arguments x and y, it creates a Point instance located at (x, y).

Hint: You will need to use default arguments

Q17:

Given a class Point, what would happen if its constructor were private?

Q18:
What is wrong with the following header file from syntax standpoint? Please explain the mistake if any and its correction.

#ifdef _CPPLIST_APPLY_H#define _CPPLIST_APPLY_H
class ApplyFunction {protected:
virtual int function( int x ) const = 0;public:
void apply() const;
virtual ~ApplyFunction() {}
};
#endif //_CPPLIST_APPLY_H

Order now