Coming Soon
This lesson is currently being developed
Introduction to program-defined (user-defined) types
Learn to create your own custom data types.
What to Expect
Comprehensive explanations with practical examples
Interactive coding exercises to practice concepts
Knowledge quiz to test your understanding
Step-by-step guidance for beginners
Development Status
Content is being carefully crafted to provide the best learning experience
Preview
Early Preview Content
This content is still being developed and may change before publication.
13.1 — Introduction to program-defined (user-defined) types
In this lesson, you'll learn about program-defined types (also called user-defined types) - custom data types that you can create to better model the data in your programs.
What are program-defined types?
So far, you've been working with fundamental types like int
, double
, bool
, and char
. These types are built into the C++ language and represent basic values. However, real-world programs often need to work with more complex data that doesn't fit neatly into these basic types.
Program-defined types (also called user-defined types) are custom data types that you create to represent specific concepts in your program. They allow you to:
- Group related data together
- Create meaningful names for sets of values
- Model real-world entities more accurately
- Make your code more readable and maintainable
Think of program-defined types like creating your own custom containers or categories for data, just as you might organize physical items into labeled boxes or folders.
Why do we need program-defined types?
Consider a simple example: representing a playing card. Using only fundamental types, you might do something like this:
#include <iostream>
int main()
{
// Representing a playing card with fundamental types
int suit = 1; // 1=hearts, 2=diamonds, 3=clubs, 4=spades
int rank = 13; // 1=ace, 11=jack, 12=queen, 13=king
std::cout << "Card: " << rank << " of suit " << suit << std::endl;
return 0;
}
Output:
Card: 13 of suit 1
This approach has several problems:
- Not intuitive: What does
suit = 1
mean? You have to remember the mapping. - Error-prone: Nothing prevents you from setting
suit = 99
orrank = -5
. - Hard to read: The code doesn't clearly express what the numbers represent.
- Difficult to maintain: If you want to change the numbering scheme, you have to update code throughout your program.
Categories of program-defined types
C++ provides several ways to create program-defined types:
1. Enumerations (enums)
Create named constants that represent a set of possible values:
#include <iostream>
enum CardSuit
{
hearts = 1,
diamonds = 2,
clubs = 3,
spades = 4
};
int main()
{
CardSuit suit = hearts;
std::cout << "Suit: " << suit << std::endl; // Prints: 1
return 0;
}
Output:
Suit: 1
2. Structures (structs)
Group related variables together:
#include <iostream>
struct PlayingCard
{
int suit;
int rank;
};
int main()
{
PlayingCard card;
card.suit = 1; // hearts
card.rank = 13; // king
std::cout << "Card: " << card.rank << " of suit " << card.suit << std::endl;
return 0;
}
Output:
Card: 13 of suit 1
3. Classes
Similar to structs but with additional features for encapsulation and member functions (covered in later chapters).
Benefits of program-defined types
Let's see how program-defined types improve our playing card example:
#include <iostream>
enum CardSuit
{
hearts = 1,
diamonds,
clubs,
spades
};
enum CardRank
{
ace = 1,
two, three, four, five, six, seven, eight, nine, ten,
jack, queen, king
};
struct PlayingCard
{
CardSuit suit;
CardRank rank;
};
int main()
{
PlayingCard card;
card.suit = hearts;
card.rank = king;
std::cout << "Card: " << card.rank << " of " << card.suit << std::endl;
return 0;
}
Output:
Card: 13 of 1
Now our code is much clearer:
- Self-documenting:
card.suit = hearts
is much clearer thansuit = 1
- Type-safe: The compiler helps prevent invalid assignments
- Maintainable: Changes to the internal representation don't affect code that uses the types
- Organized: Related concepts are grouped together logically
Real-world examples
Here are some common scenarios where program-defined types are useful:
RGB Color representation
#include <iostream>
struct RGBColor
{
int red;
int green;
int blue;
};
int main()
{
RGBColor backgroundColor;
backgroundColor.red = 255;
backgroundColor.green = 255;
backgroundColor.blue = 255; // White background
std::cout << "RGB(" << backgroundColor.red << ", "
<< backgroundColor.green << ", "
<< backgroundColor.blue << ")" << std::endl;
return 0;
}
Output:
RGB(255, 255, 255)
Game directions
#include <iostream>
enum Direction
{
north,
south,
east,
west
};
int main()
{
Direction playerDirection = north;
std::cout << "Player is facing: " << playerDirection << std::endl;
return 0;
}
Output:
Player is facing: 0
Student information
#include <iostream>
#include <string>
struct Student
{
std::string name;
int age;
double gpa;
};
int main()
{
Student student;
student.name = "Alice Johnson";
student.age = 20;
student.gpa = 3.8;
std::cout << "Student: " << student.name
<< ", Age: " << student.age
<< ", GPA: " << student.gpa << std::endl;
return 0;
}
Output:
Student: Alice Johnson, Age: 20, GPA: 3.8
Key concepts to remember
-
Program-defined types help model real-world concepts more accurately than fundamental types alone.
-
Enums are good for representing named constants or a fixed set of possible values.
-
Structs are good for grouping related data together into a single unit.
-
Program-defined types make code more readable by using meaningful names instead of magic numbers.
-
They provide better type safety by preventing invalid operations and assignments.
What's coming next
In the following lessons, you'll learn about:
- How to create and use enumerations effectively
- Working with structures to group related data
- Advanced features like scoped enumerations and class templates
- Best practices for designing your own types
Program-defined types are a fundamental building block of effective C++ programming. They help you write code that's more expressive, safer, and easier to maintain.
Summary
Program-defined types allow you to create custom data types that better represent the concepts in your program. Instead of relying only on fundamental types like int
and double
, you can create enumerations for named constants and structures to group related data. This makes your code more readable, maintainable, and less error-prone.
Quiz
- What are the main problems with using only fundamental types for complex data?
- What are the two main categories of program-defined types covered in this lesson?
- How do enumerations improve code readability?
- When would you use a struct instead of separate variables?
- What are the main benefits of using program-defined types?
Practice exercises
Try creating these program-defined types:
- Create an enumeration for the days of the week and write a program that prints the current day.
- Create a struct to represent a point in 2D space (with x and y coordinates) and create two points.
- Create an enumeration for traffic light colors and a program that cycles through them.
- Create a struct to represent a book (with title, author, and page count) and initialize one with your favorite book.
Explore More Courses
Discover other available courses while this lesson is being prepared.
Browse CoursesLesson Discussion
Share your thoughts and questions