Write a C++ Snowman object. You should create the following 3 separate files: Sn
ID: 3569000 • Letter: W
Question
- Write a C++ Snowman object. You should create the following 3 separate files:
- Snowman.h: the header file for the Snowman object and function prototypes;
- Snowman.cpp: the implementation of the project functions; and
- SnowmanDriver.cpp: a program to test your Snowman object.
- The Snowman object will contain the following attributes:
- height (double, in feet)
- weight (double, in pounds)
- temperature (double, in degrees Fahrenheit)
- hasHat (bool)
- scarfColor (string)
- The Snowman object will contain the following data and member functions:
- constructor with no arguments
- assume
- height = 5ft,
- weight = 300lbs,
- temperature = 29.80F,
- hasHat = true,
- scarfColor =
Explanation / Answer
Program Code:
//Snowman.h
#ifndef Snowman_H
#define Snowman_H
#include <string>
using namespace std;
class Snowman
{
private:
// Data Members
double height;
double weight;
double temperature;
bool hasHat;
string scarfColor;
public:
Snowman(); //default constructor
Snowman(double h, double w, double t, bool hH, string sC);
void setHeight(double h);
void setWeight(double w);
void setTemperature(double t);
void setHasHat(bool hH);
void setScarfColor(string &sC);
double getHeight( );
double getWeight( );
double getTemperature( );
bool getHasHat( );
string getScarfColor( );
double Volume( );
};
#endif
//Snowman.cpp
#include "stdafx.h"
#include "Snowman_H.h"
#include <iostream>
#include <string>
using namespace std;
const double PI=3.14159;
Snowman::Snowman(){}
Snowman::Snowman(double h, double w, double t, bool hH, string sC)
{
height = h;
weight = w;
temperature = t;
hasHat = hH;
scarfColor = sC;
}
void Snowman::setHeight(double h)
{
height = h;
}
void Snowman::setWeight(double w)
{
weight = w;
}
void Snowman::setTemperature(double t)
{
temperature = t;
}
void Snowman::setHasHat(bool hH)
{
hasHat = hH;
}
void Snowman::setScarfColor(string &sC)
{
scarfColor = sC;
}
double Snowman::getHeight( )
{
return height;
}
double Snowman::getWeight( )
{
return weight;
}
double Snowman::getTemperature( )
{
return temperature;
}
bool Snowman::getHasHat( )
{
return hasHat;
}
string Snowman::getScarfColor( )
{
return scarfColor;
}
double Snowman::Volume( )
{
double v=0;
double heigh=getHeight()*getHeight()*getHeight();
v=PI*heigh/36;
return v;
}
// SnowmanDriver.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Snowman_H.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int count=0;
void sort(Snowman *sm, int size);
void sort1(Snowman *sm, int size);
int main()
{
//declare the variables
string line;
double vol[100];
double ht;
double wt;
double temp;
bool hHat;
string sColor;
//declare the array of Snowman class
Snowman *sm=new Snowman[100];
string fname;
//accept the input
cout<<"Enter the file name: ";
cin>>fname;
//open a file
ifstream inFile(fname);
if (inFile.is_open())
{
//read the first line
getline(inFile, line);
//loop until end of the file
while ( !inFile.eof() )
{
if(inFile.eof())
{
break;
}
//read the data from file
inFile >> ht;
inFile >> wt;
inFile >> temp;
inFile >> hHat;
inFile >> sColor;
//set the data
sm[count].setHeight(ht);
sm[count].setWeight(wt);
sm[count].setTemperature(temp);
sm[count].setHasHat(hHat);
sm[count].setScarfColor(sColor);
vol[count]=sm[count].Volume();
++count;//increment the counter
}
//close the file
inFile.close();
}
//if the file is unable to open print the message
else{
cout<<"Unable to open the file."<<endl;
}
//create other Snowman array objects and
//incrementing variables
Snowman arr[50];
int j=0;
int k=0;
int p=0;
Snowman arr1[50];
Snowman arr2[50];
//loop and separate the color of the Scarf
for(int i=0;i<count;i++)
{
if(sm[i].getScarfColor().compare("RED") == 0)
{
arr[j]=sm[i];
j++;
}
if(sm[i].getScarfColor().compare("GREEN") == 0)
{
arr1[k]=sm[i];
k++;
}
if(sm[i].getScarfColor().compare("BLUE") == 0)
{
arr2[p]=sm[i];
p++;
}
}
//sort each color of Scarf with respect to temperature
sort(arr, j);
sort(arr1, k);
sort(arr2, p);
int incre=0;
//add back the elements into the Snowman array
for(int i=0;i<j;i++)
{
sm[incre]=arr[i];
incre++;
}
for(int i=0;i<k;i++)
{
sm[incre]=arr1[i];
incre++;
}
for(int i=0;i<p;i++)
{
sm[incre]=arr2[i];
incre++;
}
//Sort the elements by color of Scarf finally
sort1(sm, incre);
cout<<line<<endl;
incre=0;
//print the output
for(int i=0;i<count; i++)
{
if(sm[i].getHasHat()==0)
{
cout<<sm[i].getHeight()<<" " <<sm[i].getWeight()<<" "<<sm[i].getTemperature()
<<setw(15)<<" no "<<setw(10)<<sm[i].getScarfColor()<< endl;
}
else
{
cout<<sm[i].getHeight()<<" " <<sm[i].getWeight()<<" "<<sm[i].getTemperature()
<<setw(15)<<" yes "<<setw(10)<<sm[i].getScarfColor()<< endl;
incre++;
}
}
cout<<endl;
cout<<incre<<" Snowmen have a hats."<<endl;
cout<<j<<" Snowmen have a RED scarf."<<endl;
double total=0;
cout<<endl;
cout<<"The average volume of a snowman is ";
for(int i=0;i<count;i++)
{
total+=vol[i];
}
//print the average value of the volume
cout<<total/count<<endl;
system("pause");
exit(0);
}
//sort function with respect to temperature
void sort(Snowman *sm, int size)
{
Snowman temp=sm[0];
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(sm[i].getTemperature()<sm[j].getTemperature())
{
temp=sm[i] ;
sm[i]=sm[j];
sm[j] = temp;
}
}
}
}
//sort function with respect to ScarfColor
void sort1(Snowman *sm, int size)
{
Snowman temp=sm[0];
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(sm[i].getScarfColor().compare(sm[j].getScarfColor())<0)
{
temp=sm[i] ;
sm[i]=sm[j];
sm[j] = temp;
}
}
}
}
Sample output:
Enter the file name: InputFile.txt
Height Weight Temp Hat Scarf
6.62 671.41 28.41 no BLUE
6.37 638.67 28.62 no BLUE
5.49 552.15 28.77 yes BLUE
7.96 796.89 29.54 yes BLUE
6.51 652.86 30.31 no BLUE
6.25 630.39 30.17 yes GREEN
7.41 741.08 30.63 yes GREEN
5.87 596.68 30.76 no GREEN
3.81 383.48 30.92 yes GREEN
3.3 334.99 31.12 yes GREEN
4.73 473.94 31.72 no GREEN
5.99 608.91 31.86 yes GREEN
5.94 600.1 28.53 no GREEN
5.16 519.42 28.54 yes GREEN
7.07 710.3 28.99 no GREEN
6.22 621.98 29.26 yes GREEN
4.89 489.98 29.46 no GREEN
5.07 512.17 29.77 no RED
6.81 690.39 30.66 yes RED
5.36 537.87 30.93 yes RED
5.41 543.21 32.2 yes RED
7.7 771.42 28.56 yes RED
3.41 342.2 28.98 no RED
3.52 359.16 29.36 no RED
13 Snowmen have a hats.
7 Snowmen have a RED scarf.
The average volume of a snowman is 18.6015
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.