Help With Classes

I'm trying to write a class to figure out the parameters for a rectangle or arch window. I need some help with the class. I'm having some difficulty calling variables defined in one method to another method in the same class.

Any help is appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <ctype.h>
#include <math.h>
//#inlcude Arch_Rect_Win.h

using namespace std;

class Arch_Rect_Win
{
    private:
        double pi;
       
    public:
        Arch_Rect_Win(double =0);
        void setWidth(double);
        void setHeight(double);
        void setPPSI(double);
        double getHeight() const;
        double getWidth() const;
        double getArea() const;
        double getPrice() const;
        double getPricePSqIn() const;
        
};

int main ()

{
    Arch_Rect_Win aWin;
    char h[10], w[10], p[10];
    char rORa[10];
    int validData;
    
    //textcolor(BLUE);
    //textbackground(LIGHTGRAY);
    
    system("CLS");
    
    do
    {
        cout << "\n\t ENter Window Height: ";
        cin >> h;
        cout << "\t Enter Window Width: ";
        cin >> w;
        cout << "\tEnter Price Per Sq In: ";
        cin >> p;
        cout << "\tEnter ""A"" for ARch, ""R"" for Rectangel: ";
        cin >> rORa;
        validData = aWin.setData(h,w,p rORa);
        
        if (validData !=0)
        {
            cout << "\n\tError in Data!";
            cout << "\n\tMust enter either ""A"" for ARch or ""R"" for Rect";
            cout << "\n\t\tIf Arch Windows, height must be >= half of the width ";
            cout << "for arch window\n";
        }
    }while (validData !=0);
    
    system("CLS");
    
    if (aWin.getRectOrArch() == 'A')
        cout << "\n\t\t* * * Arch Window * * *\n";
    else
        cout << "\n\t\t* * * Rectangle Windows * * *\n";
        
    cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
    cout << "\n\t           Height: " << aWin.getHeight();
    cout << "\n\t            Width: " << aWin.getWidth();
    cout << "\n\t  Price Per Sq In: " << aWin.getPricePSqIn();
    cout << "\n\t       Total Area: " << aWin.getArea();
    cout << "\n\t      Total Price: " << aWin.getPrice();
    getch();
} 

Arch_Rect_Win::Arch_Rect_Win(double height, double width, double ppsi1)
{
    setHeight(height);
    setWidth(width);
    setPPSI(ppsi1);
    pi = 3.14159;
}

void Arch_Rect_Win::setHeight(double height)
{
    if (height>=0)
    h=height;
}

void Arch_Rect_Win::setWidth() (double width)
{
    if (width>=0)
    w=width;
}

void Arch_Rect_Win::setPPSI() (double ppsi1)
{
    if (ppsi1>=0)
    p=ppsi1;
}

double Arch_Rect_Win::getHeight() const
{
    return height;
}

double Arch_Rect_Win::getWidth() const
{
    return width;
}

double Arch_Rect_Win::getPricePSqIn() const
{
    return ppsi1;
}

double Arch_Rect_Win::getArea() const
{
    double radius = width/2;
    double AreaofArch = (pow(radius, 2.0) * pi)/2
    double LengthRectangle = height - radius;
    double AreaofRectangle = LengthRectangle * width;
    double TotalArea = AreaofRectangle + AreaofArch;
    return TotalArea;
}

double Arch_Rect_Win::getPrice() const
{
    return ppsi1 * TotalArea;
}

Last edited on
You need to declare the data as a member of the function. For example:

Foo.h
1
2
3
4
5
6
7
8
9
10
class Foo
{
private:
int MyInt; //Declaring this data as part of the function allows you to access it in any of the classes functions.
float MyFloat;
public:
int GetInt(); //Both GetInt
void AddOneToInt(); //And AddOne would be able to access the same variable "MyInt".
float GetFloat();
};


So now in your .cpp file:

Foo.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Foo.h"

int Foo::GetInt()
{
return MyInt; //You can just call the variable by the name that it was given in the class definition.
}

void Foo::AddOneToInt()
{
++MyInt; //Here we are manipulating the exact same variable. If GetInt was called after AddOne was //called the returned value would be one bigger.
}


Just remember that if you put data in the public section of a classes members it can be accessed just like in a struct:
If MyInt was public this:

1
2
3
4
5
6
int main()
{
Foo Test;
Test.MyInt = 0; //Would be perfectly legal, and now a call to GetInt would return 0.
return 0;
}


EDIT:
So for your code you would want something like:

1
2
3
4
5
6
7
8
9
10
class Rect
{
private:
int width,height;
double pi;
//Any other private data or functions.
public:
//All your public functions, for example to access the private members.
};

Just keep in mind that if your class isn't too complicated it can just bloats the class to have special accessing functions for every variable. Just make them public.
Last edited on
Topic archived. No new replies allowed.