Академический Документы
Профессиональный Документы
Культура Документы
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
{
int d_variable;
public:
SecondWithin();
int var() const
{
return d_variable;
}
};
};
In this definition access to the members is defined as follows:
The class FirstWithin is visible both outside and inside Surround. The class FirstWithin therefore has
global scope.
The constructor FirstWithin() and the member function var() of the class FirstWithin are also
globally visible.
The int d_variable datamember is only visible to the members of the class FirstWithin. Neither the
members of Surround nor the members of SecondWithin can access d_variable of the
classFirstWithin directly.
The class SecondWithin is only visible inside Surround. The public members of the
class SecondWithin can also be used by the members of the class FirstWithin, as nested classes can
be considered members of their surrounding class.
The constructor SecondWithin() and the member function var() of the class SecondWithin can also
only be reached by the members of Surround (and by the members of its nested classes).
The int d_variable datamember of the class SecondWithin is only visible to the members of the
class SecondWithin. Neither the members of Surround nor the members of FirstWithin can
accessd_variable of the class SecondWithin directly.
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
as friend classes.
The nested classes can be considered members of the surrounding class, but the members of nested classes
are not members of the surrounding class. So, a member of the class Surround may not
accessFirstWithin::var() directly. This is understandable considering the fact that a Surround object is not
also a FirstWithin or SecondWithin object. 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 class Outer and a nested class Inner.
The class Outer contains a member function caller() which uses the inner object that is composed inOuter to
call the infunction() member function of Inner:
class Outer
{
public:
void caller()
{
d_inner.infunction();
}
private:
class Inner
{
public:
void infunction();
};
Inner d_inner; // class Inner must be known
};
The mentioned function Inner::infunction() can be called as part of the inline definition of Outer::caller(),
even though the definition of the class Inner is yet to be seen by the compiler. On the other hand, the
compiler must have seen the definition of the class Inner before a data member of that class can be defined.
Defining nested class members
Member functions of nested classes may be defined as inline functions. Inline member functions can be
defined as if they were functions defined outside of the class definition: if the function Outer::caller()would
have been defined outside of the class Outer, the full class definition (including the definition of the
class Inner) 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 class FirstWithin in the example of the previous section. The constructorFirstWithin() is
defined in the class FirstWithin, which is, in turn, defined within the class Surround. Consequently, the class
scopes of the two classes must be used to define the constructor. E.g.,
Surround::FirstWithin::FirstWithin()
{
variable = 0;
}
Declaring nested classes
Nested classes may be declared before they are actually defined in a surrounding class. Such forward
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 class Outer contains two nested classes Inner1 and Inner2. The
class Inner1 contains a pointer to Inner2 objects, and Inner2 contains a pointer to Inner1 objects. Such cross
references require forward declarations. These forward declarations must be specified in the same accesscategory as their actual definitions. In the following example the Inner2 forward declaration must be given
in a private section, as its definition is also part of the class Outer's private interface:
ARRAY OF STRUCTURES
As 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, Tree and 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?
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.
Definition
An 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 is
array_type variable_name[array_size];
in your C/C++ program you can declare an array like
int 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 0
Age 1
Age 2
Age 3
Age 4
Age 5
Age 6
Age 7
Age 8
Age 9
30
32
54
32
26
29
23
43
34
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 <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_EMPLOYEE 10
cout << "There are " << gCount << " employee with salary more than 3000" << endl
<< "There are " << lCount << " employee with salary less than 3000" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
How to declare and initialise multidimensional arrays?
Often there is need to manipulate tabular data or matrices. For example if employee salary is increased by
20% and you are required to store both the salaries in your program. Then you will need to store this
information into a two dimensional arrays. C/C++ gives you the ability to have arrays of any dimension.
Multi dimension arrays
Consider the example above, you have to store, previous salary, present salary and amount of increment. In
that case you will need to store this information in three dimensional arrays.
First I will show you how to declare a two dimensional array and initialise it. Then write a complete
program to use multidimensional arrays.
int Salary[10][2];
This defines an array containing 10 elements of type int. Each of these elements itself is an array of two
integers. So to keep track of each element of this array is we have to use two indices. One is to keep track of
row and other is to keep track of column.
Elements of multidimensional arrays
Here is a graphical view of multidimensional array that we use to store salary and increment on salary. First
column stores the salary element of the array and second column stores increment on salary. We could add
another column to store the new salary which adds the increment to the salary.
Column 0 Column 1
Salary
Increment
Row 0
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Initialising multidimensional arrays
Multidimensional arrays can also be initialised in two ways just like one dimensional array. Two braces are
used to surround the row element of arrays.
If you are initialising more than one dimension then you will have to use as many braces as the dimensions
of the array are.
int Salary [5][2] = {
{2300, 460},
{3400, 680},
{3200, 640},
{1200, 240},
{3450, 690}
};
int Salary [5][2] ={0}; //This will initialise all the array elements to 0
int Salary [5][2];
Salary [0][0]=2300;
Salary [1][0]=3400;
Salary [2][0]=3200;
Salary [3][0]=1200;
Salary [4][0]=3450;
Salary [0][1]=460;
Salary [1][1]=680;
Salary [2][1]=640;
Salary [3][1]=240;
Salary [4][1]=690;
Here is a complete program written in both C and C++ to demonstrate the use of multidimensional arrays.
You can find the whole source code in a zip file at the end of the tutorial. Source code is available in both C
and C++ programming languages. Zip file also contains the demonstration of three dimensional arrays.
Demonstration of two dimension arrays
The code below demonstrates two dimension arrays. It uses the same example of employee salary to
increment it by 20% and adds it to actual salary then print current salary, increment and new salary.
Two dimensional Array in C Programming
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_EMPLOYEE 10
int main(int argc, char *argv[]){
//initialise Salary of each employee
int Salary[NUM_EMPLOYEE][2]={
{2300,0},
{3400,0},
{3200,0},
{1200,0},
{3450,0},
{3800,0},
{3900,0},
{2680,0},
{3340,0},
{3000,0}
};
int lCount=0,gCount=0,i=0;
for(i=0; i<NUM_EMPLOYEE; i++){
Salary[i][1] = ((Salary[i][0]*20)/100);
}
printf("Initial Salary + Increment = Total Salary\n");
for (i=0; i<NUM_EMPLOYEE; i++){
printf("%d\t\t%d\t\t%d\n",Salary[i][0],Salary[i][1],Salary[i][0]+Salary[i][1]);
}
printf("Press ENTER to continue...\n");
getchar();
return 0;
}
Two dimensional array in C++ Programming
#include <cstdlib>
#include <iostream>
#define NUM_EMPLOYEE 10
using namespace std;
int main(int argc, char *argv[]){
POINTERS
Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers,
and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it
becomes necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple
and easy steps.
As you know, every variable is a memory location and every memory location has its address defined which
can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following
example, which will print the address of the variables defined:
#include <stdio.h>
int main ()
{
int var1;
char var2[10];
C Pointers in Detail:
Pointers have many but easy concepts and they are very important to C programming. There are following
few important pointer concepts which should be clear to a C programmer:
Concept
Description
C - Pointer arithmetic
C - Array of pointers
C - Pointer to pointer
The keyword public determines the access attributes of the members of the class that follow it. A public
member can be accessed from outside the class anywhere within the scope of the class object. You can also
specify the members of a class as private or protected which we will discuss in a sub-section.
Define C++ Objects:
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects
of a class with exactly the same sort of declaration that we declare variables of basic types. Following
statements declare two objects of class Box:
Box Box1;
Box Box2;
Both of the objects Box1 and Box2 will have their own copy of data members.
Accessing the Data Members:
The public data members of objects of a class can be accessed using the direct member access operator (.).
Let us try the following example to make the things clear:
#include <iostream>
using namespace std;
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main( )
{
Box Box1;
// Declare Box1 of type Box
Box Box2;
// Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
Description
private:
int data1;
float data2;
public:
void int_data(int d){
data1=d;
cout<<"Number: "<<data1;
}
float float_data(){
cout<<"\nEnter data: ";
cin>>data2;
return data2;
}
};
int main(){
temp obj1, obj2;
obj1.int_data(12);
cout<<"You entered "<<obj2.float_data();
return 0;
}
Output:
Number: 12
Enter data: 12.43
You entered: 12.43
Explanation of Program
In
this
program,
two
data
members data1 and data2 and
two
member
function int_data() andfloat_data() are defined under temp class. Two objects obj1 and obj2 of that
class are declared. Function int_data() for the obj1 is executed using code obj1.int_data(12);, which
sets 12 to thedata1 of object obj1. Then, function float_data() for the object obj2 is executed which
takes data from user; stores it in data2 of obj2 and returns it to the calling function.
Note: In this program, data2 for object obj1 and data1 for object obj2 is not used and contains
garbage value.