View the step-by-step solution to:

In the program figures.cpp we have a base class location and various derived classes: circle, triangle, rectangle. Complete the implementation of the...

In the program figures.cpp we have a base class location and various derived classes: circle, triangle, rectangle. Complete the implementation of the derived classes. For marking purposes, run the program entering: circle(1,2,3), triangle(1,2,1,2,1) and rectangle(1,2,3,4).

// File: figures.cpp

#include <cmath>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class location {
private:
float x; // position of the figure
float y;

public:
void read(istream& in) ;
void write(ostream& out);
float area(void); // returns 0
};

class circle : public location {
private:
float radius;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

class triangle : public location {
private:
float side1, side2, angle;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

class rectangle : public location {
private:
float side1, side2;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

int main()
{
string type; // figure type
ofstream fout ("figures.out");

while(1) { // loop until break occurs
cout << "nnType of figure: "; cin >> type;

if(type == "circle") {
circle* p = new circle;
p->read(cin);
fout << "nobject is a circlen";
p->write(fout);
}

else if (type == "triangle") {
triangle* p = new triangle;
p->read(cin);
fout << "nobject is a trianglen";
p->write(fout);
}

else if (type == "rectangle") {
rectangle* p = new rectangle;
p->read(cin);
fout << "nobject is a rectanglen";
p->write(fout);
}

else break; // we are done entering data

}

return 0;
}

////////////////// implementation of location /////// ///////////

void location::read(istream& in)
{
if(in == cin)
cout <<"x coordinate: ";
in >> x;

if(in == cin)
cout <<"y coordinate: ";
in >> y;
}

float location::area(void)
{
return 0.0;
}

void location::write(ostream& out)
{
out << "x coordinate: " << x << "n";
out << "y coordinate: " << y << "n";
}

////////////////// implementation of circle //////////////////


////////////////// implementation of triangle //////////////////


////////////////// implementation of rectangle //////////////////

/* sample output

object is a circle
x coordinate: 1
y coordinate: 2
radius = 3
area = 28.2743

object is a triangle
x coordinate: 1
y coordinate: 2
side1 = 1
side2 = 2
angle = 1
area = 0.841471

object is a rectangle
x coordinate: 1
y coordinate: 2
side1 = 3
side2 = 4
area = 12
*/


Try out the program figures1.cpp which uses the same classes and almost the same main program as the previous question. Explain why this program does not work properly. Change the classes (not main) so that the program runs properly and run the program with the same data as before.

// File: figures1.cpp

#include <cmath>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class location {
private:
float x; // position of the figure
float y;

public:
void read(istream& in) ;
void write(ostream& out);
float area(void);
};

class circle : public location {
private:
float radius;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

class triangle : public location {
private:
float side1, side2, angle;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

class rectangle : public location {
private:
float side1, side2;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

int main()
{
string type; // figure type
ofstream fout ("figures1.out");
location* p;

while(1) { // loop until break occurs
cout << "nnType of figure: "; cin >> type;

if(type == "circle") {
p = new circle;
p->read(cin);
fout << "nobject is a circlen";
p->write(fout);
}

else if (type == "triangle") {
p = new triangle;
p->read(cin);
fout << "nobject is a trianglen";
p->write(fout);
}

else if (type == "rectangle") {
p = new rectangle;
p->read(cin);
fout << "nobject is a rectanglen";
p->write(fout);
}

else break; // we are done entering data
}

return 0;
}

////////////////// implementation of location /////// ///////////

void location::read(istream& in)
{
if(in == cin)
cout <<"x coordinate: ";

in >> x;
if(in == cin)
cout <<"y coordinate: ";
in >> y;
}

float location::area(void)
{
return 0.0;
}

void location::write(ostream& out)
{
out << "x coordinate: " << x << "n";
out << "y coordinate: " << y << "n";
}

////////////////// implementation of circle //////////////////


////////////////// implementation of triangle //////////////////


////////////////// implementation of rectangle //////////////////


Try out the main program figstack.cpp which uses a the STL stack class to implement a stack of figures. Explain why this program does not work properly.
For marking purposes push the figures: circle(1,2,3), triangle(1,2,1,2,1) and rectangle(1,2,3,4) then pop them.

Fix the program figstack.cpp so that it does work. For marking purposes use the same data.


// File: figstack.cpp
// This program implements a stack of locations using a stack template class

#include <stack>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class location {
private:
float x; // position of the figure
float y;

public:
void read(istream& in) ;
void write(ostream& out);
float area(void);
};

class circle : public location {
private:
float radius;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

class triangle : public location {
private:
float side1, side2, angle;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

class rectangle : public location {
private:
float side1, side2;

public:
void read(istream& in);
void write(ostream& out);
float area(void); // area of the figure;
};

int main()
{
ofstream fout("figstack.out");
stack<location> mystack;
char ch; // response to prompt
string type; // type of figure
location* ptr; // pointer to object pushed or popped

do {
// print a little menu
cout << "nnp = push n";
cout << "o = popn";
cout << "q = quitnn";

cin >> ch;

if (ch == 'p') {
cout <<"nEnter type of data to push : ";
cin >> type;
ptr = NULL;

if(type == "circle")
ptr = new circle;

else if(type == "triangle")
ptr = new triangle;

else if (type == "rectangle")
ptr = new rectangle;

if(ptr != NULL) { // got a valid type
ptr->read(cin);
mystack.push(*ptr);
}
}

if(ch == 'o') {
if(mystack.empty())
cout << "stack is emptyn";
else {
ptr = &mystack.top();
mystack.pop();
cout << "popped:n"; ptr->write(cout); cout << "n";
fout << "popped:n"; ptr->write(fout); fout << "n";
}
}

}while(ch != 'q');


return 0;
}

////////////////// implementation of location /////// ///////////

void location::read(istream& in)
{
if(in == cin)
cout <<"x coordinate: ";

in >> x;
if(in == cin)
cout <<"y coordinate: ";
in >> y;
}

float location::area(void)
{
return 0.0;
}

void location::write(ostream& out)
{
out << "x coordinate: " << x << "n";
out << "y coordinate: " << y << "n";
}

////////////////// implementation of circle //////////////////


////////////////// implementation of triangle //////////////////


////////////////// implementation of rectangle //////////////////


In the following program the bank account classes previously coded are used.

(a)
Run the program using the input file transactions.in, choose the chequeing account when prompted. Does the program work properly?

(b)
Make the necessary methods in the class account virtual. Does the program work properly?

(c)
Make the necessary change to the processtransactions function so that the program works properly.


// File: accountstransactions.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class account {
protected:
string owner;
float balance;
float withdrawfee;

public:
account(string name, float fee);
void show(ostream& out);
void deposit(float amount);
bool withdraw(float amount);
};

class savingsaccount :public account {
private:
float interestrate;

public:
savingsaccount(string name, float fee, float rate);
void show(ostream& out);
void addinterest(void);
};

class chequingaccount : public account {
private:
int remainingfreewithdrawals;

public:
chequingaccount(string name, float fee, int numberfree);
void show(ostream& out);
bool withdraw(float amount);
};

void processtransactions(account x);

int main(void)
{
char type;
savingsaccount s("joe", 0.50, 0.03);
chequingaccount c("bob", 0.50, 2);

cout << "Which account to process? (c or s) :";
cin >> type;

if(type == 's') processtransactions(s);
if(type == 'c') processtransactions(c);

system("pause");

return 0;
}

///////////////////// implementation of account ///////////////////////
account::account(string name, float fee)
{
owner = name;
balance = 0;
withdrawfee = fee;
}

void account::show(ostream& out)
{
out << "owner: " << owner << "n";
out << "balance: " << balance << "n";
out << "withdrawfee " << withdrawfee << "n";
}

void account::deposit(float amount)
{
balance = balance + amount;
}

bool account::withdraw(float amount)
{
bool result;
if (amount > balance-withdrawfee) {
cout << "Insufficient funds";
result = false;
}
else {
balance = balance - amount - withdrawfee;
result = true;
}

return result;
}

///////////////////// implementation of savingsaccount ///////////////////////

savingsaccount::savingsaccount(string name, float fee, float rate)
: account(name, fee)
{
interestrate = rate;
}

void savingsaccount::show(ostream& out)
{
account::show(out);
out << "interest rate: " << interestrate << "n";
}

void savingsaccount::addinterest(void)
{
balance = (1 + interestrate)*balance;
}

///////////////////// implementation of chequingaccount ///////////////////////

chequingaccount::chequingaccount(string name, float fee, int numberfree)
: account(name, fee)
{
remainingfreewithdrawals = numberfree;
}

void chequingaccount::show(ostream& out)
{
account::show(out);
out << "remaing free withdrawals: " << remainingfreewithdrawals << "n";
}

bool chequingaccount::withdraw(float amount)
{
bool result;
float value;

if(remainingfreewithdrawals >0)
value = amount;
else
value = amount + withdrawfee;

if (value > balance) {
cout << "Insufficient funds";
result = false;
}
else {
balance = balance - value;
result = true;
remainingfreewithdrawals--;
}

return result;
}

void processtransactions(account x)
{
ifstream fin("transactions.in");
char type;
float amount;

while(fin >> type >> amount) {
if(type == 'w'){
x.withdraw(amount);
cout << "withdraw " << amount << "n";
}

if(type == 'd') {
x.deposit(amount);
cout << "deposit " << amount << "n";
}

x.show(cout);
cout << "n";
}
}

assign9.mht

This question was asked on Mar 19, 2010.

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors and customizable flashcards—available anywhere, anytime.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access or to earn money with our Marketplace.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
  • -

    Flashcards

    Browse existing sets or create your own using our digital flashcard system. A simple yet effective studying tool to help you earn the grade that you want!

    Browse Flashcards