T
Tony Johansson
Hello!!
All my exemples is from a book so don't bother complaining that the struct
should have been a class instead.
Below I have two example it says START EXAMPLE 1 and futher down you have
END EXAMPLE 1 and after END EXAMPLE 1 you find START EXAMPLE 2 and futher
down you have END EXAMPLE 2.
In example 1 you have an abstract base class defined as a struct called
IMotion and
a class definition called CSpaceship with the class XMotion class nested
inside this CSpaceship class.
In example 2 you still have an abstract base class defined as a struct
called IMotion and the class XMotion is now not nested but it's defined
lika a normal data member inside class CSpaceship. The class definition
XMotion is now separete.
Now to my question what is the difference functional between example 1 and
example 2. is it the same thing perhaps?
//Tony
//START EXAMPLE 1
//*****************
struct IMotion
{
virtual void Fly() = 0;
virtual int& GetPosition() = 0;
};
class CSpaceship
{
protected:
int m_nPosition;
int m_nAcceleration;
int m_nColor;
public:
CSpaceship()
{m_nPosition = m_nAcceleration = m_nColor = 0;}
class XMotion : public IMotion
{
public:
XMotion(){}
virtual void Fly();
virtual int& GetPosition();
}m_xMotion;
friend class XMotion;
};
//END EXAMPLE 1
//***************
//START EXAMPLE 2
//*****************
struct IMotion
{
virtual void Fly() = 0;
virtual int& GetPosition() = 0;
};
class XMotion : public IMotion
{
public:
XMotion(){}
virtual void Fly();
virtual int& GetPosition();
};
class CSpaceship
{
protected:
int m_nPosition;
int m_nAcceleration;
int m_nColor;
public:
CSpaceship()
{m_nPosition = m_nAcceleration = m_nColor = 0;}
XMotion m_xMotion;
};
//END EXAMPLE 2
//***************
All my exemples is from a book so don't bother complaining that the struct
should have been a class instead.
Below I have two example it says START EXAMPLE 1 and futher down you have
END EXAMPLE 1 and after END EXAMPLE 1 you find START EXAMPLE 2 and futher
down you have END EXAMPLE 2.
In example 1 you have an abstract base class defined as a struct called
IMotion and
a class definition called CSpaceship with the class XMotion class nested
inside this CSpaceship class.
In example 2 you still have an abstract base class defined as a struct
called IMotion and the class XMotion is now not nested but it's defined
lika a normal data member inside class CSpaceship. The class definition
XMotion is now separete.
Now to my question what is the difference functional between example 1 and
example 2. is it the same thing perhaps?
//Tony
//START EXAMPLE 1
//*****************
struct IMotion
{
virtual void Fly() = 0;
virtual int& GetPosition() = 0;
};
class CSpaceship
{
protected:
int m_nPosition;
int m_nAcceleration;
int m_nColor;
public:
CSpaceship()
{m_nPosition = m_nAcceleration = m_nColor = 0;}
class XMotion : public IMotion
{
public:
XMotion(){}
virtual void Fly();
virtual int& GetPosition();
}m_xMotion;
friend class XMotion;
};
//END EXAMPLE 1
//***************
//START EXAMPLE 2
//*****************
struct IMotion
{
virtual void Fly() = 0;
virtual int& GetPosition() = 0;
};
class XMotion : public IMotion
{
public:
XMotion(){}
virtual void Fly();
virtual int& GetPosition();
};
class CSpaceship
{
protected:
int m_nPosition;
int m_nAcceleration;
int m_nColor;
public:
CSpaceship()
{m_nPosition = m_nAcceleration = m_nColor = 0;}
XMotion m_xMotion;
};
//END EXAMPLE 2
//***************