Coding/Learn/cpp/structs and objects
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnCustom Data Types: Structs & OOP Basics
19 / 20 0%
Lesson 19 of 20
5 mins read

Custom Data Types: Structs & OOP Basics

PreviousNext Lesson
In Plain English

Real-world data has multiple attributes: a User has a name, email, and age; a GameCharacter has health, speed, and rank. A struct allows you to create your own custom composite data type bundling multiple variables together into a single blueprint.

The Mental Model

"If primitive types (int, string) are individual ingredients (sugar, flour), a struct is a pre-packaged cake mix box containing all the right ingredients measured out together under one name."

Deep Dive: How It Works

struct Blueprint: struct Character { std::string name; int health; int level; }; (Note the mandatory semicolon ; after the closing brace!).

Creating Instances: Character hero = {"Valkyrie", 100, 5};.

Dot Operator (.): Access or modify members: hero.health -= 20;.

Arrow Operator (->): When accessing members through a pointer to a struct: ptr->health is shorthand for (*ptr).health.

Syntax Blueprint

struct Player {
  std::string name;
  int score;
};

Player p1 = {"Neo", 9000};
std::cout << p1.name << ": " << p1.score;

Define blueprint with struct, instantiate, and access fields with dot operator.

PointCore Rules to Remember

Point star
The Closing Semicolon: Never forget the semicolon at the end of a struct declaration: struct Box { };.
Point star
Dot Notation (.): Access individual fields inside the struct.
Point star
Foundation of OOP: In C++, a struct and a class are identical, except struct members default to public.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting the semicolon after struct closing brace: struct Item { int id; }

Why it happens: Functions don't end with semicolons, so beginners omit it on structs.

How to fix: Always write }; at the end of struct definitions.

Pro Tip: Pass structs to functions by const reference (void render(const Character &c)) to avoid copying data!

Live Interactive Example

Hit Run Code to see it live
Building a Character Struct
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Output Console
Click "Run Code" to view the rendered output.
How it works: The Server struct packages 4 fields cleanly into one cohesive object.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a Product struct

  • PointDefine struct Product { std::string title; double price; };
  • PointIn main(), create Product p = {"Mechanical Keyboard", 89.99};
  • PointPrint "Product: Mechanical Keyboard | Price: $89.99".
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates