Skip to content

Instantly share code, notes, and snippets.

@Theartbug
Last active January 18, 2020 00:07
Show Gist options
  • Save Theartbug/9293a61df31d0a935daf4ae28f3a980e to your computer and use it in GitHub Desktop.
Save Theartbug/9293a61df31d0a935daf4ae28f3a980e to your computer and use it in GitHub Desktop.
c++ structs

structures & Review

structs

  • a way to group different types of data
  • Example:
struct product {
	float price;
	char name[41];
	char description[131];
	char distributor[131];
};
  • considered data abstraction
  • they can be used as a data type
  • Class can do the same thing, but with overhead of extra features
  • ; is required after creation of structs!
  • cannot initialize within struct creation
    • no memory is allocated!
    • should initialize each field for safety at creation
  • Typically created like global variables, before functions, outside of main
  • compiler will not check if you've assigned everything to each field
    • will blow up if you attempted to call something that is undefined
  • all memory is allocated during variable declaration for all fields in the struct
product item;
item.price = 15.99;
item.name = "iodine";
item.description = "yes good product";
item.distributor = "Freddies";
  • we cannot read in an entire struct with cin
    • must read in each field individually and compose the struct
  • setting two struct variables equal to one another will copy each field
    • item = item2
  • structs can be passed by value, DO NOT DO
    • returnType function(product &struct)

review

  • || truth table
or T F
T T T
F T F
  • && truth table
&& T F
T T F
F F F
  • before you can call a function it must be declared or defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment