programming in c++ lecture 7 structures

14
Programming in C++ Lecture 7 –Structures By T.K.Malwatta Senior Lecturer Department of Software Technology Faculty of IT UoVT 1

Upload: others

Post on 03-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Programming in C++ Lecture 7 –Structures

By T.K.Malwatta

Senior Lecturer Department of Software Technology

Faculty of IT UoVT

1

Structure Structure is a collection of variables under a single name. (Variables can be of any type: int, float, char etc.) Sometimes, some logically related elements need to be treated under one unit. For example, the elements storing a student's information (e.g, rollno, name, class, marks, grade) need to be processed together under one roof. Similarly, elements keeping a date's information (e.g., day, month, and year) need to be processed together. To handle and serve to such situations, C++ offers structures. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name. The data item in a structure are called the members of the structure.

Declaring data structure

struct structure name

{

Data type structure member 1;

Data type structure member 2;

.

..

} structure variables; 2

Example Three variables: custnum of type int, salary of type int, commission of type float are structure members and the structure name is Customer. This structure is

declared as follows:

Declare Structure Variables

This is similar to variable declaration. For variable declaration, data type is defined followed by variable name. For structure variable declaration, the data type is the name of the structure followed by the structure variable name Above example, structure variable cust1 define as Customer cust1

(structure name structure variable) 3

How to access structure members in C++ To access structure members, the operator used is the dot operator denoted by (.). The dot operator for accessing structure members is used thusly: structure variable name . member name example cust1. salary=2000; Structure Members Initialization: As with arrays and variables, structure members can also be initialized. This is performed by enclosing the values to be initialized inside the braces { and } after the structure variable name while it is defined.

Example

Customer cust1={100,20000,35.5};

We can also declare structure variables at the time of defining the structure as follows.

struct Customer { int custnum; int salary; float commission; } cust1,cust2;

4

Example 1 include <iostream> using namespace std; struct Customer { int custnum; int salary; float commission; }; int main( ) { Customer cust1; cust1.custnum=100; cust1.salary =20000; cust1.commission=35.5; //Customer cust1={100,20000,35.5}; cout<<"Customer Number:"<< cust1.custnum<<endl; cout<<"Customer Salary:"<< cust1.salary<<endl; cout<<"Customer Commision:"<< cust1.commission<<endl; return 0; }

5

Example 2 #include <iostream> #include <cstring> using namespace std; struct student { int Reg_No; string name; int phone_number; }; int main() { //structure name and variable name student stu1 = {1,"Brown",242526}; //first student details student stu2;// second students details stu2.Reg_No=2; stu2.name="Smith"; stu2.phone_number=2678222;

cout<<"Students Information\n"; cout<<"Student 1 Details \n"; cout<<"student Reg_No: "<<stu1.Reg_No<<endl; cout<<"student Name: "<<stu1.name<<endl; cout<<"student Phone_number: "<<stu1.phone_number<<endl<<endl;; cout<<"Student 2 Details \n"; cout<<"student Reg_No: "<<stu2.Reg_No<<endl; cout<<"student Name: "<<stu2.name<<endl; cout<<"student Phone_number: "<<stu2.phone_number<<endl; return 0; }

Array of Structures We can also make an array of structures. In the first example in structures, we stored the data of 2 students. Now suppose we need to store the data of 100 such children. Declaring 100 separate variables of the structure is definitely not a good option. For that, we need to create an array of structures. Let's see an example for 5 students.

#include <iostream> #include <cstring> using namespace std; struct student { int roll_no; string name; int phone_number; };

int main(){ student stud[5]; int i; for(i=0; i<5; i++) { //taking values from user cout << "Student " << i + 1 << endl; cout << "Enter roll no" << endl; cin >> stud[i].roll_no; cout << "Enter name" << endl; cin >> stud[i].name; cout << "Enter phone number" << endl; cin >> stud[i].phone_number; } for(i=0; i<5; i++) {//printing values cout << "Student " << i + 1 << endl; cout << "Roll no : " << stud[i].roll_no << endl; cout << "Name : " << stud[i].name << endl; cout << "Phone no : " << stud[i].phone_number << endl; } return 0; }

Example 3 #include <iostream> #include <cstring> using namespace std; struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { //struct Books Book1 Books Book1 ; // Declare Book1 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 = 6495; // Print Book1 info cout << "Book 1 title : " << Book1.title <<endl; cout << "Book 1 author : " << Book1.author <<endl; cout << "Book 1 subject : " << Book1.subject <<endl; cout << "Book 1 id : " << Book1.book_id <<endl; return 0; }

10

A structure variable can be passed to a function in similar way as normal argument. Consider this example: Example 1 #include<iostream> using namespace std; struct Person { int citizenship; int age; }; void func( Person p); int main() { Person p; p.citizenship = 1; p.age = 27; func(p); return 0; } void func(Person p) { cout << " Person citizenship: " << p.citizenship<<endl; cout << " Person age: " << p.age; }

Passing structure to function in C++

Example 2 #include <iostream> using namespace std; struct Person { char name[50]; int age; float salary; }; void displayData(Person p); // 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; }

Exercises Write a program using structure . Details are give below (1) Structure Name: Person There are 3 members of the structure name,age,salary Read above structure members and display the name,age and salary; (2) Structure name : Student Members : Name, ID, Marks(3 subject marks) Read above structure members and display Student name,ID, marks of 3 subjects