473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested classes within other classes

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
//***************
Jul 22 '05 #1
2 1570
In terms of functionality there's no difference between example 1 and
2, they differ in the logical design:
From example 1 I read that CSpaceship::XMotion is specific to

CSpaceship. I hardly would use to move
an Asteroid around. This example also implies that XMotion could easily
be friend of CSpaceship if needed so.

In example 2 the designer tells me that XMotion can be used for a whole
group of classes
(tough the criteria are not documented).

regards,
Stephan Brönnimann
br****@osb-systems.com
http://www.osb-systems.com
Open source rating and billing engine for
communication networks.

Jul 22 '05 #2

Tony Johansson wrote:
Hello! 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.
Yes. In addition, there is _also_ one member CSpaceship::m_xMotion
which has type CSpaceship::XMotion
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.
No. XMotion is declared and defined outside CSpaceship. However, the
data member CSpaceship::m_xMotion is still defined inside CSpaceship.

Now to my question what is the difference functional between example 1 and example 2. is it the same thing perhaps?
In example 1, the type CSpaceship::XMotion is a private member of
CSpaceship whereas in example 2, ::XMotion is globally visible.
That means that in example 1, it's a whole lot easier to find all
the users of the type. Private members are used only by the class.

Regards,
Michiel Salters
//START EXAMPLE 1
struct IMotion
{
virtual void Fly() = 0;
};

class CSpaceship
{
class XMotion : public IMotion
{
public:
XMotion(){}
virtual void Fly();
}m_xMotion;

friend class XMotion;
};
//END EXAMPLE 1

//START EXAMPLE 2
struct IMotion
{
virtual void Fly() = 0;
};

class XMotion : public IMotion
{
public:
XMotion(){}
virtual void Fly();
};

class CSpaceship
{
XMotion m_xMotion;
};
//END EXAMPLE 2


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
2
by: Alfonso Morra | last post by:
I have a (largish) "master" header file (>130 loc). I also have a number of seperate "satelite" header files which declare various objects used by the object declared in the "master" header file....
4
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
1
by: Raju Joseph | last post by:
Hi All, We are in the process of developing an N-Tier app using VB.NET. We are extensively using classes (entity objects) in our design. Further, most of the times, we do have to specify nested...
6
by: moondaddy | last post by:
I'm new to c# and am wondering if its possible to access members of a nested class. Can someone please advise? Thanks. class Program { static void Main(string args) { try { Test1 obj =...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.