473,406 Members | 2,705 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,406 software developers and data experts.

Classes' Classes

I am writing a program to familiarize myself better with certain aspects of classes, and was wondering how to properly implement a class inside a class, and make it so only a certain instance of the original class can call a certain instance of the nested class. Basically I'm trying to get an object to have an object on it, and to make the second object exclusive to the first object. I'm confused about how to go about this.
Oct 16 '07 #1
8 1386
Banfa
9,065 Expert Mod 8TB
Something like this

Expand|Select|Wrap|Line Numbers
  1. class Outer
  2. {
  3. private:
  4.     class Inner
  5.     {
  6.     private:
  7.         int data;
  8.  
  9.     public:
  10.         int GetValue() const { return data; }
  11.         void SetValue(int newValue){ data = newValue; }
  12.         Inner(int init = 0) : data(init) {}
  13.     };
  14.  
  15.     Inner *pInner;
  16.  
  17. public:
  18.     Outer(int init = 0) : pInner(new Inner(init)) {}
  19.     ~Outer(){ delete pInner; }
  20.  
  21.     int GetValue() const { return pInner->GetValue(); }
  22.     void SetValue(int newValue){ pInner->SetValue(newValue); }
  23. };
Although this is a slightly converluted way to store an int :D
Oct 16 '07 #2
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. class yourFirstClass {
  2.    private:
  3.       class yourSecondClass {
  4.          // yourSecondClass definition here.
  5.       }
  6.  
  7.    // yourFirstClass definition here
  8. }
Follow this basic pattern, and you should be OK.
Oct 16 '07 #3
Expand|Select|Wrap|Line Numbers
  1. class yourFirstClass {
  2.    private:
  3.       class yourSecondClass {
  4.          // yourSecondClass definition here.
  5.       }
  6.  
  7.    // yourFirstClass definition here
  8. }
Follow this basic pattern, and you should be OK.
I understand the declarations, but I'm confused as to how to implement it in my code, say I want to reference a member of mysecondClass how would I do that?
Oct 17 '07 #4
Banfa
9,065 Expert Mod 8TB
I understand the declarations, but I'm confused as to how to implement it in my code, say I want to reference a member of mysecondClass how would I do that?
Have you read my post?
Oct 17 '07 #5
I have one more problem, how can I have multiple private classes inside my outer class? like Inner1 and Inner2, I do not fully understand how this works.
Oct 17 '07 #6
I have one more problem, how can I have multiple private classes inside my outer class? like Inner1 and Inner2, I do not fully understand how this works.
I'm not sure if I was really clear in my above question, so I wrote some code that maybe illustrates better what I am asking

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. class Food
  6. {
  7.       private:
  8.       class vegetable
  9.       {
  10.             private:
  11.             string vegname;
  12.             int calories;
  13.             public:
  14.             int GetCalories() const { return calories; }
  15.             void SetCalories(int newValue){ calories = newValue; }
  16.             vegetable(int init = 0) : calories(init) {}
  17.  
  18.       };
  19.       class fruit
  20.       {
  21.             private:
  22.             string fruitname;
  23.             int calories;
  24.             public:
  25.             int GetCalories() const { return calories; }
  26.             void SetCalories(int newValue){ calories = newValue; }
  27.             fruit(int inita = 1) : calories(inita) {}
  28.       };
  29.       vegetable *newveg;
  30.       fruit *newfruit;
  31.       public:
  32.       Food(int init = 0) : newveg(new vegetable(init)) {}
  33.       ~Food(){ delete newveg; }
  34.       int GetCaloriesveg() const { return newveg->GetCalories(); }
  35.       void SetCaloriesveg(int newValue){ newveg->SetCalories(newValue); }
  36. };
  37.  
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     Food(carrot);
  42.     carrot.SetCaloriesveg(50);
  43.     cout<<carrot.GetCaloriesveg()<<"\n";
  44.     system("PAUSE");
  45.     return EXIT_SUCCESS;
  46. }
I was wondering how I initialize the second object in this fashion, and similarily create a destructor for it. I'm confused because this is totally different than the way I was attempting to nest classes before.
Oct 17 '07 #7
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Food(int veginit = 0, int fruitinit = 0) 
  2.                         : newveg(new vegetable(veginit)),
  3.                           newfruit(new fruit(fruitinit))
  4. {
  5. }
  6.  
  7. ~Food()
  8. {
  9.     delete newveg; 
  10.     delete newfruit; 
  11. }
  12.  
Oct 17 '07 #8
Expand|Select|Wrap|Line Numbers
  1. Food(int veginit = 0, int fruitinit = 0) 
  2.                         : newveg(new vegetable(veginit)),
  3.                           newfruit(new fruit(fruitinit))
  4. {
  5. }
  6.  
  7. ~Food()
  8. {
  9.     delete newveg; 
  10.     delete newfruit; 
  11. }
  12.  
How would I declare that something should be a new vegetable and not a new fruit, and vice versa.
Oct 17 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
12
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you...
4
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.