EECE 352: PS1

Due: Tuesday, 31 August 1999

This problem set is designed to remind you of what you already learned about C++ pointers, classes, inheritance, constructors, and operators; and to introduce you to the use of templates. This problem set requires a minimal coding effort.

You will implement a hierachy of "investment vehicles". The top-level class class (which should be an abstract class) is called Security. Three classes inherit from Security: Bond, Stock, and CashAccount. Two classes inherit from CashAccount: Checking and Savings. All of these classes must implement a presentValue() function which returns the current value of that particular investment. They must also implement an operator<<.

The program below is the main.cpp file you will use to test your program. The comments tell you the output you should see when you run this file. Notice that the file uses several different constructors and other functions, such as setSharePrice(double d). You must implement all these

Grading: The grade will depend on:

// ps1.cpp : A simple portfolio application.

#include "Checking.h"
#include "Savings.h"
#include "Bond.h"
#include "Stock.h"
#include "Portfolio.h"

using namespace std;

//The output from this program should be:
//Checking:       CashAccount: balance=100
//Savings: interest=0.06  CashAccount: balance=200
//Savings: interest=0.06  CashAccount: balance=200
//Savings: interest=0.06  CashAccount: balance=200
//Bond: cost=133  yield=0.05
//Stock: numShares=10     pricepershare=14.5
//Portfolio Value = 1113.1
//Portfolio Value = 457.1
//Press any key to continue

int main(int argc, char* argv[])
	Checking myCheckAccount(100); //checking account with 100 dollars
	cout << myCheckAccount << endl;

	Savings mySavingsAccount(200, .06); //savings with 200 dollars and 6% interest
	cout << mySavingsAccount << endl;
	Savings * savingsCopy = new Savings(mySavingsAccount); //a copy constructor
	cout << (*savingsCopy) << endl;
	delete savingsCopy;
	cout << mySavingsAccount << endl; //just making sure you did copy it.

	Bond myBond(133, .05); //I buy a bond worth $133, with a 5% yield
	cout << myBond << endl;

	Stock myStock(10, 14.5); //I buy 10 shares of a stock at $14.50/share
	cout << myStock << endl;

	Security * pfolio[4];

	pfolio[0] = & myCheckAccount; //place them in array
	pfolio[1] = & mySavingsAccount;
	pfolio[2] = & myBond;
	pfolio[3] = & myStock;
	double myNetWorth = 0;
	for (int i =0; i <4 ; i++)
		//C++ determines what is the REAL type and call the appropiate function
		myNetWorth += pfolio[i]->presentValue();
	cout << "myNetWorth=$" << myNetWorth << endl; //how much am I worth?

	myStock.setSharePrice(22); //stock goes up in price

	myNetWorth = 0;
	for (i =0; i <4 ; i++)
		myNetWorth += pfolio[i]->presentValue();
	cout << "myNetWorth=$" << myNetWorth << endl; //how much am I worth?

	//You must implement the class Portfolio, which uses a template.
	// It stores a number of assets of the given type (here we use 
	//  Security, but you can imagine a Portfolio of RealEstate, or
	//  BeaniBabies, or anything that has a currentValue() function).
	// and automatically calculates the total currentValue
	//TIP: Place all the Portfolio code on Portfolio.h, otherwise it
	// will not compile (see Stroustroup 13.7 for the reason, basically
	// VC++ has no export keyword, yet.)
	//TIP: Portfolio should keep an array (you can hardwire the size to 4)
	// of pointers to Security.
	Portfolio<Security> allInv;
	Stock s1(15, 12.5);
	Stock s2(14, 9.4);
	Checking c1(138);
	Bond b1(656, .08);

	cout << "Portfolio Value = " << allInv.presentValue() << endl;

	allInv.removeLast(); // removes the last add. 

	cout << "Portfolio Value = " << allInv.presentValue() << endl;

	return 0;

Jose M. Vidal
Last modified: Mon Aug 16 16:17:17 EDT 1999