#include <iostream.> using namespace std; class TwoDPoint { protected: int x; in
ID: 3546316 • Letter: #
Question
#include <iostream.>
using namespace std;
class TwoDPoint
{
protected:
int x;
int y;
public:
TwoDPoint(int x, int y) { this->x = x; this->y = y;}
TwoDPoint() {x =-1; y=-1;}
virtual char *getName() {char *temp = new char[15]; strcpy(temp, "two_d_point"); return(temp);}
int getX() {return x;}
int getY() {return y; }
int calcDist(TwoDPoint p);
};
int TwoDPoint::calcDist(TwoDPoint p)
{
int dist;
dist = sqrt((p.x - x)*(p.x-x) + (p.y-y)*(p.y-y));
return(dist);
}
class ThreeDPoint : public TwoDPoint
{
private:
int z;
public:
ThreeDPoint(int z);
ThreeDPoint() { z = 0;}
int calcDist(ThreeDPoint p);
virtual char *getName() {char *temp = new char[15]; strcpy(temp,"three_d_point"); return(temp);}
int getZ() {return z;}
};
ThreeDPoint::ThreeDPoint(int z) : TwoDPoint(1,1)
{
z = 1;
}
int ThreeDPoint::calcDist(ThreeDPoint p)
{
int dist;
dist = sqrt((p.x - x)*(p.x - x) + (p.y -y)*(p.y-y) +(p.z-z)*(p.z-z));
return dist;
}
int main()
{
ThreeDPoint three_p(1), three_p1;
TwoDPoint two_p(1,1), *two_ptr;
//Line 2
cout << three_p.getX() << " " << three_p.getY() << " " << three_p.getZ() << endl;
//Line 3
cout << three_p1.getX() << " " << three_p1.getY() << " " << three_p1.getZ() << endl;
//Line 4
two_p = three_p;
//Line 5
two_ptr = &three_p1;
//Line 6
cout << two_ptr->getName() << " " << two_p.getName() << " " << two_ptr->getX() << endl;
return 0; }
Answer These Questions
Is line 4 legal (i.e., does it compile)?
Is line 5 legal (i.e., does it compile)?
Is getName a virtual method?
Is getName pure virtual method?
Is getName an overriden method?
Is calcDist an overriden method?
Is ThreeDPoint an overloaded constructor?
Identify an example in above program where base-class constructor is invoked explicitly. (Enter answer below):
What is displayed at line 2?
What is displayed at line 3?
What is displayed at line 6?
Explanation / Answer
1. Yes it will compile
2. yes
3.no
4. yes
5. no
6.yes
7. yes
when three_p1 is declared constructor is invoked explicitly
line 2 output ---> 1 1 0
line 3 output ---> -1 -1 0
line 6 output ---> three_d_point two_d_point -1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.