struct in C and class in C++
Model related state and behavior.
What is struct in C and class in C++?
C structs group data; C++ classes can enforce invariants by combining private state with member functions.
Important points
- State the invariants before implementing operations.
- Handle empty, full, and allocation-failure cases.
- Measure time complexity and memory ownership together.
C and C++ code examples
#include <stdio.h>
typedef struct {
const char *owner;
double balance;
} Account;
void deposit(Account *account, double amount) {
if (amount > 0) {
account->balance += amount;
}
}
int main(void) {
Account account = {"Ada", 100.0};
deposit(&account, 25.0);
printf("%s: %.2f\n", account.owner, account.balance);
return 0;
}
Ada: 125.00
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
class Account {
std::string owner_;
double balance_;
public:
Account(std::string owner, double balance)
: owner_(std::move(owner)), balance_(balance) {}
void deposit(double amount) {
if (amount > 0) {
balance_ += amount;
}
}
void print() const {
std::cout << owner_ << ": " << std::fixed
<< std::setprecision(2) << balance_ << '\n';
}
};
int main() {
Account account("Ada", 100.0);
account.deposit(25.0);
account.print();
}
Ada: 125.00
C and C++ comparison
The C design passes a struct pointer to functions. The C++ class keeps balance private and exposes operations, making invalid direct state changes harder.
Structures and operations are usually separate and allocation is explicit.
Classes and containers can preserve invariants and manage resources automatically.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add empty and capacity-boundary tests.
- Implement cleanup and verify no leaks remain.
- Compare operation complexity with a standard container.
struct in C and class in C++ at a glance
struct Point { int x; int y; }; | C: define a struct (note the semicolon) |
struct Point p = {1, 2}; | C: create and initialize a struct |
struct Point p = {.x = 1, .y = 2}; | C: designated initializers |
typedef struct { int x, y; } Point; | C: use Point without writing struct each time |
p.x, ptr->x | Access a member directly, or through a pointer |
Point p{1, 2}; | C++: create a struct with brace initialization |
class Account { public: ... private: ... }; | C++: a class with public methods and private data |
Point(int x, int y) : x(x), y(y) {} | C++: a constructor with an initializer list |
struct vs class | C++: struct members are public by default, class members are private by default |
Common mistakes with struct and class
Forgetting the semicolon after the definition
A struct or class definition ends with }; and leaving out the semicolon causes confusing errors on the next line.
struct Point {
int x;
int y;
}
struct Point {
int x;
int y;
};
Leaving out struct in C
In C, the type name is struct Point, not Point, unless you add a typedef.
struct Point { int x; int y; };
Point p = {1, 2}; /* error in C */
struct Point { int x; int y; };
struct Point p = {1, 2};
Using . with a pointer
Through a pointer, use -> (or (*ptr).x).
struct Point *ptr = &p;
ptr.x = 5; /* error */
struct Point *ptr = &p;
ptr->x = 5;
Comparing structs with == in C
C has no == for structs. Compare the members you care about.
if (a == b) { /* error in C */ }
if (a.x == b.x && a.y == b.y) { /* equal */ }
Frequently asked questions
What is the difference between a struct and a class in C++?
Only the default access: struct members and base classes are public by default, class members are private by default. Both can have constructors, methods and inheritance. By convention, struct is used for simple data and class for types with behavior.
Can a struct have functions in C?
No, C structs hold only data. You can store function pointers in a struct, or write functions that take a pointer to the struct, which is how C code imitates methods.
What does typedef struct do?
It gives the struct a type name, so you can write Point p; instead of struct Point p; in C. C++ does not need it.
Why is sizeof(struct) bigger than the sum of its members?
The compiler may add padding between members so each one sits at an address the processor can read efficiently. Ordering members from largest to smallest often reduces the padding.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges