Unit 05: Structs

  1. Objects
  2. Anatomy of a struct
  3. Creating an object variable
  4. Program design: Multiple files

  5. Example code and additional resources

This week's stuff:

Structs

Objects

Object Oriented Programming is one of the most common styles of programming these days, and is a big part of how C++, C#, and Java-based programs are designed.

Defining classes and structs in our programs are a way that we can create our own data types for our variables. When we define our own structures, we can create them with internal variables and functions available to them. The variables we create, whose data types come from a defined class or struct, is known as an object.

Design-wise, the idea is to take real-world objects and find a way to represent them in a computer program as an object that has attributes (variables) and functionality (functions).


For now, we're just goign to be creating structs, and only put variables within them. This will help us get some practice with basic objects before moving on to classes and more sophisticated designs later in the course.

Anatomy of a struct

A struct takes the form:

struct NAME
{
	DATATYPE1 VAR1;
	DATATYPE2 VAR2;
	DATATYPE3 VAR3;
};

Where we create a struct "NAME" and give it member variables within. This groups these variables together in this container. Design-wise, this shows that these attributes are related.


Example menu item, like at a restaurant:

struct MenuItem
{
  string name;
  float price;
  int calories;
  bool isVeggie;
};

NOTE that a struct declaration must have a semicolon on its closing curly brace!

Creating an object variable

Once we've declared a struct we can declare variables with that struct as our data type:

MenuItem food1;

And since our struct contains inner member variables, we can assign values to those as well. We just need to use the object's name, and then the member variable name:

food1.name = "Bean Burrito";
food1.price = 1.99;
food1.calories = 350;
food1.isVeggie = true;

Program design: Multiple files

  • Struct declarations should go in .h files, another type of C++ file called a "Header File".

  • When building with g++, you'll want to include all your files, but a shortcut to do this is to use the * wildcard:
    g++ *.h *.cpp

  • Something you need to require in your .h files that isn't needed for your .cpp files are file guards. These prevent the compiler from reading the file more than once. If it reads it multiple times, it will think you're redeclaring things over and over again. All .h files should begin with:
    #ifndef _MYFILE_H
    #define _MYFILE_H
    
    // put code here
    
    #endif
  • Note that each .h file needs file guards, and the label ("_MYFILE_H") should be unique for each .h file.
  • To use your struct within your main .cpp file, you will need to have #include "STRUCTFILE.h" - for whatever your struct filename is.

Example code and additional resources

Example code: (repository)

Videos:

  • WORK IN PROGRESS