Difference Between C Structures And C++ Structures

C and C++ are two computer language mainstays that have maintained their appeal throughout time. Both languages have strong characteristics for developing software, and programmers must be able to distinguish between the subtle differences between them. One such area where variations occur is in the way structures are handled. In this article, we will examine the differences between C and C++ structures to provide insight into how each language handles this essential programming idea.

C Structures: The Basis:

The early 1970s saw the creation of C, a procedural programming language. In C, structures offer a means of combining related data under a single name. They consist of variables, also referred to as members or fields, representing various data kinds. In contrast to classes in C++, structures in C cannot encapsulate methods and lack member functions.

Declaration and Syntax:

Declaring a structure in C follows a simple syntax. A structure declaration, which defines the architecture for a user-defined data type, usually exists outside of any function. As an illustration, consider this:

Example

struct Student {
 char name[50];
 int age;
 float grade;
};

Declare a variable in a structure:

struct Student s1;

Let's take a C program to show how to use structures. For this example, we'll construct a structure named Person to hold personal data like name, age, and height. After that, the information on two people will be created and displayed using this structure.

Program:

Let us take an example to illustrate the use of structure in C.

Example

Example

#include <stdio.h>
// Define the structure
struct Person {
 char name[50];
 int age;
 float height;
};
int main() {
 // Declare variables of type Person
 struct Person person1, person2;
 // Input information for the first person
 printf("Enter information for person 1:\n");
 printf("Name: ");
 scanf("%s", person1.name);
 printf("Age: ");
 scanf("%d", &person1.age);
 printf("Height (in meters): ");
 scanf("%f", &person1.height);
 // Input information for the second person
 printf("\nEnter information for person 2:\n");
 printf("Name: ");
 scanf("%s", person2.name);
 printf("Age: ");
 scanf("%d", &person2.age);
 printf("Height (in meters): ");
 scanf("%f", &person2.height);
 // Display information for both persons
 printf("\nInformation for person 1:\n");
 printf("Name: %s\n", person1.name);
 printf("Age: %d\n", person1.age);
 printf("Height: %.2f meters\n", person1.height);
 printf("\nInformation for person 2:\n");
 printf("Name: %s\n", person2.name);
 printf("Age: %d\n", person2.age);
 printf("Height: %.2f meters\n", person2.height);
 return 0;
}

Output:

Explanation:

The code is explained as follows:

  • In this example, three components make up the Person structure: name (string), age (integer), and height (float).
  • Person1 and person2, two variables of type Person, are declared in the main function.
  • The printf and scanf methods are utilized to input data from the user for both parties.
  • Lastly, we use printf to show the data for both individuals.
  • C++ Structures:

Object-oriented programming features were introduced by Bjarne Stroustrup in the 1980s with the development of C++, which is an extension of C. Structures were redesigned in C++ to include several characteristics that bring them closer to classes.

Declaration and Syntax:

In C++, structures, also known as "structs" can contain both data and member functions. The syntax for declaring a struct in C++ is similar to that of C, using the struct keyword followed by the struct's name and its body enclosed in curly braces {} .

Example

struct Student {
 char name[50];
 int age;
 float grade;
 void displayInfo() {
 // Function to display information about the student
 }
};

Declaring a variable in a C++ structure:

Student s1;

The following is an instance of C++ program. In this instance, we'll construct a structure called Person to hold personal data about a person, including their name, height, and age. We'll also provide information using a member function in the structure.

Program:

Let us take an example to illustrate the use of structure in C++.

Example

Example

#include <iostream>
#include <string>
// Define the structure
struct Person {
 std::string name;
 int age;
 float height;
 // Member function to display information
 void displayInfo() {
 std::cout << "Name: " << name << "\n";
 std::cout << "Age: " << age << "\n";
 std::cout << "Height: " << height << " meters\n";
 }
};

int main() {
 // Declare variables of type Person
 Person person1, person2;
 // Input information for the first person
 std::cout << "Enter information for person 1:\n";
 std::cout << "Name: ";
 std::cin >> person1.name;
 std::cout << "Age: ";
 std::cin >> person1.age;
 std::cout << "Height (in meters): ";
 std::cin >> person1.height;
 // Input information for the second person
 std::cout << "\nEnter information for person 2:\n";
 std::cout << "Name: ";
 std::cin >> person2.name;
 std::cout << "Age: ";
 std::cin >> person2.age;
 std::cout << "Height (in meters): ";
 std::cin >> person2.height;
 // Display information for both persons using member function
 std::cout << "\nInformation for person 1:\n";
 person1.displayInfo();
 std::cout << "\nInformation for person 2:\n";
 person2.displayInfo();

 return 0;
}

Output:

Explanation:

The code is explained as follows:

  • In this example, three components make up the Person structure: name (string), age (integer), and height (float) .
  • The member function displayInfo is included inside the structure and is responsible for displaying personal data.
  • Person1 and person2, two variables of type Person, are declared in the main function.
  • After that, we performs input and output operations using std::cout and std::cin .
  • The application asks the user to submit data for two people and then uses the displayInfo member method to show the data entered for each person.
  • Differences between C Structures and C++ Structures:

There are several differences between the C Structure and C++ Structure . Some main differences between the C Structure and C++ Structure are as follows:

Member Purposes:

C Structures: In C, structures are limited to storing data members or variables and they are incapable of incorporating member functions.

C++ Structures: It is often called "structs" . C++ structures are capable of having member functions. It makes it possible for C++ structures to encapsulate data and behavior, making distinguishing between classes and structures harder.

Control of Access:

C Structures: By default, every member of a C structure has public access. Access specifiers such as public and private do not exist.

C++ Structures: C++ structures support public, private, and protected access specifiers. By default, members of structures are private which promotes data hiding and encapsulation. Developers can specifically specify access levels of members.

Memory Handling:

C Structures: C's structures make managing memory easier. Memory allocation and release for structure variables are the responsibilities of programmers.

C++ Structures: Similar to classes, C++ structures can include constructors and destructors that can use dynamic memory allocation and deallocation to automate memory management.

Default Access for Members:

C Structures: By default, all members of C structures have open access. By default, there is no notion of private or protected members.

C++ Structures: Members of a structure in C++ have private access by default. Developers must explicitly utilize the public: access specifier to make them public.

Encapsulation:

C Structures: Encapsulation is not present in C structures. They are merely data containers with no built-in means of combining data and behaviour.

C++ Structures: C++ structures allow encapsulation by incorporating member functions and access control. It makes it possible to combine data with related processes into a single entity.

It is essential to comprehend these distinctions when switching between C and C++ or deciding which language is best for a project. C++ structures offer characteristics that extend beyond the fundamental data-organizing capabilities of C structures, giving a more thorough and object-oriented approach.

Input Required

This code uses input(). Please provide values below: