Just create a header file ie:
Code:
// arch_win.h
#ifndef __ARCH_WIN__H__
#define __ARCH_WIN__H__
const float pi = 3.14159; // this will NEVER change
class arch_win
{
private:
/* anything that shouldn't be tampered with outside the class */
/* in case of anything dosn't add up, everything is 0 */
float height, width, price, totalprice, radius, area;
void set_radius(void){ };
void set_total_price(void){};
public:
/* all things that should be accessible outside the class */
void set_area(char ar){};
/* Any combination of constructor for the class */
arch_win(){};
arch_win(float h){};
arch_win(float h, float w){};
arch_win(float h, float w, char ar){};
void set_price(float p){};
/* we should be able to fetch any internal info */
float get_height(){};
float get_width(){};
float get_price(){};
float get_area(){};
float get_totalprice(){};
};
#endif
And a code file ie:
Code:
// arch_win.cpp
#include <math.h>
#include "arch_win.h"
void arch_win::set_radius(void)
{
radius = (width * .5);
}
void arch_win::set_total_price(void)
{
totalprice = price * area;
}
/* all things that should be accessible outside the class */
void arch_win::set_area(char ar)
{
if (toupper(ar)=='A')
area=((height*width)+ (.5*(pow(radius,2)* pi)));
else
area=height*width;
};
/* Any combination of constructor for the class */
void arch_win::arch_win()
{
height = 0;
width = 0;
radius = 0;
area = 0;
}
void arch_win::arch_win(float h)
{
height = h;
width = 0;
radius = 0;
area = 0;
}
void arch_win::arch_win(float h, float w)
{
height = h;
width = w;
arch_win::set_radius(w);
area = 0;
/* theres no aORr to decide area here set_area() will
* have to explicit be called from outside
*/
}
void arch_win::arch_win(float h, float w, char ar)
{
height = h;
width = w;
arch_win::set_radius();
arch_win::set_area(ar);
}
void arch_win::void set_price(float p)
{
price = p;
arch_win::set_total_price();
}
/* we should be able to fetch any internal info */
float arch_win::get_height()
{
return height;
}
float arch_win::get_width()
{
return width;
}
float arch_win::get_price()
{
return price;
}
float arch_win::get_area()
{
return area;
}
float arch_win::get_totalprice()
{
return totalprice;
}
And a main file ie:
Code:
// my_project.cpp
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctype.h>
#include "arch_win.h"
using namespace std;
int main()
{
float h=0, w=0, p=0; //set all float variables to 0
char aORr = '\0';
while ((!h&&!w)|| (h < w))
{
cout << "\nEnter Window Height: ";
cout.flush();
cin >> h;
cout << "\nEnter Window Width: ";
cout.flush();
cin >> w;
if (h < w)
cout << "Height must be greater than or Equal to Width!!\n";
}
cout << "\nEnter Price Per Sq in: ";
cout.flush();
cin >> p;
cout << "\nEnter A for Arch, R for Rectangle: ";
cout.flush();
cin >> aORr;
/* call the constructor which match the input */
arch_win AW(h, w, aORr);
AW.set_price(p);
//-------------------Formatting and Displaying Results----------------
cout << "\n\nHeight" << setw(7) << "Width" << setw(13) << "PPSqinch";
cout << setw(8) << "Area" << setw(15) << "Price\n";
cout << "___________________________________________________\n";
cout << setiosflags(ios::showpoint|ios::fixed) << setprecision(1)
/* get the height from our class */
<< AW.get_height();
cout << setw(8) << setprecision(1)
/* get width from class */
<< AW.get_width();
cout << setw(8) << setprecision(2) << "$"
/* get price from class */
<< AW.get_price();
cout << setw(10) << setprecision(1)
/* get area from class */
<< AW.get_area();
cout << setw(8) << setprecision(2) << "$"
/* get total price */
<< AW.get_totalprice();
cout << endl; /* flush any output */
/* halt user input */
cin >> aORr;
return 0;
}
Then compile it like:
~ > g++ -o project arch_win.cpp my_project.cpp
Bookmarks