Given the following classes, classA and classB: class ClassA{ public: virtual vo
ID: 641542 • Letter: G
Question
Given the following classes, classA and classB:
class ClassA{
public:
virtual void print()
{ cout << "ClassA x: " << x << endl; }
void doubleNum()
{x = 2*x;}
ClassA(int a = 0);
private:
int x;
};
ClassA :: ClassA (int a)
{ x = a;}
class ClassB : public ClassA {
public:
void print()
{
ClassA :: print();
cout << "ClassB y: " << y << endl; }
void doubleNum()
{
ClassA :: doubleNum();
y = 2*y;
}
ClassB(int a = 0, int b = 0);
private:
int y;
};
ClassB :: ClassB (int a, int b)
: ClassA (a)
{ y = b;}
What is the output of the following C++ code?
int main () {
ClassA *ptrA;
ClassA objectA(2);
ClassB objectB(3,5);
ptrA = &objectA;
ptrA -> doubleNum();
ptrA -> print();
cout << endl;
ptrA = &objectB;
ptrA -> doubleNum();
ptrA -> print();
cout << endl;
return 0;
}
Now, describe what is happening in Lines 1 through 9:
int main () {
ClassA *ptrA; //Line 1:_______________________________________
ClassA objectA(2); //Line 2:_______________________________________
ClassB objectB(3,5); //Line 3:_______________________________________
ptrA = &objectA; //Line 4:_______________________________________
ptrA -> doubleNum(); //Line 5:_______________________________________
ptrA -> print(); //Line 6:_______________________________________
cout << endl;
ptrA = &objectB; //Line 7:_______________________________________
ptrA -> doubleNum(); //Line 8:_______________________________________
ptrA -> print(); //Line 9:_______________________________________
cout << endl;
return 0;
}
What is the output of the function main of problem #1, if the definition of ClassA is replaced by the following definition?
class ClassA{
public:
virtual void print()
{ cout << "ClassA x: " << x << endl; }
virtual void doubleNum()
{x = 2*x;}
ClassA(int a = 0);
private:
int x;
};
Now, describe what ended up happening in Lines 1 through 9:
int main () {
ClassA *ptrA; //Line 1:_______________________________________
ClassA objectA(2); //Line 2:_______________________________________
ClassB objectB(3,5); //Line 3:_______________________________________
ptrA = &objectA; //Line 4:_______________________________________
ptrA -> doubleNum(); //Line 5:_______________________________________
ptrA -> print(); //Line 6:_______________________________________
cout << endl;
ptrA = &objectB; //Line 7:_______________________________________
ptrA -> doubleNum(); //Line 8:_______________________________________
ptrA -> print(); //Line 9:_______________________________________
cout << endl;
return 0;
}
What is the difference between compile-time binding and run-time binding?
Is it legal to have an abstract class with all member functions pure virtual?
Consider the definition of the following function template:
template <class Type>
Type surprise (Type x, Type y)
{
return x + y;
}
What is the output of the following statements?
cout << surprise(5,7) << endl;
string str1 =
Explanation / Answer
What is the output of the following C++ code?
int main () {
ClassA *ptrA;
ClassA objectA(2);
ClassB objectB(3,5);
ptrA = &objectA;
ptrA -> doubleNum();
ptrA -> print();
cout << endl;
ptrA = &objectB;
ptrA -> doubleNum();
ptrA -> print();
cout << endl;
return 0;
}
Now, describe what is happening in Lines 1 through 9:
int main () {
ClassA *ptrA; //Line 1:declaring class *ptrA
ClassA objectA(2); //Line 2:initializing calss object value 2 but...it declares 0 since int a=0
ClassB objectB(3,5); //Line 3:initializing classB values
ptrA = &objectA; //Line 4:storing address of objectA to ptrA class
ptrA -> doubleNum(); //Line 5: it class function makes number double
ptrA -> print(); //Line 6:prints class ptrA and displays the value of x
cout << endl;
ptrA = &objectB; //Line 7: ObjectB class address is stored ti ptrA
ptrA -> doubleNum(); //Line 8:number will be doubled
ptrA -> print(); //Line 9:prints class name and prints x value
cout << endl;
return 0;
}
it prints values classA x value 4 and classB output 6*5 = 30
Dynamic Binding :
The address of the functions are determined at runtime rather than @ compile time. This is also known as "Late Binding".
Static Binding :
The address of the functions are determined at compile time rather than @ run time. This is also known as "Early Binding"
Is it legal to have an abstract class with all member functions pure virtual? no
Consider the definition of the following function template:
template <class Type>
Type surprise (Type x, Type y)
{
return x + y;
}
What is the output of the following statements?
cout << surprise(5,7) << endl;//prints 7
string str1 =
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.