Chapter 1 D Struct
Chapter 1 D Struct
Chapter one
Overviews of C++ concepts
Properties of arrays
- Arrays in C++ are zero-bounded; that is the index of the first element in
the array is 0 and the last element is N-1, where N is the size of the array.
- It is illegal to refer to an element outside of the array bounds, and your
program will crash or have unexpected results, depending on the compiler.
- Array can only hold values of one type
Arrays(cont.)
Accessing arrays
• In any point of the program in which the array is visible we can access
individually anyone of its elements for reading or modifying it as if it was
a normal variable. To access individual elements, index or subscript is
used.
The format is the following:
name [ index ]
Multidimensional Arrays
Multidimensional arrays can be described as arrays of arrays. For example, a
bi-dimensional array can be imagined as a bi-dimensional table of a uniform
concrete data type.
Example
int matrix[3][5];
Multidimensional arrays are not limited to two indices (two dimensions).
They can contain so many indices as needed, For example:
char century [100][365][24][60][60];
Functions
A function provides a convenient way of packaging a computational recipe, so that
it can be used as often as required. Therefore, a function is a block of code designed
to tackle a specific problem
C++ adhere the following rules on functions
- Every functions must have a name
- Function names are made up and assigned by the programmer
following the same rules that apply to naming variables. They can contain up to
32 characters, they must begin with a letter, and they can consist of letters,
numbers, and the underscore (_) character
- All function names have one set of parenthesis immediately following them.
This helps you (and C++ compiler) differentiate them from variables.
- The body of each function, starting immediately after parenthesis of the function
name, must be enclosed by braces
Functions(cont.)
Declaring function:
The interface of a function (also called its prototype) specifies how it may be used. It
consists of three entities:
The function return type. This specifies the type of value the function returns. A
function which returns nothing should have a return type void
The function name. this is simply a unique identifier
The function parameters (also called its signature). This is a set of zero or more typed
identifiers used for passing values to and from the function
Example
Void fun1();, int fun2(int x, int y);
Defining a function
A function definition consists of two parts: interface (prototype) & body. The brace of a
function contains the computational steps (statements) that computerize the function.
The definition consists of a line called the decelerator
Example
Void func1() – no semi colon decelerator
{
//body implementation
}
Functions(cont.)
If function definition is done before the main function, then there is no need to
put the prototype, otherwise the prototype should be scripted before the main
function starts.
Calling the function
- Calling a function means making the instruction of the function to be
executed.
- A function call consists of the function name followed by the call operator
brackets ‘()’, inside which zero or more comma-separated arguments
appear.
-The number and type of arguments should match the number of function
parameters. Each argument is an expression whose type should match the
type of the corresponding parameter in the function interface.
- The parameters of a function are list of variables used by the function to
perform its task and the arguments passed to the function during calling of
a function are values sent to the function
example func1();
Functions(cont.)
- Inline Functions :- a function that is expanded when it is called and its
definitions are small. Syntax for inline functions
Inline int x()
{
Return 2;
}
Example Suppose that a program frequently requires finding the absolute
value
of an integer quantity
- Overloaded Functions :- Functions with the same name are called
overloaded functions. C++ requires that each overloaded functions differ in
its argument list. Overloaded functions enable you to have similar
functions that work on different types of data.
- Recursion Functions :- A function which calls itself is said to be recursive.
Recursion is a general programming technique applicable to problems
which can be defined in terms of themselves example factorial
Structures
A structure is a collection of one or more variable types grouped together that
can be referred using a single name (group name) and a member name.
- Each element (called a member) in a structure can be of different data type
The General Syntax of structures is:
Struct [structure tag]
{
Member definition;
Member definition;
…
Member definition;
}[one or more structure variables];
Example
struct Inventory
{
char description[15];
char part_no[6];
int quantity; members of the structure
float cost;
}; // all structures end with semicolon
struct Inventory inv;//structure variable declaration
Structures(cont.)
Referencing members of a structure
To refer to the members of a structure we need to use the dot operator (.)
The General syntax to access members of a structure variable would be:
VarName.Member
Where VarName is the varaibale name of the structure variable And Member is
varaibale name of the members of the structure
inv.cost // from previous example
Initializing Structure Data
You can initialize members when you declare a structure, or you can initialize
a structure in the body of the program
struct cd_collection
{
char title[25];
char artist[20];
int num_songs;
float price;
char date_purchased[9];
} cd1 = {"Red Moon Men","Sams and the Sneeds", 12, 11.95,"08/13/93"};
Structures(cont.)
Arrays of Structures
Arrays of structures are good for storing a complete employee file, inventory
file, or any other set of data that fits in the structure format.
Example
struct Company
{
int employees;
int registers;
double sales;
}store[1000];
Referencing the array structure
The dot operator (.) works the same way for structure array element as it does
for regular variables.
Example
Store[2].employees += 3 // if employees number increase by 3 for store index
2.
Pointers
A pointer is a variable which stores the address of another variable. The only
difference between pointer variable and regular variable is the data they hold.
There are two pointer operators in C++:
& the address of operator
* the dereference operator
Whenever you see the & used with pointers, think of the words “address of.”
The & operator always produces the memory address of whatever it precedes. The *
operator, when used with pointers, either declares a pointer or dereferences the
pointer’s value. The dereference operator can be literally translated to "value
pointed by" .
A pointer is simply the address of an object in memory. Generally, objects can
be accessed in two ways: directly by their symbolic name, or indirectly
through a pointer. The act of getting to an object via a pointer to it, is called
dereferencing the pointer. Pointer variables are defined to point to objects of
a specific type so that when the pointer is dereferenced, a typed object is
obtained.
Pointers(cont.)
Declaring Pointers:
Is reserving a memory location for a pointer variable in the heap.
Syntax: type * pointer_name ;
Example int * x;
Whenever the dereference operator, *, appears in a variable declariation, the
variable being declared is always a pointer variable.
Assigning values to pointers:
int age = 26;
int * p_age;
p_age = &age;
OR
int age = 26;
int * p_age = & age;
both ways are possible
This would assign to variable p_age the address of variable age, since when
preceding the name of the variable age with the ampersand ( & ) character we
are no longer talking about the content of the variable, but about its address
in memory.
Pointers(cont.)
Arrays of Pointers
If you have to reserve many pointers for many different values, you might
want to declare an array of pointers. The following reserves an array of 10
integer pointer variables:
int *iptr[10]; //reserves an array of 10 integer pointers
iptr[4] = &age;// makes iptr[4] point to address of age.
Pointer to pointer
The address of a pointer can also be stored in another pointer. This pointer is
said to be pointer to a pointer. An array of pointer is conceptually same as
pointer to pointer type. The pointer to pointer type is declared as follows:
Data_type ** pointer_name;
Example
char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;
Data structure
Program is a set of instruction which is written in order to solve a
problem.
A solution to a problem actually consists of two things:
A way to organize the data
Sequence of steps to solve the problem
The way data are organized in a computers memory is said to be Data
Structure.
The sequence of computational steps to solve a problem is said to be an
Algorithm.
Therefore, a program is Data structures plus Algorithm.
• The first step to solve the problem is obtaining ones own abstract
view, or model, of the problem.
• This process of modeling is called abstraction.
Data structure(cont.)
Abstraction is a process of classifying characteristics as relevant and irrelevant
for the particular purpose at hand and ignoring the irrelevant ones.