- Used to represent abstract data types; user-defined types; very similar to structures
- Presents an interface to the data; provides functions for accessing the data
- The actual data representation is hidden from the user; a black box
- Prevents users from corrupting data with invalid values by restricting access to the data members
- Allows the implementation to change without affecting any existing user code (also called client code)
struct EmployeeA // EmployeeA is a new type
{
char firstName[10]; // first name is a char array
char lastName[12]; // last name is char array
float salary; // yearly pay is float
int years; // years of service
};
class EmployeeB // EmployeeB is a new type
{
char firstName[10]; // first name is a char array
char lastName[12]; // last name is char array
float salary; // yearly pay is float
int years; // years of service
};
EmployeeA empA; // variable of type EmployeeA
EmployeeB empB; // variable of type EmployeeB
// called a class object
empA.salary = 45200.0f; // ok, can access members
empB.salary = 45200.0f; // error, can't access members