Write a program to demonstrate the use of 'Composition' in C++
Answer Posted / jojo
#ifndef POINT2D_H
#define POINT2D_H
#include <iostream>
class Point2D
{
private:
int m_nX;
int m_nY;
public:
// A default constructor
Point2D()
: m_nX(0), m_nY(0)
{
}
// A specific constructor
Point2D(int nX, int nY)
: m_nX(nX), m_nY(nY)
{
}
// An overloaded output operator
friend std::ostream& operator<<(std::ostream& out, const
Point2D &cPoint)
{
out << "(" << cPoint.GetX() << ", " << cPoint.GetY()
<< ")";
return out;
}
// Access functions
void SetPoint(int nX, int nY)
{
m_nX = nX;
m_nY = nY;
}
int GetX() const { return m_nX; }
int GetY() const { return m_nY; }
};
#endif
| Is This Answer Correct ? | 1 Yes | 3 No |
Post New Answer View All Answers
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
Write a java applet that computes and displays the squares of values between 25 and 1 inclusive and displays them in a TextArea box
What is protected in oop?
What is the problem with multiple inheritance?
c++ program to swap the objects of two different classes
What are different oops concepts?
What is encapsulation c#?
What is object-oriented programming? Webopedia definition
What is difference between multiple inheritance and multilevel inheritance?
What is polymorphism explain its types?
What is polymorphism oop?
What is the highest level of cohesion?
What is purpose of inheritance?
How Do you Code Composition and Aggregation in C++ ?
what are the realtime excercises in C++?