CIS 22B - Notes for Tuesday 2/20

Announcements and Reminders

  • Review your midterm
  • Online time will be held tomorrow at 5:00 pm

Comments on Assignment 5 

  • File open error - name the file that does not open
  • EOF handling errors
  • Mac users
    • Handle the \r that is left in the buffer when you use getline
    • Initialize variables and struct members
    • If your code does not compile and run on a PC, you will not get full credit

enum (enumerated type)

  • is a type, an integer type
  • can be converted to an int
  • does not automatically act like an int, not int arithmetic
  • an int can be cast to an enum
  • can be used as an array index
  • adheres to scoping rules
  • can be anonymous
Example

enum comparison

  unions

  • like a struct, it contains members
  • members occupy the same area of memory
  • the size of the union is the size of its largest member
  • unions are used to represent mutually exclusive characteristics
  • adheres to scoping rules
  • can be anonymous
Example 1
Example 2

C++ Classes

Terminology

  • class definition, declaration
  • members
    • data members, characteristics
    • member functions, methods, messages, behaviors
  • access specifier
    • public
    • private
    • protected
  • (member) function defintion
  • scope resolution operator
  • declaring an object
  • calling a member function
    • calling a member function with a dot operator
    • calling a member function with an arrow operator
  • inline function
    • implicit inline function
    • explict inline function
  • const member function
  • instantiation
  • object

Examples

  • circle
  • triangle
  • fraction
  • Program 13-1 from the textbook (pages 724-726)

Videos

Lab Exercise #7

Put your name, the compiler and operating system used, and Lab Exercise #7 in a comment at the top of your program. Email your source code.   You may email your lab solution prior to that.  

Modify example, Program 13-1 from the textbook, by adding a data member, color (use type string) and 2 member functions, setColor() and getColor() to the class.  Use the main() function below to test your code.

int main()
{
Rectangle box; // Define an instance of the Rectangle class
double rectWidth; // Local variable for width
double rectLength; // Local variable for length
string rectColor; // Local variable for color

// Get the rectangle's width and length from the user.
cout << "This program will calculate the area of a\n";
cout << "rectangle. What is the width? ";
cin >> rectWidth;
cout << "What is the length? ";
cin >> rectLength;
cout << "What is the color? ";
cin >> rectColor;

// Store the width and length and color of the rectangle
// in the box object.
box.setWidth(rectWidth);
box.setLength(rectLength);
box.setColor(rectColor);

// Display the rectangle's data.
cout << "Here is the rectangle's data:\n";
cout << "Width: " << box.getWidth() << endl;
cout << "Length: " << box.getLength() << endl;
cout << "Area: " << box.getArea() << endl;
cout << "Color: " << box.getColor() << endl;
return 0;
}