answersLogoWhite

0


Best Answer

A nested structure is simply one structure inside another. The inner structure is local to the enclosing structure.

struct A { struct B {}; };

Here, we can instantiate an instance of A as normal.

A a;

But to instantiate B we must qualify the type because it is local to A:

A::B b;

If B were only required by A, then we can prevent users from instantiating instances of B simply by declaring it private:

struct A { private: struct B {}; };

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

A nested class is a class that is declared local to another class, the enclosing class.

Example:

class A // enclosing class

{

public:

class B {}; // public nested class

};

To access the nested class from outside of the A class, you would use the scope resolution operator, A::B.

A typical scenario where you would use a nested class is where the nested class is only of relevance to its enclosing class, and you do not wish instances of that class to be created from outside of the enclosing class. This is achieved by declaring the nested class private to the enclosing class:

class A // enclosing class

{

private:

class B {}; // private nested class

};

Since the enclosing class is also a namespace, you may also nest classes within explicit namespaces:

namespace foo

{

class A {}; // nested class

};

This allows two or more namespaces to use the same class name for entirely different purposes. For instance, the class name "cuboid" could mean the 3-dimensional shape or it could mean a 3-dimensional array. If you ever needed to use both definitions within the same program, then you can either rename one of them to remove the ambiguity, or you can simply enclose them within separate namespaces, such as shape::cube and multi_dimensional::cube.

If you wanted to use both definitions within the same program, then you must enclose at least one of these definitions in a separate namespace, such as shape::cube. Thus shape::cube is treated as being a separate entity to the cube type in the global namespace. Ideally, both should be in separate namespaces, such as multi-dimensional::cube, thus

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A nested structure in any language is a structure within a structure.

class A {

class B {}; // nested structure

};

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is nested class and what is its use in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is nested class in c plus plus?

s.


Can you use c in c plus plus without using class and object?

Sure.


How do you calculate the size of a class in memory in C plus plus?

Use sizeof( ).


What do you understand by a Nested Class?

A nested class is a class that is declared within the scope of another class, the enclosing or outer class. class A // enclosing class { public: class B {}; // nested class }; Note that although class B is declared public in this example, keep in mind that it is still scoped to class A. Therefore the following code will fail to compile: int main() { B b; // Compiler error! } Instead, you must use scope resolution: int main() { A::B b; // OK } If class B were declared private to class A, then only class A and friends of class A have access to class A::B. If declared protected, accessibility extends to derivatives of class A. Nested classes may also inherit from other nested classes within the same enclosing class. class A { class B {}; class C : public class B {}; }; Keep in mind that nested classes are scoped to the enclosing class and are primarily used to reduce the visibility of the nested class, particularly when combined with private or protected access. When you have a class that is only of relevance to the enclosing class, this makes perfect sense. However, if the nested class is declared public, you have the option of using an embedded object or a nested class. But the point of scope resolution is in order to reduce the chances of programmer errors, particularly when using two or more classes with the same name but with different implementations.


What is an instance class in C plus plus?

You have a class(i.g. MyClass): class MyClass{ public: int MyData; }; And then you use the class like this: int main(){ MyClass MyObject; MyObject.MyData=7; }


Can a c plus plus class be derived from a Java class?

No.


What is the difference between n-- and --n in C plus plus statement?

If these expressions are stand-alone (not nested), then they do the same thing, ie increment 'n'.


What is the compound in c language?

compound c language is complicated where we need to use many nested functions and loops


What is the role of object in c plus plus?

An object in C++ is an instance of a C++ class.


Can include files be nested in c?

Yes, include files can be nested in C and C++. In fact, most library implementations do just that.


How do you do nested strcutres program in c plus plus?

As its name suggests, a nested structure is a structure which contains another within it. Here is an example in which the "nApple" structure is nested withing the "nTree" structure: #includestruct nApple{int stem;int skin;};struct nTree{int leaves;nApple redDelicious;nApple grannySmith;};


Explain instantiation of objects in c plus plus?

Instantiation of a class literally means creating an instance of a class. This is the process of allocating memory for an object that you can use in your program.