Tuesday 18 June 2019

structure


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.
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
{
    int age;
    float salary;
};
Here a structure person is defined which has two members:  age and salary.

C++ Program to assign data to members of a structure variable and display it.
#include <iostream>
using namespace std;

struct Person
{
   
    int age;
    float salary;
};

int main()
{
    Person p1;
   
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;

    cout << "\nDisplaying Information." << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;

    return 0;
}
Output
Enter age: 27
Enter salary: 1024.4

Displaying Information.
Age: 27
Salary: 1024.4


EXAMPLE :


#include <iostream>
using namespace std;

int main()
{
    struct product {
  int weight;
  double price;
} apple, banana, melon;
   
   
 apple.weight=10;
 apple.price=20;

    cout << "\nDisplaying Information." << endl;
    cout <<"Apple weight " <<apple.weight << endl;
    cout << "apple price  " << apple.price;

    return 0;

}

#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;        // 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;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;

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

   // 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;
}
When the above code is compiled and executed, it produces the following result −
Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700


#include <iostream>
#include <cstring>

using namespace std;

int main(){

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

 struct student p1 = {1,"Brown",123443};
   struct student p2, p3;
   p2.roll_no = 2;
   p2.name = "Sam";
   p2.phone_number = 1234567822;
   p3.roll_no = 3;
   p3.name = "Addy";
   p3.phone_number = 1234567844;

   cout << "First Student" << endl;
   cout << "roll no : " << p1.roll_no << endl;
   cout << "name : " << p1.name << endl;
   cout << "phone no : " << p1.phone_number << endl;
   cout << "Second Student" << endl;
   cout << "roll no : " << p2.roll_no << endl;
   cout << "name : " << p2.name << endl;
   cout << "phone no : " << p2.phone_number << endl;
   cout << "Third Student" << endl;
   cout << "roll no : " << p3.roll_no << endl;
   cout << "name : " << p3.name << endl;
   cout << "phone no : " << p3.phone_number << endl;
 return 0;
}





Structure and Function in C++


In this previous tutorial we learnt about structures, the compound data type that groups different types of variables. In this tutorial, we will learn how to pass structures as an argument to the function and how to return the structure from the function.

How to pass structure as an argument to function

Here we have a function printStudentInfo() which takes structure Student as an argument and prints the details of student using structure varaible. The important point to note here is that you should always declare the structure before function declarations, otherwise you will get compilation error.
#include <iostream>
using namespace std;
struct Student{
   char stuName[30];
   int stuRollNo;
   int stuAge;
};
void printStudentInfo(Student);
int main(){
   Student s;
   cout<<"Enter Student Name: ";
   cin.getline(s.stuName, 30);
   cout<<"Enter Student Roll No: ";
   cin>>s.stuRollNo;
   cout<<"Enter Student Age: ";
   cin>>s.stuAge;
   printStudentInfo(s);
   return 0;
}
void printStudentInfo(Student s){
   cout<<"Student Record:"<<endl;
   cout<<"Name: "<<s.stuName<<endl;
   cout<<"Roll No: "<<s.stuRollNo<<endl;
   cout<<"Age: "<<s.stuAge;
}
Output:
Enter Student Name: Rick
Enter Student Roll No: 666123
Enter Student Age: 19
Student Record:
Name: Rick
Roll No: 666123
Age: 19


Array of Structures


We can also make an array of structures. In the first example in structures, we stored the data of 3 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(){ struct 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;
}


Pointers to Structures


Like we have pointers to int, char and other data-types, we also have pointers pointing to structures. These pointers are called structure pointers.
Now, how to define a pointer to a structure? The answer is below:
struct structure_name {     data-type member-1;     data-type member-1;     data-type member-1;     data-type member-1; }; int main() {     struct structure_name *ptr; }
Let's see an example using structure pointer.
#include <iostream>
#include <cstring>

using namespace std;

struct student
{
  string name;
  int roll_no;
};

int main(){

 struct student stud = {"Sam",1};
   struct student *ptr;
   ptr = &stud;

   cout << stud.name << stud.roll_no << endl;
   cout << ptr->name << ptr->roll_no << endl;
 return 0;
}

No comments:

Post a Comment

Directory implementation

The selection of directory-allocation and directory-management algorithms significantly affects the efficiency, performance, and reliabil...