24
NESTING Classes can be defined inside other classes. Classes that are defined inside other classes are called nested classes. Nested classes are used in situations where the nested class has a close conceptual relationship to its surrounding class. For example, with the class string a type string::iterator is available which will provide all elements (characters) that are stored in the string. This string::iterator type could be defined as an object iterator, defined as nested class in the class string. A class can be nested in every part of the surrounding class: in the public, protected or private section. Such a nested class can be considered a member of the surrounding class. The normal access and rules in classes apply to nested classes. If a class is nested in the public section of a class, it is visible outside the surrounding class. If it is nested in the protected section it is visible in subclasses, derived from the surrounding class (see chapter 13 ), if it is nested in the private section, it is only visible for the members of the surrounding class. The surrounding class has no special privileges with respect to the nested class. So, the nested class still has full control over the accessibility of its members by the surrounding class. For example, consider the following class definition: class Surround { public: class FirstWithin { int d_variable; public: FirstWithin(); int var() const { return d_variable; } }; private: class SecondWithin

less 5 (1) (1)

Embed Size (px)

Citation preview

NESTINGClasses can be defined inside other classes. Classes that are defined inside other classes are callednested classes. Nested classes are used in situations where the nested class has a close conceptual relationship to its surrounding class. For example, with the classstringa typestring::iteratoris available which will provide all elements (characters) that are stored in thestring. Thisstring::iteratortype could be defined as an objectiterator, defined as nested class in the classstring.A class can be nested in every part of the surrounding class: in thepublic, protectedorprivatesection. Such a nested class can be considered a memberof the surrounding class. The normal access and rules in classes apply to nested classes. If a class is nested in thepublicsection of a class, it isvisible outside the surrounding class. If it is nested in theprotectedsection it is visible in subclasses, derived from the surrounding class (see chapter13), if it is nested in theprivatesection, it is only visible for the members of the surrounding class.The surrounding class has no special privileges with respect to the nested class. So, the nested class still has full control over the accessibility of its members by the surrounding class. For example, consider the following class definition: class Surround { public: class FirstWithin { int d_variable;

public: FirstWithin(); int var() const { return d_variable; } }; private: class SecondWithin { int d_variable; public: SecondWithin(); int var() const { return d_variable; } }; };In this definition access to the members is defined as follows: The classFirstWithinis visible both outside and insideSurround. The classFirstWithintherefore has global scope. The constructorFirstWithin()and the member functionvar()of the classFirstWithinare also globally visible. Theint d_variabledatamember is only visible to the members of the classFirstWithin. Neither the members ofSurroundnor the members ofSecondWithincan accessd_variableof the classFirstWithindirectly. The classSecondWithinis only visible insideSurround. The public members of the classSecondWithincan also be used by the members of the classFirstWithin, as nested classes can be considered members of their surrounding class. The constructorSecondWithin()and the member functionvar()of the classSecondWithincan also only be reached by the members ofSurround(and by the members of its nested classes). Theint d_variabledatamember of the classSecondWithinis only visible to the members of the classSecondWithin. Neither the members ofSurroundnor the members ofFirstWithincan accessd_variableof the classSecondWithindirectly. As always, an object of the class type is required before its members can be called. This also holds true for nested classes.If the surrounding class should have access rights to the private members of its nested classes or if nested classes should have access rights to the private members of the surrounding class, the classes can be defined asfriendclasses.The nested classes can be considered members of the surrounding class, but themembers of nested classes arenotmembers of the surrounding class. So, a member of the classSurroundmay not accessFirstWithin::var()directly. This is understandable considering the fact that aSurroundobject is not also aFirstWithinorSecondWithinobject. In fact, nested classes are just typenames. It is not implied that objects of such classes automatically exist in the surrounding class. If a member of the surrounding class should use a (non-static) member of a nested class then the surrounding class must define a nested class object, which can thereupon be used by the members of the surrounding class to use members of the nested class.For example, in the following class definition there is a surrounding classOuterand a nested classInner. The classOutercontains a member functioncaller()which uses theinnerobject that is composed inOuterto call theinfunction()member function ofInner: class Outer { public: void caller() { d_inner.infunction(); } private: class Inner { public: void infunction(); }; Inner d_inner; // class Inner must be known };The mentioned functionInner::infunction()can be called as part of the inline definition ofOuter::caller(), even though the definition of the classInneris yet to be seen by the compiler. On the other hand, the compiler must have seen the definition of the classInnerbefore a data member of that class can be defined.Defining nested class membersMember functions of nested classes may be defined as inline functions. Inlinemember functions can be defined as if they were functions defined outside of the class definition: if the functionOuter::caller()would have been defined outside of the classOuter, the full class definition (including the definition of the classInner) would have been available to the compiler. In that situation the function is perfectly compilable. Inline functions can be compiled accordingly: they can be defined and they can use any nested class. Even if it appears later in the class interface.Member functions of nested classes can also be defined outside of their surrounding class. Consider the constructor of the classFirstWithinin the example of the previous section. The constructorFirstWithin()is defined in the classFirstWithin, which is, in turn, defined within the classSurround. Consequently, the class scopes of the two classes must be used to define the constructor. E.g., Surround::FirstWithin::FirstWithin() { variable = 0; }Declaring nested classesNested classesmay be declared before they are actually defined in a surrounding class. Suchforward declarations are required if a class contains multiple nested classes, and the nested classes contain pointers, references, parameters or return values to objects of the other nested classes.For example, the following classOutercontains two nested classesInner1andInner2. The classInner1contains a pointer toInner2objects, andInner2contains a pointer toInner1objects. Such cross references require forward declarations. These forward declarations must be specified in the same access-category as their actual definitions. In the following example theInner2forward declaration must be given in aprivatesection, as its definition is also part of the classOuter's private interface:ARRAY OF STRUCTURESAs discussed in the previous post, there are two types of data structures available to C/C++ programmers. One is already built into the programming language and other one is a bit complex in a sense that it can be implemented using the built in data structures and data types. In C/C++ programming language, built in data structures include Arrays, structures, unions and classes. Some of the examples of complex data structures are Stack, Queue, Linked List,Treeand Graph.The aim of this first tutorials is to teach you how to declare, initialise and use simple arrays as well as multidimensional arrays. You will also be able to use arrays as data structure in your C/C++ program. So at the end of this tutorial you will be able to answer: What is an array and how you can use it? How to declare and initialise simple arrays? How to declare and initialise multidimensional arrays? How to perform simple operations on arrays?What is an array?Array is a very basic data structure provided by every programming language. Lets talk about an example scenario where we need to store ten employees data in our C/C++ program including name, age and salary. One of the solutions is to declare ten different variables to store employee name and ten more to store age and so on. Also you will need some sort of mechanism to get information about an employee, search employee records and sort them. To solve these types of problem C/C++ provide a mechanism called Arrays.DefinitionAn array is simply a number of memory locations, each of which can store an item of data of the same data type and which are all referenced through the same variable name.Ivor Horton.Array may be defined abstractly as finite order set of homogeneous elements. So we can say that there are finite numbers of elements in an array and all the elements are of same data type. Also array elements are ordered i.e. we can access a specific array element by an index.How to declare an array?The general form of declaring a simple (one dimensional) array isarray_type variable_name[array_size];in your C/C++ program you can declare an array likeint Age[10];Here array_type declares base type of array which is the type of each element in array. In our example array_type is int and its name is Age. Size of the array is defined by array_size i.e. 10. We can access array elements by index, and first item in array is at index 0. First element of array is called lower bound and its always 0. Highest element in array is called upper bound.In C programming language upper and lower bounds cannot be changed during the execution of the program, so array length can be set only when the program in written.Age 0Age 1Age 2Age 3Age 4Age 5Age 6Age 7Age 8Age 9

3032543226292343345

Array has 10 elementsNote: One good practice is to declare array length as a constant identifier. This will minimise the required work to change the array size during program development.Considering the array we declared above we can declare it like #define NUM_EMPLOYEE 10 int Age[NUM_EMPLOYEE];How to initialise an array?Initialisation of array is very simple in c programming. There are two ways you can initialise arrays. Declare and initialise array in one statement. Declare and initialise array separately.Look at the following C code which demonstrates the declaration and initialisation of an array. int Age [5] = {30, 22, 33, 44, 25}; int Age [5]; Age [0]=30; Age [1]=22; Age [2]=33; Age [3]=44; Age [4]=25;Array can also be initialised in a ways that array size is omitted, in such case compiler automatically allocates memory to array. int Age [] = {30, 22, 33, 44, 25};Lets write a simple program that uses arrays to print out number of employees having salary more than 3000.Array in C Programming#include #include #include #define NUM_EMPLOYEE 10

int main(int argc, char *argv[]){int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0;printf("Enter employee salary (Max 10)\n ");for (i=0; i