This section delves into fundamental C++ concepts: structures, pointers, and arrays. While C++ inherits many of these ideas from C, it introduces powerful enhancements, particularly in the realm of structures.
C++ Structures
In C++, structures (struct) build upon their C counterparts by allowing the inclusion of member functions (methods) alongside member variables. This capability makes C++ structures more object-like, enabling them to encapsulate both data and behavior. Unlike C, where structures primarily served as data aggregates, C++ structures can define operations that act upon they own data.
Example: Structure with a Member Function
Consider a simple structure to represent an individual's profile, including their name, age, and a method to describe an activity.
#include <iostream>
#include <string> // For std::string
// Define a structure named 'IndividualProfile'
struct IndividualProfile
{
std::string fullName; // Member variable for the individual's full name
int currentAge; // Member variable for age
double academicScore; // Member variable for a score
// Member function to log an activity
void performActivity() const // 'const' indicates the method does not modify the object's state
{
std::cout << fullName << " is currently engaged in learning activities." << std::endl;
}
};
int main()
{
// Create an instance of IndividualProfile
IndividualProfile studentOne;
studentOne.fullName = "Alice Smith";
studentOne.currentAge = 20;
studentOne.academicScore = 95.5;
// Call the member function
studentOne.performActivity();
return 0;
}
Expected Output:
Alice Smith is currently engaged in learning activities.
Pointers and Arrays in C++
C++ largely maintains the pointer and array mechanics established in C, offering direct memory manipulation capabilities. An array name can often be treated as a pointer to its initial element, facilitating pointer arithmetic for traversing array elements efficiently. This allows for flexible and performant access to contiguous data blocks.
Example: Managing Student Records with Pointers and Arrays
This example demonstrates how to use an array of structures, combined with pointer arithmetic, to input and display details for multiple students.
#include <iostream>
#include <string> // For std::string
// No need for <stdio.h>, <string.h>, <stdlib.h> for this C++ example
// Reusing the structure, perhaps with minor adjustments for context
struct LearnerRecord {
std::string learnerName;
int learnerAge;
double finalGrade;
// A method to describe the learner's status
void describeStatus() const {
std::cout << learnerName << " is a dedicated learner." << std::endl;
}
};
int main()
{
const int RECORD_COUNT = 3;
// Declare an array of LearnerRecord structures
LearnerRecord studentRegistry[RECORD_COUNT];
// Declare a pointer to LearnerRecord, initialized to the beginning of the array
LearnerRecord* currentRecordPtr = studentRegistry;
// Input student information using pointer arithmetic
std::cout << "--- Enter Student Information ---" << std::endl;
for (int i = 0; i < RECORD_COUNT; ++i)
{
std::cout << "Student " << (i + 1) << " Name: ";
std::cin >> currentRecordPtr->learnerName;
std::cout << "Student " << (i + 1) << " Age: ";
std::cin >> currentRecordPtr->learnerAge;
std::cout << "Student " << (i + 1) << " Grade: ";
std::cin >> currentRecordPtr->finalGrade;
std::cout << std::endl; // Add a newline for better formatting
// Move the pointer to the next element in the array
currentRecordPtr++;
}
// Reset the pointer to the beginning of the array for output
currentRecordPtr = studentRegistry;
// Display student information and invoke their status method
std::cout << "--- Displaying Student Records ---" << std::endl;
for (int i = 0; i < RECORD_COUNT; ++i)
{
std::cout << "Student " << (i + 1) << " Details:" << std::endl;
std::cout << " Name: " << currentRecordPtr->learnerName << std::endl;
std::cout << " Age: " << currentRecordPtr->learnerAge << std::endl;
std::cout << " Grade: " << currentRecordPtr->finalGrade << std::endl;
currentRecordPtr->describeStatus(); // Call the member function
std::cout << std::endl; // Add a newline for separation
// Advance the pointer to the next record
currentRecordPtr++;
}
return 0;
}
Example Interactive Session and Output:
--- Enter Student Information ---
Student 1 Name: John
Student 1 Age: 19
Student 1 Grade: 88.5
Student 2 Name: Jane
Student 2 Age: 20
Student 2 Grade: 92.0
Student 3 Name: Mike
Student 3 Age: 18
Student 3 Grade: 79.3
--- Displaying Student Records ---
Student 1 Details:
Name: John
Age: 19
Grade: 88.5
John is a dedicated learner.
Student 2 Details:
Name: Jane
Age: 20
Grade: 92.0
Jane is a dedicated learner.
Student 3 Details:
Name: Mike
Age: 18
Grade: 79.3
Mike is a dedicated learner.