LAB 11B - TRIANGLE CLASS

Concept

Focus on defining a Triangle class from scratch.

You should write a main.cpp function that shows each feature of your class works as expected (e.g., print out a triangle's area by calling the area function). You will need to submit your class prototype file, your class implementation file, and your main.cpp file for this lab with Homework 11.

Member Functions

Consider a program that works with photographs. Photos have a height and a width. We might model such a class like this:

class Photo {
    public:
        Photo();        // Photo constructor function
        int height;
        int width;
};

Since photos have a height and a width, they also have an area. What is a photo's area? It's a value generated by multiplying a Photo object's height and width together. How might we enable our Photo class such that a Photo object is able to tell us its area? In other words, what do we need to do in order to be able to write code like the following?

Photo p;
cout << "The area of Photo p is" << p.Area() << endl;

To support the ability to write p.Area(), we must define area as a member function, or "a function that belongs to objects of a class." We do this in two steps.

  1. Declare the function prototype in the header file
  2. Define the function implementation in the implementation file

In other words, in the header file photo.h, we should add:

class Photo {
    public:
        Photo();        // Photo constructor function
        int Area();      // area member function
        int height;
        int width;
};

And in the implementation file, photo.cpp, we should add:

int Photo::Area() {
    return height * width;
}

The function prototype in photo.h tells the machine, "Hey, Photo objects have a member function called area that returns an int." The function definition in photo.cpp tells the machine, "Hey, the definition of the Photo member function area() I told you about in photo.h works like this."

Look at the body of the area function above. Notice how the function, since it is declared "inside" the class, has direct access to everything else inside the class, such as height and width.

This is so important, we'd like you to read it again. Look at the body of the area function above. Notice how the function, since it is declared "inside" the class, has direct access to everything else inside the class, such as height and width.

When should you use member variables (e.g., height and width) instead of member functions in your main.cpp file? We hope you are starting to see why most seasoned programmers use member functions.

Public vs. Private Members

In Lab 11A, you defined a simple Money class that used the public: syntax to tell the machine that the dollars and cents properties were "accessible from outside the object." For example, consider the following code:

photo.h

class Photo {
    public:
        Photo();
        int Area();
        int height;
        int width;
};

photo.cpp

Photo::Photo() {
    height = 8;
    width = 5;
}

That public: thing you see in the header file tells the machine, "Hey computer, when a programmer instantiates a Photo object, they should be allowed to access the values of height and width." In other words, this mechanism allows you to do the following:

Photo myFamily;
cout << myFamily.height; // accessing the value of the object's height
myFamily.height = 20;    // assigning a value to the object's height

Do you see how you can access the height attribute of the Photo myFamily, as well as assign a new value to this data member?

More importantly, do you see the problem? Look at this:

myFamily.height = -12;

Uh oh! Because the height attribute is public, any programmer using Photo objects can assign any integer value to that member variable, including ones that don't make sense, such as negative heights.

"Big whoop," you say, "I know that we can make data members private." And you'd be right (and deserve a gold star for paying attention). Let's change our class so that arbitrary values can't be assigned to data members.

photo.h

class Photo {
    public:
        Photo();
        int Area();
    private:
        int height;
        int width;
};

Ahh, there, now no one can assign values to Photo objects:

myFamily.height = -12;

However, now you've introduced another problem. By declaring the data members to be private, you've disabled all access to the member variables! This means that you can no longer do this:

cout << myFamily.height;

The height attribute is declared to be private, so there is no direct access to the attribute. Are you thinking, "I hate C++!"? Don't worry, here's how you can control access to data members: define member functions we call "getters and setters."

Accessors and Mutators, aka Getters and Setters

Let us restate the problem above: we do not want to allow direct access to member variables because invalid values can be assigned to them, causing the world to end (or, less dramatically, causing our program to be incorrect). We can declare a data member to be private, but then the programmer loses the ability to read the value of a data member as well.

The solution? Let us define two member functions that we use to manage reading and writing for each data member.

photo.h

class Photo {
    public:
        Photo();
        int Area();
        int GetHeight();
        int GetWidth();
        void SetHeight(int h);
        void SetWidth(int w);
    private:
        int height;
        int width;
};

photo.cpp

Photo::Photo() {
    height = 8;
    width = 5;
}

int Photo::GetHeight() {
    return height;
}

int Photo::GetWidth() {
    return width;
}

void Photo::SetHeight(int h) {
    if (h > 0) {
        height = h;
    }
}

void Photo::SetWidth(int w) {
    if (width > 0) {
        width = w;
    }
}

If you were to implement the code above, your programs could then do the following:

Photo p;
cout << "Height is " << p.GetHeight();

p.SetHeight(20);  // height is now 20
p.SetHeight(-69); // height is unchanged

Spend time looking at the code in photo.cpp above to see what those functions do. Notice how GetHeight and GetWidth are very similar. Almost every "getter" function you write will follow this pattern. Ditto for "setter" functions.

In order to understand how this works, you should realize that member functions have full access to everything declared inside the class; member functions are not subject to the public/private rules.

Triangle Class

As you know from earlier assignments (i.e., HW 02, Lab03A, Lab03B, and Lab04A), we have a strange fetish with triangles. Now that you have some skills with classes, write a Triangle class to represent this "thing" to the computer. What properties do you need to represent a triangle? Make these properties private to your class. (You should know why private makes sense; ask if you don't!) In all functions that modify data members, you should always ensure the values being set make sense. Also, use a private helper function wherever one makes sense. Functions you should include in your class:

For added practice, consider using an array to represent the triangle's properties and create an array (or vector) of triangles in main.