0% found this document useful (0 votes)
28 views14 pages

DS Notes

Uploaded by

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

DS Notes

Uploaded by

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

C++ Structures

A structure is a user-defined data type in C/C++. A structure creates a data type that can be
used to group items of possibly different types into a single type.

How to declare a structure in C++ programming?


struct Person

char name[50];

int age;

float salary;

};

How to define a structure variable?


Once you declare a structure person as above. You can define a structure variable as:

Person bill;

How to access members of a structure?


he 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;

1. #include <iostream>
2. using namespace std;
3.
4. struct Person
5. {
6. char name[50];
7. int age;
8. float salary;
9. };
10.
11. int main()
12. {
13. Person p1;
14.
15. cout << "Enter Full name: ";
16. cin.get(p1.name, 50);
17. cout << "Enter age: ";
18. cin >> p1.age;
19. cout << "Enter salary: ";
20. cin >> p1.salary;
21.
22. cout << "\nDisplaying Information." << endl;
23. cout << "Name: " << p1.name << endl;
24. cout <<"Age: " << p1.age << endl;
25. cout << "Salary: " << p1.salary;
26.
27. return 0;
28. }

C++ Program to Add Two Distances (in inch-


feet) System Using Structures
This program takes two distances (in inch-feet system), adds them and displays the
result on the screen.

1. #include <iostream>
2. using namespace std;
3.
4. struct Distance{
5. int feet;
6. float inch;
7. }d1 , d2, sum;
8.
9. int main()
10. {
11. cout << "Enter 1st distance," << endl;
12. cout << "Enter feet: ";
13. cin >> d1.feet;
14. cout << "Enter inch: ";
15. cin >> d1.inch;
16.
17. cout << "\nEnter information for 2nd distance" << endl;
18. cout << "Enter feet: ";
19. cin >> d2.feet;
20. cout << "Enter inch: ";
21. cin >> d2.inch;
22.
23. sum.feet = d1.feet+d2.feet;
24. sum.inch = d1.inch+d2.inch;
25.
26. // changing to feet if inch is greater than 12
27. if(sum.inch > 12)
28. {
29. ++ sum.feet;
30. sum.inch -= 12;
31. }
32.
33. cout << endl << "Sum of distances = " << sum.feet << " feet "
<< sum.inch << " inches";
34. return 0;
35. }
36.

C++ Structure and Function


In this article, you'll find relevant examples to pass structures as an argument to a
function, and use them in your program.

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>

using namespace std;

struct Person

char name[50];

int age;
float salary;

};

void displayData(Person); // Function declaration

int main()

Person p;

cout << "Enter Full name: ";

cin.get(p.name, 50);

cout << "Enter age: ";

cin >> p.age;

cout << "Enter salary: ";

cin >> p.salary;

// Function call with structure variable as an argument

displayData(p);

return 0;

void displayData(Person p)

cout << "\nDisplaying Information." << endl;


cout << "Name: " << p.name << endl;

cout <<"Age: " << p.age << endl;

cout << "Salary: " << p.salary;

Output

Enter Full name: Bill Jobs


Enter age: 55
Enter salary: 34233.4

Displaying Information.
Name: Bill Jobs
Age: 55
Salary: 34233.4

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);

Nested Structure (Structure with in


Structure).
In this program we will learn how to declare, read and access
Nested Structure, Structure within Structure?

In this program we will declare two


structure student and date_of_birth. date_of_birth will be accessed
inside the student structure.

Nested Structure
/*C++ - program for Nested Structure
(Structure with in Structure).*/

#include <iostream>
using namespace std;
struct date_of_birth{
int dd,mm,yy;
};

struct student{
char name[30];
int rollNumber;
date_of_birth dob;
};

int main(){
student s;
cout<<"Enter name : ";
cin.getline(s.name,25);
cout<<"Enter roll number : ";
cin>>s.rollNumber;
cout<<"Enter date of birth (dd mm yy) : " ;
cin>>s.dob.dd>>s.dob.mm>>s.dob.yy;

cout<<"Name:"<<s.name<<",Roll
Number:"<<s.rollNumber<<endl;
cout<<"Date of
birth:"<<s.dob.dd<<"/"<<s.dob.mm<<"/"<<s.dob.yy<<endl;

return 0;
}
Enter name : Mike
Enter roll number : 101
Enter date of birth (dd mm yy) : 29 09 2000
Name:Mike,Roll Number:101
Date of birth:29/9/2000

Array and Structure.


Array and Structure in C++
We can use structure and array as :

 Array of Structure
 Array within Structure

Array of Structure
As we know "Array is the collection of variables of similar data
types" same as "Array of Structures is the collection of
structure variables".

Array of Structure
#include<iostream>

using namespace std;

struct Employee

int Id;

char Name[25];

int Age;

long Salary;

};

int main()

int i;

Employee Emp[ 3 ]; //Statement 1

for(i=0;i<1;i++)

cout << "\nEnter details of " << i+1 << " Employee";
cout << "\n\tEnter Employee Id : ";

cin >> Emp[i].Id;

cout << "\n\tEnter Employee Name : ";

cin >> Emp[i].Name;

cout << "\n\tEnter Employee Age : ";

cin >> Emp[i].Age;

cout << "\n\tEnter Employee Salary : ";

cin >> Emp[i].Salary;

cout << "\nDetails of Employees";

for(i=0;i<1;i++)

cout << "\n"<< Emp[i].Id <<"\t"<< Emp[i].Name <<"\t"

<< Emp[i].Age <<"\t"<< Emp[i].Salary;

Array within Structure

array within structure


As we know, structure is collection of different data type. Like normal data type, It can
also store an array as well.

Syntax for array within structure


#include<iostream>

using namespace std;

struct Student

int Roll;

char Name[25];

int Marks[3]; //Statement 1 : array of marks

int Total;

float Avg;

};

int main()

int i;

Student S;

cout << "\n\nEnter Student Roll : ";

cin >> S.Roll;

cout << "\n\nEnter Student Name : ";

cin >> S.Name;

S.Total = 0;
for(i=0;i<3;i++)

cout << "\n\nEnter Marks " << i+1 << " : ";

cin >> S.Marks[i];

S.Total = S.Total + S.Marks[i];

S.Avg = S.Total / 3;

cout << "\nRoll : " << S.Roll;

cout << "\nName : " << S.Name;

cout << "\nTotal : " << S.Total;

cout << "\nAverage : " << S.Avg;

Structure to Function

We can also pass a structure to a function.


There are two methods by which we can pass structures to functions.

 Passing by Value
 Passing by Reference

Passing by Value
In this, we pass structure variable as an argument to a function. Let's see an example to
make it clearer.

#include <iostream>
using namespace std;

struct student
{
int roll_no;
string name;
int phone_number;
};

void display(struct student st)


{
cout << "Roll no : " << st.roll_no << endl;
cout << "Name : " << st.name << endl;
cout << "Phone no : " << st.phone_number << endl;
}

int main(){
struct student s;
s.roll_no = 4;
s.name = "Ron";
s.phone_number = 888888;
display(s);
return 0;
}

Output

Roll no : 4
Name : Ron
Phone no : 888888

In this example, we are printing roll number, name and phone number of a student
using a function. We first declared a structure named student with roll_no, name and
phone number as its members and 's' as its variable. Then we assigned the values of
roll number, name and phone number to the structure variable s. Just as we pass any
other variable to a function, we passed the structure variable 's' to a function 'display'.
Now, while defining the function, we passed a copy of the variable 's' as its argument
with 'struct student' written before it because the variable which we have passed is of
type structure named student. Finally, in the function, we printed the name, roll number
and phone number of the structure variable.

Passing by Reference

In passing by reference, the address of a structure variable is passed to a function. In


this, if we change the structure variable which is inside the function, the original
structure variable which is used for calling the function changes. This was not the case
in calling by value.

#include <iostream>
using namespace std;

struct student
{
int roll_no;
string name;
int phone_number;
};

void display(struct student *st)


{
cout << "Roll no : " << st -> roll_no << endl;
cout << "Name : " << st -> name << endl;
cout << "Phone no : " << st -> phone_number << endl;
}

int main(){
struct student s;
s.roll_no = 4;
s.name = "Ron";
s.phone_number = 888888;
display(&s;);
return 0;
}

Output

Roll no : 4
Name : Ron
Phone no : 888888

This case is similar to the previous one, the only difference is that this time, we are
passing the address of the structure variable to the function. While declaring the
function, we passed the pointer of the copy 'st' of the structure variable 's' in its
parameter. Since the pointer is of a variable of type structure named student, we wrote
'struct student' before the name of the pointer in the argument of the function. In the
function , we accessed the members of the pointer using -> sign as discussed before.
Try to change the value of the variables inside the function in both the cases and see
the changes in the real variables.

Pointers to Structure
1. #include <iostream>
2. using namespace std;
3.
4. struct Distance
5. {
6. int feet;
7. float inch;
8. };
9.
10. int main()
11. {
12. Distance *ptr, d;
13.
14. ptr = &d;
15.
16. cout << "Enter feet: ";
17. cin >> (*ptr).feet;
18. cout << "Enter inch: ";
19. cin >> (*ptr).inch;
20.
21. cout << "Displaying information." << endl;
22. cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch
<< " inches";
23.
24. return 0;
25. }

Output

Enter feet: 4
Enter inch: 3.5
Displaying information.
Distance = 4 feet 3.5 inches

In this program, a pointer variable ptr and normal variable d of type


structure Distance is defined.
The address of variable d is stored to pointer variable, that is, ptr is pointing to
variable d. Then, the member function of variable d is accessed using pointer.
Note: Since pointer ptr is pointing to variable d in this
program, (*ptr).inch and d.inch is exact same cell.
Similarly, (*ptr).feet and d.feet is exact same cell.

The syntax to access member function using pointer is ugly and there is alternative
notation -> which is more common.

ptr->feet is same as (*ptr).feet

ptr->inch is same as (*ptr).inch

You might also like