ARRAY OF OBJECTS
We know that an array can be of any data type including struct. Similarly we can also have arrays of variables that are of the type class. Such variables are called arrays of objects.
Program 9:
#include<iostream.h>
#include<conio.h>
class employee
{
char name[10];
int no;
public:
void getdata()
{
cout << "enter the no and name of an employee " ;
cin >> no >> name;
}
void showdata()
{
cout << "\n" << no << " name = " << name;
}
}p[3];
void main()
{
clrscr();
int i;
for(i=0;i<3;i++)
{
p[i].getdata();
}
for(i=0;i<3;i++)
{
p[i].showdata();
}
getch();
}
NESTED CLASS
Class can be defined inside the other class; such a class is known as nested class. The class that contains the nested class is known as enclosing class. A nested class can be defined in private or public portion.
Defining a nested class in private section of enclosing class:
class a
{
class b
{
};
};
Defining a nested class in public section of enclosing class:
class a
{
public:
class b
{
};
};
Program 10:
#include<iostream.h>
#include<conio.h>
class a
{
public:
class b
{
int x;
public:
void getdata()
{
cout << "enter the value ";
cin >> x;
}
void display()
{
cout << "value = " << x;
}
};
};
void main()
{
a::b j;
j.getdata();
j.display();
getch();
}
Compiled By: Chaudhary Amit V.
Comments
Post a Comment