DOC PREVIEW
UD CISC 181 - Struct and Class

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

struct and classKeith Trnka2009-10-21structures•history: carried over from C•motivation•programs mirror the real world•real things have parts, attributes, etc.example problem•example problem: inventory tracking system•each item has:•name•price•ID number (such as UPC)•quantityexample problem•possible solution: use four arrays•char* product_names[MAX_PRODUCTS];•float product_prices[MAX_PRODUCTS];•int product_IDs[MAX_PRODUCTS];•int product_quantities[MAX_PRODUCTS];•Keith’s side note: float vs int for pricesexample problem•How to describe product i?! cout << product_names[i] << endl! ! << "\tID: " << product_IDs[i] << endl! ! << "\tQuantity: " << product_quantities[i] << endl! ! << "\tPrice: $" << fixed << setprecision(2) << product_prices[i] << endl! ! << "\tTotal value: $" << product_quantities[i] * product_prices[i] << endl;Paper (500 sheets) ID: 12345 Quantity: 145 Price: $10.50 Total value: $1522.50example problem•problems•annoying to sort the inventory (have to sort 4 arrays in parallel)•susceptible to some nasty bugs•possible to accidentally mis-align the arrays•could cause mis-matched product IDs and pricesexample with struct•alternative: use a struct to represent an itemstruct Item! {! char* name;! float price;! int ID;! int quantity;! };making structs•keyword struct•struct name: Item•semi-colon at end•variables declared normally•called fields, member values, members, etc.struct Item! {! char* name;! float price;! int ID;! int quantity;! };using structs•Item is now a data type, can be used just like other data types•access fields/members with dot-operator! Item products[MAX_PRODUCTS];!! products[0].name = "Paper (500 sheets)";! products[0].price = 10.50;! products[0].ID = 12345;! products[0].quantity = 145;using structs•the cout statement is a little easier to read now:! cout << products[i].name << endl! ! << "\tID: " << products[i].ID << endl! ! << "\tQuantity: " << products[i].quantity << endl! ! << "\tPrice: $" << fixed << setprecision(2) << products[i].price << endl! ! << "\tTotal value: $" << products[i].quantity * products[i].price << endl;struct notes•often thought of as “nouns” or “objects in the real world”•arrow operator: syntactic sugarthese two are identical•deference+access: (*item_pointer).price•arrow: item_pointer->pricestruct notes•structures can’t have fields of the struct’s data type (but can pointers)•won’t compile:struct Item { float price; Item foo; }•will compile:struct Item { float price; Item* foo; }•Why? (hint: memory allocation)classes•a class represents•a “thing” along with it’s parts/attributes(like a struct)•functionality to:•get information about the thing•modify the thing•simplify creation of itclasses•classes are data+functions all grouped•overall theme of classes: tools to support LARGE software projects•terms•class: the data type•object: a value of that data type•example: cars in general (a class) vs Keith’s car (a specific one)access modifiers•classes are defined much like structs, but change struct to class:class Item! {! char* name;! float price;! int ID;! int quantity;! };access modifiers•this class can’t be used for anything•by default, fields/members are private•only functions in the class can access/modify themclass Item! {! char* name;! float price;! int ID;! int quantity;! };access modifiers•specify access manually:•publicanyone can access•privateonly class functions can accessclass Item! {! public:! ! char* name;! ! float price;! ! int ID;! ! int quantity;! };class template•class definitions will all basically look like this/* some formal description */class SomeClassName { public: // some function prototypes private: // data members };member functions•normal functions•function prototype in class definition•function body elsewhere(maybe even another file)•need to attach the class name in the definition like so:ReturnType ClassName::FunctionName(params)member functions•example prototypes in public section:float getTotalValue();void print();•example function body:float Item::getTotalValue()! {! return quantity * price;! }member functions•functions can•access the fields of the object directlymember functions•inline functions•the full function body can be placed in the class instead of a prototype•don’t put the scope (ClassName and ::)•example:float getPrice()! {! return price;! }member functions•inline functions•are treated by the compiler just like a normal function with the inline keywordmember functions•call them like you access fields:Item someItem;someItem.print();special functions•constructors - called when an object comes into existence•used for setting default values•used for initializing values in one line•must have:•same name as the class•no return type (not void or anything)special functions•destructors - called when an object goes out of existence•used to•free dynamically allocated memory•close files, network connections, database connections•not necessary for many simple classesconstructors•default constructor•the constructor that is called with no arguments•example:Item someItem; // this calls default constr.Item someItem(); // same thingItem(); // creates an anonymous objectconstructors•example constructor:Item() { price = quantity = ID = 0; name = NULL; }constructors•can take parameters and be overloaded (just like normal functions)•can have default argumentsItem(char* n = NULL, double price = 0, ...)constructors•constructors work funny with arrays•example:Item inventory[100];•this calls the default constructor once for each elementother weirdness•classes and =•by default, you can do something like this:Item foo;Item bar(“Soap”);foo = bar;•this copies values across•is a shallow copy (meaning it doesn’t copy allocated memory, but copies the pointers)underlying principles•motivation•you are coding up the class•someone else is using your classes in their code (but not modifying it)•you don’t want them being able to set fields to invalid values•the more you allow them to create bugs, the more bugs will be createdgood design•fields/variables should be private•most functions should be public•people that code using your class should only need to read the


View Full Document
Download Struct and Class
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Struct and Class and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Struct and Class 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?