Thanks to visit codestin.com
Credit goes to www.scribd.com

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

ch8C++ Struction

fdgdfgdfgdfg

Uploaded by

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

ch8C++ Struction

fdgdfgdfgdfg

Uploaded by

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

C++

LANGUAGES
C++ Structures
C++ STRUCTURES
 Structure is a collection of variables of different data
types under a single name. It is similar to a class in
that, both holds a collecion of data of different data
types.
 For example: You want to store some information
about a person: his/her name, citizenship number and
salary. You can easily create different variables name,
citNo, salary to store these information separately.
 However, in the future, you would want to store information about multiple
persons. Now, you'd need to create different variables for each information
per person: name1, citNo1, salary1, name2, citNo2, salary2

 You can easily visualize how big and messy the code would look. Also, since
no relation between the variables (information) would exist, it's going to be
a daunting task.

 A better approach will be to have a collection of all related information


under a single name Person, and use it for every person. Now, the code
looks much cleaner, readable and efficient as well.
HOW TO DECLARE A STRUCTURE IN
C++ PROGRAMMING?
 The struct keyword defines a structure type followed by an identifier (name
of the structure).
 Then inside the curly braces, you can declare one or more members
(declare variables inside curly braces) of that structure. For example:
 struct Person
{
 char name[50];
 int age;
 float salary;
 };
 Here a structure person is defined which has three members: name, age
and salary.
 When a structure is created, no memory is allocated.
 The structure definition is only the blueprint for the creating of variables.
You can imagine it as a datatype. When you define an integer as below:int
foo;
 The int specifies that, variable foo can hold integer element only. Similarly,
structure definition only specifies that, what property a structure variable
holds when it is defined.

 Note: Remember to end the declaration with a semicolon (;)


HOW TO DEFINE A STRUCTURE
VARIABLE?
 Once you declare a structure person as above. You can define a structure variable
as:

 Person bill;
 Here, a structure variable bill is defined which is of type structure Person.

 When structure variable is defined, only then the required memory is allocated by
the compiler.

 Considering you have either 32-bit or 64-bit system, the memory of float is 4 bytes,
memory of int is 4 bytes and memory of char is 1 byte.

 Hence, 58 bytes of memory is allocated for structure variable bill.


HOW TO ACCESS MEMBERS OF A
STRUCTURE?
 The members of structure variable is accessed using a dot (.) operator.

 Suppose, you want to access age of structure variable bill and assign it 50
to it. You can perform this task by using following code below:

 bill.age = 50;
EXAMPLE: C++ STRUCTURE

 #include <iostream> int main()


 using namespace std; {
Person p1;

 struct Person cout << "Enter Full name: ";


cin.get(p1.name, 50);
{ cout << "Enter age: ";

cin >> p1.age;
char name[50]; cout << "Enter salary: ";
 int age; cin >> p1.salary;
 float salary; cout << "\nDisplaying Information." << endl;
 }; cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;

return 0;
}
C++ STRUCTURE AND
FUNCTION
C++ STRUCTURE AND FUNCTION

 Structure variables can be passed to a function and returned in a similar


way as normal arguments.

 Passing structure to function in C++


 A structure variable can be passed to a function in similar way as normal
argument. Consider this example:
EXAMPLE 1: C++ STRUCTURE AND FUNCTION

#include <iostream> int main() {


using namespace std; Person p;

struct Person { cout << "Enter Full name: ";


char name[50]; cin.get(p.name, 50);
int age; cout << "Enter age: ";
float salary; cin >> p.age;
}; cout << "Enter salary: ";
cin >> p.salary;
void displayData(Person);
// Function declaration // Function call with structure variable as an
argument
void displayData(Person p) { displayData(p);
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl; return 0;
cout <<"Age: " << p.age << endl; }
cout << "Salary: " << p.salary;
}
 In this program, user is asked to enter the name, age and salary of a
Person inside main() function.

 Then, the structure variable p is to passed to a function using.

 displayData(p);
 The return type of displayData() is void and a single argument of type
structure Person is passed.

 Then the members of structure p is displayed from this function.


EXAMPLE 2: RETURNING STRUCTURE FROM
FUNCTION IN C++
#include <iostream> Person getData(Person p) {
using namespace std;
cout << "Enter Full name: ";
struct Person { cin.get(p.name, 50);
char name[50];
int age; cout << "Enter age: ";
float salary; cin >> p.age;
};
cout << "Enter salary: ";
Person getData(Person); cin >> p.salary;
void displayData(Person);
return p;
int main() { }

Person p, temp;

temp = getData(p);
p = temp;
displayData(p);

return 0;
}
 In this program, we have created two structure variables p and temp of
type Person under the main() function.

 The structure variable p is passed to getData() function which takes input


from the user which is then stored in the temp variable.

 temp = getData(p);
 We then assign the value of temp to p.

 p = temp;
 Then the structure variable p is passed to displayData() function, which
displays the information.
 Note: We don't really need to use the temp variable for most compilers and
C++ versions. Instead, we can simply use the following code:

 p = getData(p);

You might also like