0% found this document useful (0 votes)
31 views32 pages

Structures and Namespace: Dr. Hadeer A. Hosny

The document discusses structures and namespaces in C++. It covers creating and initializing structures, assigning structure variables, comparing structures, passing structures to functions, pointers to structures, and arrays of structures. Structures allow grouping of different data types under a single name, like storing student records with attributes such as ID, name, marks etc. The key points are defining a structure with struct, initializing members, accessing members with . or ->, passing structures as arguments to functions, and creating arrays of structures to store multiple records.

Uploaded by

koumton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views32 pages

Structures and Namespace: Dr. Hadeer A. Hosny

The document discusses structures and namespaces in C++. It covers creating and initializing structures, assigning structure variables, comparing structures, passing structures to functions, pointers to structures, and arrays of structures. Structures allow grouping of different data types under a single name, like storing student records with attributes such as ID, name, marks etc. The key points are defining a structure with struct, initializing members, accessing members with . or ->, passing structures as arguments to functions, and creating arrays of structures to store multiple records.

Uploaded by

koumton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1

Structures and
namespace
Dr. Hadeer A. Hosny

Second term
2021
2 Points to be covered:
 Structure
 Introduction
 Create a Structure
 Initializing Structures
 Assignment in structure variable
 Comparing between structure variables
 Structure and Functions
 Pointer to Structure
 Array of Structures
 Nested Structures
 Namespace
3 Introduction
 C/C++ arrays allow you to define variables that combine
several data items of the same kind, but structure is another
user defined data type which allows you to combine data items
of different kinds.
 Structures are used to represent a record, suppose you want to
keep track of your books in a library. You might want to track the
following attributes about each book −
• Title
• Author
• Subject
• Book ID
4 Introduction
 A structure is a collection of variables of different data types
under a single name.
 For Example : to store a student record which includes different
data items like (student_no, Fname, Lname, Total_Marks, GPA ,
…….).

 The structure is also called a user-defined data type.


5 Creating a Structure

 Steps to create a Structure:

1. Declare/ Define Structure


2. Initialize member of structure
3. Access structure elements
Declare/Define structure
6
 1. To define a structure, you must use the struct
statement. The struct statement defines a new data type,
with more than one member, for your program.
 The format of the struct statement is this −

struct [structure tag] { Example struct Books {


member definition; char title[50];
member definition;
char author[50];
char subject[100];
...
int book_id;
member definition; };
};

 The structure tag is optional and each member


definition is a normal variable definition, such as int i; or
float f; or any other valid variable definition.
7
Declare/Define structure
For Example : To store an Employee data like
( emp_no, fname, lname, salary , bonus , net_salary);
Initialize member of structure
8
 2. Once , a new struct is declared , we can
Initialize member of structure and use it as any
other data.
Accessing to structure
9
 3. To Access the struct elements or members use
the“.” Operator Emp1.
Structure
10
 Following is the example to explain usage of structure

#include <iostream>
#include <cstring>
using namespace std; Structure
struct Books { declaration
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
Initialize member
struct Books Book1; // Book1 of type Book of structure
struct Books Book2; // Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407; Access to structure

// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
Structure
11
 Following is the example to explain usage of structure

// Print Book1 info Print content of


cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author <<endl; structure
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;

// Print Book2 info


cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author <<endl;
cout << "Book 2 subject : " << Book2.subject <<endl;
cout << "Book 2 id : " << Book2.book_id <<endl;

return 0;
}
Initializing Structures
12
 Like normal variables Structure at the same time of
declaration.
 Initialization of structure is almost similar to initializing
arrays.
Initializing Structures
13
Assignment in structure variable
14
 Value of one struct variable can be assigned to another
struct variable of the same type using an assignment
statement.
Comparing between structure
15 variables

 To compare the values of emp1and emp2


Structure and Functions
16
 A struct variable can be passed as a parameter by value or
by reference.
 A function can return a value of type struct.
Structure and Functions
17
 Following is the example to explain usage of structure
#include<iostream>
Pass structure to
#include<cstring> function
using namespace std;
void printBook( struct Books book );

struct Books { Structure


char title[50]; declaration
char author[50];
char subject[100];
int book_id; Initialize member
};
int main() { of structure
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Access to structure
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
Structure and Functions
18
 Following is the example to explain usage of structure

// Print Book1 info


printBook( Book1 );

// Print Book2 info


printBook( Book2 );
Print content of
return 0; structure using
}
void printBook( struct Books book ) {
function
cout << "Book title : " << book.title <<endl;
cout << "Book author : " << book.author <<endl;
cout << "Book subject : " << book.subject <<endl;
cout << "Book id : " << book.book_id <<endl;
}
Pointer to Structure
19

 You can define pointers to structures in the same way


as you define pointer to any other variable

 Now, you can store the address of a structure variable


in the above defined pointer variable.
 To find the address of a structure variable, place the
'&'; operator before the structure's name as follows
Pointer to Structure
20

 To access the members of a structure using a pointer


to that structure, you must use the(-> )operator as
follows
Pointer to Structure
21  Let us re-write above example using structure pointer,
hope this will be easy for you to understand the concept −
#include <iostream> Pass structure to
#include <cstring>
function by refrence
using namespace std;
void printBook( struct Books *book );
struct Books {
char title[50]; Structure
char author[50]; declaration
char subject[100];
int book_id;
}; Initialize member
int main() {
struct Books Book1; of structure
// Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
Access to structure
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
Pointer to Structure
22
 Following is the example to explain usage of structure

// Print Book1 info, passing address of structure


printBook( &Book1 );
// Print Book1 info, passing address of structure
printBook( &Book2 );
return 0;
}
Print content of
structure using
// This function accept pointer to structure as parameter.
void printBook( struct Books *book ) { function
cout << "Book title : " << book->title <<endl;
cout << "Book author : " << book->author <<endl;
cout << "Book subject : " << book->subject <<endl;
cout << "Book id : " << book->book_id <<endl;
}
23 Using arrays in structures
Array of Structures
24

 Let us consider having a structure as:

If we want to keep record of 100 Employee, we have to make


100 structure variables as Emp1, Emp2, …, Emp100.
 In this situation we can use array of structure to store the
records of 100 students which is easier and efficient to
handle(because loops can be used).
25
Nested Structure
26

 Nested Structure is a structs within a struct.


 We can write one structure inside another structure
as member of another structure.
Nested Structure
27
Namespace
28

 Consider a situation, when we have two persons with the same


name, Zara, in the same class. Whenever we need to
differentiate them definitely we would have to use some
additional information along with their name, like either the
area, if they live in different area or their mother’s or father’s
name, etc.
 Same situation can arise in your C++ applications.
 For example, you might be writing some code that has a
function called xyz() and there is another library available
which is also having same function xyz(). Now the compiler has
no way of knowing which version of xyz() function you are
referring to within your code.

Solution is using
namespace
Namespace
29
 A namespace is designed to overcome this difficulty and is used as
additional information to differentiate similar functions, classes,
variables etc. with the same name available in different libraries.

 Defining a Namespace
 A namespace definition begins with the keyword namespace
followed by the namespace name as follows −

namespace namespace_name {
// code declarations
}

 To call the namespace-enabled version of either function or


variable, prepend (::) the namespace name as follows
 name::code; // code could be variable or function.
Namespace
30

#include <iostream>
using namespace std;
// first name space
namespace first_space { Output
void func() {
cout << "Inside first_space" << endl;
}
}

// second name space


namespace second_space {
void func() {
cout << "Inside second_space" << endl;
}
}
int main () {
// Calls function from first name space.
first_space::func();
// Calls function from second name space.
second_space::func();
return 0;
}
Namespace
31 The using directive
 You can also avoid prepending of namespaces with the using
namespace directive. This directive tells the compiler that the
subsequent code is making use of names in the specified
namespace.
#include <iostream>
using namespace std;
// first name space
namespace first_space {
void func() {
cout << "Inside first_space" << endl;
}
}
// second name space
namespace second_space { Output
void func() {
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main () {
// This calls function from first name space.
func();
return 0;
}
Namespace
32 The using directive
 The ‘using’ directive can also be used to refer to a particular
item within a namespace. For example, if the only part of the
std namespace that you intend to use is cout, you can refer to it
as follows −
 using std::cout;

#include <iostream> Output


using std::cout;

int main () {
cout << "std::endl is used with std!" <<
std::endl;

return 0;
}

You might also like