C++ structure
structure (struct) are used to combine multiple data into single unit. That is reliable to create user defined data type. c++ are allowing to use data members and member function within the structures. there are declared using of this syntax.
struct tag_name{
//data_members;
//functions(){}
}variable_list;
Key points | Description |
---|---|
struct | keyword |
tag_name | name of structure |
data_member | Use predefined and user defined data |
functions | structure functions |
variable_list | Variable of structure |
for example
//structure of Book
struct Books{
char name[30],
author[30],
serial_number[20];
int pages;
float weight,price;
}book;
//structure of Employee
struct Employee{
char first_name[30],
middle_name[30];
int age;
float salary;
}employee;
In above we are declare two structures Book and employee. struct is an keyword followed by its name. Curly braces are used to defined its body and this are terminated by semicolon. inside of structure declare variables and function those are used by structure variable and pointer. let view one example.
#include<iostream>
#include <string>
using namespace std;
//structure of Book
struct Book{
string name,
author,
serial_number;
int pages;
float weight,price;
}book;
int main(){
//assign Book structure variable book values
book.name="C++ Programming";
book.author="Me and You";
book.serial_number="oo7";
book.pages=101;
book.weight=250.59f;//gm
book.price=1000.12f;
//display values
cout<<"Display Book information"<<endl;
cout<<"name :"<<book.name<<endl;
cout<<"author :"<<book.author<<endl;
cout<<"pages :"<<book.pages<<endl;
cout<<"serial_number :"<<book.serial_number<<endl;
cout<<"weight :"<<book.weight<<endl;
cout<<"price :"<<book.price<<endl;
return 0;
}
Output
Display Book information
name :C++ Programming
author :Me and You
pages :101
serial_number :oo7
weight :250.59
price :1000.12
Note that help of struct variable and dot operator to access the data member value.
Why use structure
why use structure there question are arise in your mind. they are really useful to combine predefined and user defined data. for example array are used to store similar data. but array are not capable to store multiple different data. like student information (name,address,age,enrollment and so on). structures are suitable in this case.
Array of structure
When we declare an structure of our program then there are possible to create array of structure variables. for example suppose we are create an student structure then we can create array of structure in using of this syntax.
struct_variable[size_of_array];
Let view an example.
//example of structure
#include<iostream>
#include <string>
using namespace std;
//structure of Book
struct Student{
string name;
int age;
};
int main(){
//record is Array of 3 Students
Student record[3];
//you can use loop and insert values
//array of record[0]
record[0].name="foo";
record[0].age=19;
//array of record[1]
record[1].name="bar";
record[1].age=21;
//array of record[2]
record[2].name="woo";
record[2].age=20;
//display all record structure values
for(int index=0;index<3;index++){
cout<<"record["<<index<<"]"<<endl;
cout<<"name: "<<record[index].name<<" age: "<<record[index].age<<endl;
}
return 0;
}
Output
record[0]
name: foo age: 19
record[1]
name: bar age: 21
record[2]
name: woo age: 20
structure of functions in c++
There are possible to defined function inside a structure in c++ programming. There is new features this are not possible in c programming.
//example of structure function
#include<iostream>
#include <string>
using namespace std;
//structure of Calculate
struct Calculate{
int result;
/*----
sum is structure function
accepts two integers value.
*/
void sum(int num1,int num2){
result=num1+num2;
cout<<"Sum ("<<num1<<"+"<<num2<<") :"<<result<<endl;
}
};
int main(){
//Create structure variables
Calculate operation;
//access structure function
operation.sum(12,32);
operation.sum(10,20);
return 0;
}
Output
Sum (12+32) :44
Sum (10+20) :30
Note that using dot(.) operator access the structure function.
structure Vs class
No | Class | Structure (struct) |
---|---|---|
1 | class data member and member functions are by default is private. | Structure data member and member functions are by default is public. |
2 | Inheritance are possible in class. | There are no inheritance. |
3 | Can't declare one class inside another class. | structure are allowed to create multiple structure inside a single structure. |
Nested structure
C++ are allowed to define one structure inside another structure. This process is called nested structure.help of nested structure we can solve complex task in very efficiently.
struct outer{
//data member & functions
struct inner{
//data member & functions
}inner_var;
}outer_var;
look at view example
//structure of Company
struct Company{
//data member
int company_code;
/*company has department section*/
struct departments
{ //data member
int department_id;
/*department has sale section*/
struct sales
{
//logical code
}sale;
/*department has sale section*/
struct accounts
{
//defined operation and logic
}account;//variable of structure
}department;
struct employees
{ //function of employee structure
void salary(int id){
//logic code
}
}employee;
}my_company;
View a program to access struct data member and functions.
//example of nested structure in c++
#include<iostream>
using namespace std;
//structure of Company
struct Company{
//data member
int company_code;
/*company has department section*/
struct departments
{ //data member
int department_id;
/*department has sale section*/
struct sales
{
void sales_list(){
cout<<"This is sales_list function"<<endl;
}
//logical code
}sale;
/*department has sale section*/
struct accounts
{
void get_amount(){
cout<<"This is get_amount function"<<endl;
}
//defined operation and logic
}account;//variable of structure
}department;
struct employees
{ //function of employee structure
void salary(int id){
cout<<"This is get_amount function"<<endl;
}
}employee;
}my_company;
int main(){
/*
access nested member function using of structure
variables and dot operator. let example.
*/
my_company.department.sale.sales_list();
my_company.department.department_id=10;
cout<<"department_id :"<<my_company.department.department_id<<endl;
//try to access other function
return 0;
}
Output
This is sales_list function
department_id :10
There are most important to declare inner structure variable. because help of structure variable there are possible to access all data members and function.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment