|
ex3-2.cpp - Example 3-2 The triangle class |
// File: ex3-2.cpp - the triangle class
#include <iostream>
using namespace std;
class triangle
{
private:
double base;
double height;
double area;
public:
// member function prototypes
void store(double, double);
void calc_area(void);
void show(void);
};
// member function definitions
void triangle::store(double b, double h)
{
base = b;
height = h;
return; // "return" is optional
}
void triangle::calc_area(void)
{
area = .5 * base * height;
return;
}
void triangle::show(void)
{
cout << "base = " << base;
cout << "\theight = " << height;
cout << "\tarea = " << area << endl;
return;
}
int main(void)
{
triangle t; // an instance (object) of triangle class
t.store(1.23,4.55); // initialize triangle t
t.calc_area(); // calculate the area of triangle t
t.show(); // display triangle t data
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley