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
- will blow up if you attempted to call something that is
- 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
withcin
- must read in each field individually and compose the
struct
- must read in each field individually and compose the
- 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
ordefined