NESTED
CLASSES:
Ø A class that contains the definition of another class called nesting class and the class inside the nesting class called nested class.
Ø The nesting class can access the data member and function member of a nested class.
Ø Nested classes can directly use names, type names, names of static members, and enumerators only from the enclosing class. To use names of other class members, you must use pointers, references, or object names.
Ø The nesting of classes enables building of
powerful data structures.
Syntax
class
Nesting_Class
{
Public:
Data
Members of Nesting Class;
class
Nested_Class
{
public:
Data
Member of Nested Class
Function
Members of Nested Class
};
};
void main()
{
Nesting_class::Nested_class
Object;
object.Nested_class
Datamember=value;
object.Nested_class
Functionmember();
}
Example:
#include<iostream.h>
#include<conio.h>
class
fxclg //Nesting
Class
{
public:
class cse //Nested
Class
{
public:
char
name[10],dept[5];
int age;
void
get()
{
cout<<"Enter
Details Name Age Dept:\n";
cin>>name;
cin>>age;
cin>>dept;
cout<<"\n";
}
void
put()
{
cout<<"name\t"<<name<<"age:\t"<<age<<"dept:\t"<<dept;
}
};
};
void
main()
{
clrscr();
fxclg::cse
cs1; //Declaring Object for Nested Class
cs1.get();
cs1.put();
getch();
}
Output:
Enter
Details Name Age Dept:
kumar
24
CSE
name kumar age: 24
dept: CSE
In this
example, the class cse (nested class) is defined within the class fxclg(nesting
class),To declare the objects of a nested class we have to follow the
procedure, fxclg::cse c1; Using this object it access the data and function
member of nested class cse
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.