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()
{
struct Book b1 = {100, "Learning Mania", 430.50}; // In C++ we may or may not need to write struct before Book b1
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 bookid, title and price of the book";
cin>>b1.Bookid>>b1.title>>b1.price;
cout<<b1.Bookid<<b1.title<<b1.price;
}


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 bookid, title and price of the book";
cin>>b.Bookid>>b.title>>b.price;
return b;
}

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

int main()
{
Book b1;
b1 = input();
Display(b1);
}



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 bookid, title and price of the book";
cin>>b.Bookid>>b.title>>b.price;
}

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

int main()
{
Book b1;
b1.input();
b1.Display();
}

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
{
private:
int Bookid;
char title[20];
float price;

public:
void input()
{
cout<<"Enter bookid, title and price of the book";
cin>>b.Bookid>>b.title>>b.price;
}

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


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 »