Structure in C++:
- Structure is a user-defined data type.
- The format of the structure is known as template and we can create multiple structure variables from template.
Difference between C Structure and C++ Structure:
- In C structure contains only variable while in C++ structure contains variable as well as function both.
- In C++ programming the format of structure is as follow:
return_value function_name(argument1, argument2, …)
- In C programming language we cannot perform an arithmetic operation (addition, multiplication, division, subtraction, module) on structure variable but in C++ we can perform by using operator overloading.
c3 = c1 + c2; // illegal in C but legal in C++ by operator overloading.
- The variable of structure can be declared by using keyword ‘struct’ and structure tag name in C program.
- But in C++ we can declare the structure variable by using directly tag name without keyword ‘struct’.
struct student s1,s2,s3; // C technique (with keyword ‘struct’) student s1,s2,s3; // C++ technique (without keyword ‘struct’)
- In C++ we can also inherit (create one structure from another structure) the structure.
- In short in C++ programming structure is similar to class but only difference is that by default all the variable and function of class are private while in structure it is public.