Topics:
Consider the following two files that declare (square.h
) and define (square.cpp
) a
class named Square. The Square class includes one floating-point data member, side, in the private
section which holds the state of the object. The class also has an accessor member function for accessing the value
of side, a mutator (modifier) function for setting the value of side, and a member function for calculating
the area.
Notice the member function prototype statements appear in the public section of the class declaration. This declares them as member functions of the Square class. Notice further that the member function definitions have direct access to the state (private data members) of the object, so there is no need to pass the side of a Square object to the member functions. Also note the scope resolution operator (::) in the .cpp file. This operator tells the compiler which class a member function belongs to.
// square.h // Specifies general square class class Square { public: //Member functions void setSide(); // Initializes a square to a user specified size double getSide(); // Returns width of the square double area(); // Returns area of square private: //Data Members double side; // square width };
// square.cpp // Implements member functions of class Square #include <iostream> #include "square.h" using namespace std; void Square::setSide() //Initialize a square to a user specified size { cout << "Enter the width of the square: "; cin >> side; cout << endl; } double Square::getSide() //Return width of square { return side; } double Square::area() //Return area of square { return(side*side); }
square.h
. Type in (or copy/paste)
the Square class declaration as it is given above. Be sure not to miss the semi-colon after the closing curly-brace.
Save the file in the project folder.
square.cpp
in the project folder. Type in (or copy/paste)
the member function definitions above. Add square.cpp
to the project.
// main.cpp // Driver to demonstrate square class #include <iostream> #include "square.h"
using namespace std; int main() { Square sqr1, sqr2; sqr1.setSide(); sqr2.setSide(); cout << "The width of sqr1 is: " << sqr1.getSide() << endl; cout << "The width of sqr2 is: " << sqr2.getSide() << endl; cout << "The area of sqr1 is: " << sqr1.area() << endl; cout << "The area of sqr2 is: " << sqr2.area() << endl; return 0; }
The this entry in the variable window of the debugger during the execution of a member function refers to the calling object. Tipping it down will uncover the data members of that calling object.
perimeter()
that returns the perimeter of a square. To do this,
you will need to modify the Square declaration in square.h
by adding the prototype and add the perimeter
function definition to the Square implementation file in square.cpp.
Now we will examine another example of a programmer-defined classes. The files bankAccount.h
and
bankAccount.cpp
(given below) define a class named BankAccount
. This class is nearly
identical to the one developed in Section 6.2 of the text. This version has an additional member function, deposit
and is separated into two files - a header file and an implementation file. We will implement and then modify this
class.
bankAccount.h
.
Type in (or copy/paste) the BankAccount class declaration as it is given below. Be sure not to miss the semi-colon
after the closing curly-brace. Save the file.
#include <iostream> using namespace std; //Class for a bank account class BankAccount { public: void set(int dollars, int cents, double rate); void set(int dollars, double rate); void update(); void deposit(double amount); double getBalance(); double getRate(); void output(ostream& outs); private: double balance; double interestRate; double fraction(double percent); };
Look at the private members of BankAccount class. There are two data members called balance
and
interestRate
. The third private member is a function called fraction
. Since fraction
is a private member function, it cannot be called in the body of main
or in the body of any function
that is not a BankAccount member function. Function fraction
can only be called in the definitions
of other BankAccount member functions (Notice how it is used below).
bankAccount.cpp
in your project folder and add it to your
project. Type in (or copy/paste) the BankAccount member function definitions as given below.
#include <iostream>
#include "bankAccount.h"
using namespace std;
void BankAccount::set(int dollars, int cents, double rate)
{
balance = dollars + 0.01*cents;
interestRate = rate;
}
void BankAccount::set(int dollars, double rate)
{
balance = dollars;
interestRate = rate;
}
void BankAccount::update( )
{ balance = balance + fraction(interestRate)*balance; }
void BankAccount::deposit( double amount )
{ balance = balance + amount; }
double BankAccount::fraction(double percent)
{ return (percent/100.0); }
double BankAccount::getBalance( )
{ return balance; }
double BankAccount::getRate( )
{ return interestRate; }
void BankAccount::output(ostream& outs)
{
outs.setf(ios::fixed);
outs.setf(ios::showpoint);
outs.precision(2);
outs << "Account balance $" << balance << endl;
outs << "Interest rate " << interestRate << "%" << endl;
}
#include <iostream>
using namespace std;
#include "bankAccount.h"
int main( )
{
BankAccount account1, account2;
double depositAmount;
cout << "Start of Test:\n";
account1.set(123, 99, 3.0);
cout << "account1 initial statement:\n";
account1.output(cout);
account2.set(100, 5.0);
cout << "account2 initial statement:\n";
account2.output(cout);
account1.update( );
cout << "account1 after update:\n";
account1.output(cout);
cout << "How much do you wish to deposit? ";
cin >> depositAmount;
account1.deposit(depositAmount);
cout << "account1 after deposit: \n";
account1.output(cout);
account2 = account1;
cout << "account2:\n";
account2.output(cout);
return 0;
}
withdrawal
with the following prototype:
void withdrawal(double amount);
//Postcondition: amount is subtracted from the account balance.
Again, you will need to modify the BankAccount declaration in bankAccount.h
, and also add the withdrawal
function definition to the BankAccount implementation in bankAccount.cpp
.
void
functions. They do
not return a value. In this step, these functions will be changed to return a bool
to indicate failure
or success of the operation. This makes it possible to use calls to these functions as logical expressions. For
example, the calls made to these functions could be used in an if-else statement such as:
depositAmount = -100;
if ( account1.deposit(depositAmount) )
cout << "Deposit succeeded. \n";
else
cout << "Deposit failed. \n";
cout << "account1 balance after deposit of " << depositAmount << account1.getBalance();
Open bankAccount.h and modify the class declaration so that deposit and withdrawal return bool
values. Modify the postconditions to indicate that when the functions return 0 or when a 1 is returned.