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.
8 1386 Banfa 9,065
Expert Mod 8TB
Something like this - class Outer
-
{
-
private:
-
class Inner
-
{
-
private:
-
int data;
-
-
public:
-
int GetValue() const { return data; }
-
void SetValue(int newValue){ data = newValue; }
-
Inner(int init = 0) : data(init) {}
-
};
-
-
Inner *pInner;
-
-
public:
-
Outer(int init = 0) : pInner(new Inner(init)) {}
-
~Outer(){ delete pInner; }
-
-
int GetValue() const { return pInner->GetValue(); }
-
void SetValue(int newValue){ pInner->SetValue(newValue); }
-
};
Although this is a slightly converluted way to store an int :D
- class yourFirstClass {
-
private:
-
class yourSecondClass {
-
// yourSecondClass definition here.
-
}
-
-
// yourFirstClass definition here
-
}
Follow this basic pattern, and you should be OK.
- class yourFirstClass {
-
private:
-
class yourSecondClass {
-
// yourSecondClass definition here.
-
}
-
-
// yourFirstClass definition here
-
}
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?
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?
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 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 - #include <cstdlib>
-
#include <iostream>
-
-
using namespace std;
-
class Food
-
{
-
private:
-
class vegetable
-
{
-
private:
-
string vegname;
-
int calories;
-
public:
-
int GetCalories() const { return calories; }
-
void SetCalories(int newValue){ calories = newValue; }
-
vegetable(int init = 0) : calories(init) {}
-
-
};
-
class fruit
-
{
-
private:
-
string fruitname;
-
int calories;
-
public:
-
int GetCalories() const { return calories; }
-
void SetCalories(int newValue){ calories = newValue; }
-
fruit(int inita = 1) : calories(inita) {}
-
};
-
vegetable *newveg;
-
fruit *newfruit;
-
public:
-
Food(int init = 0) : newveg(new vegetable(init)) {}
-
~Food(){ delete newveg; }
-
int GetCaloriesveg() const { return newveg->GetCalories(); }
-
void SetCaloriesveg(int newValue){ newveg->SetCalories(newValue); }
-
};
-
-
-
int main(int argc, char *argv[])
-
{
-
Food(carrot);
-
carrot.SetCaloriesveg(50);
-
cout<<carrot.GetCaloriesveg()<<"\n";
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
}
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.
Banfa 9,065
Expert Mod 8TB -
Food(int veginit = 0, int fruitinit = 0)
-
: newveg(new vegetable(veginit)),
-
newfruit(new fruit(fruitinit))
-
{
-
}
-
-
~Food()
-
{
-
delete newveg;
-
delete newfruit;
-
}
-
-
Food(int veginit = 0, int fruitinit = 0)
-
: newveg(new vegetable(veginit)),
-
newfruit(new fruit(fruitinit))
-
{
-
}
-
-
~Food()
-
{
-
delete newveg;
-
delete newfruit;
-
}
-
How would I declare that something should be a new vegetable and not a new fruit, and vice versa.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |