Structure in C++

Structure in C++

1. Structure is a collection of dissimilar elements.
2. Structure is a user-defined datatype in C/C++.
3. Structure is a way to group variables.
4. Structure is used to create datatype.
5. The 'struct' keyword is used to create a structure in C/C++.
6. Structure can contain two types of members inside it (Data member and member functions).


#include<string.h>
struct Book
{
  int Bookid;
  char title[20];
  float price;
}; Book b2;   // b2 is a global variable

int main()
{
  // In C++ we may or may not need to write struct before Book b1
  struct Book b1 = {100, "Learning Mania", 430.50};
  
  Book b3;
  b3.Bookid = 101;
  
  strcpy(b3.title, "C++ by Learning Mania");
  b3.price=300.0;
}

How to get data from user in Structure

#include<iostream>
using namespace std;

struct Book
{
  int Bookid;
  char title[20];
  float price;
};

int main()
{
  Book b1;
  
  cout << "Enter Book's id: ";
  cin >> b1.Bookid;
  cout << endl;
  
  cout << "Enter Book's Title: ";
  cin >> b1.title;
  cout << endl;
  
  cout << "Enter Book's Price: ";
  cin >> b1.price;
  cout << endl;
  
  cout << endl;
  
  cout << b1.Bookid << " " << b1.title << " " << b1.price;
}
Output:
Enter Book's id: 11
Enter Book's Title: C++byLearningMania 
Enter Book's Price: 251

11 C++byLearningMania 251

How to get data using function

#include<iostream>
using namespace std;

struct Book
{
  int Bookid;
  char title[20];
  float price;
};

Book input()
{
  Book B;
  
  cout << "Enter Book's id: ";
  cin >> B.Bookid;
  cout << endl;
  
  cout << "Enter Book's Title: ";
  cin >> B.title;
  cout << endl;
  
  cout << "Enter Book's Price: ";
  cin >> B.price;
  cout << endl;
  
  return B;
}

void Display(Book b)
{
  cout << "\n" << b.Bookid << " " << b.title << " " << b.price;
}

int main()
{
  Book b1;
  b1 = input();
  Display(b1);
}
Output:
Enter Book's id: 11
Enter Book's Title: C++byLearningMania 
Enter Book's Price: 251

11 C++byLearningMania 251

Points to remember in Structure in C/C++

In C language, functions cannot be written inside the structure block but in C++ structure, functions can be written inside the structure block.

For example, function Display() and input() in the above example can be written inside the structure block as shown in the example given below.

#include<iostream>
using namespace std;

struct Book
{
  int Bookid;
  char title[20];
  float price;
  
  void input()
  {
    cout << "Enter Book's id: ";
    cin >> Bookid;
    cout << endl;
  
    cout << "Enter Book's Title: ";
    cin >> title;
    cout << endl;
  
    cout << "Enter Book's Price: ";
    cin >> price;
    cout << endl;
  }
  
  void Display()
  {
    cout << "\n" << Bookid << " " << title << " " << price;
  }
};

int main()
{
  Book b1;
  b1.input();
  b1.Display();
}
Output:
Enter Book's id: 11
Enter Book's Title: C++byLearningMania 
Enter Book's Price: 251

11 C++byLearningMania 251

Here, struct Book has 5 members (3 member variables + 2 member functions).

Member functions can directly access its own structure member without dot operator.

In C++, Unlike Class, structure is also a way to achieve encapsulation.

In C language, we cannot use access specifiers (like private, public and protected) inside structure but in C++, we can use access specifiers if we want.

Example:
#include<iostream>
using namespace std;

struct Book
{
  int Bookid;
  char title[20];
  float price;
 
public: 
  void input()
  {
    cout << "Enter Book's id: ";
    cin >> Bookid;
    cout << endl;
  
    cout << "Enter Book's Title: ";
    cin >> title;
    cout << endl;
  
    cout << "Enter Book's Price: ";
    cin >> price;
    cout << endl;
  }
  
  void Display()
  {
    cout << "\n" << Bookid << " " << title << " " << price;
  }
};

int main()
{
  Book b1;
  b1.input();
  b1.Display();
}
Output:
Enter Book's id: 11
Enter Book's Title: C++byLearningMania 
Enter Book's Price: 251

11 C++byLearningMania 251

Difference between Structure in C and C++

In C++, Unlike Class, structure is also a way to achieve encapsulation.

In C language, we cannot use access specifiers (like private, public and protected) inside structure but in C++, we can use access specifiers if we want.

Difference between Class and Structure in C++

The only difference between class and structure in C++ is that the members of structures are by default public and the members of class are by default private.



<< Previous                                                                                      Next >>
Previous
Next Post »