Reading classes from a text file

closed account (iN8poG1T)
Hi I'm still new with classes, all i want to do is to read a text file and output it as a shape. The thing is, i already put the ifstream but i didnt know how to continue, i only succeed doing the shape but for rectangle, square and triangle is not that i wanted it to be

THIS IS MY TEXT FILE

6 25
2 4
2 2
3 8

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

class Shape
{
private :
    int x;
    int y;

public :
    Shape();
    Shape(int newx, int newy);
    int getX ();
    int getY();
    void setX(int newx);
    void setY (int newy);
    virtual void draw();


};

Shape :: Shape (){}
Shape :: Shape(int newx, int newy){}
int Shape :: getX(){return x;}
int Shape :: getY(){return y;}
void Shape :: setX(int newx){x= newx;}
void Shape :: setY(int newy){y= newy;}
void Shape :: draw(){
    {
        for(int i=0; i<x; i++)
        {
            for(int j=0; j<y; j++)
                cout << ".";
            cout<<endl;
        }
    }
}

class Rectangle : public Shape
{
private:
   int width;
   int height;
public:
   Rectangle();
   Rectangle(int newx, int newy, int widthR, int heightR);
   int getWidth();
   int getHeight();
   void setWidth(int widthR);
   void setHeight(int heightR);
   void draw();

};

Rectangle :: Rectangle (){};
Rectangle::Rectangle(int newx, int newy, int widthR, int heightR): Shape(newx, newy) {
   setWidth(widthR);
   setHeight(heightR);
}

int Rectangle :: getWidth(){return width;}
int Rectangle :: getHeight(){return height;}
void Rectangle::setWidth(int widthR) { width = widthR; }
void Rectangle::setHeight(int heightR) { height = heightR;}

void Rectangle::draw() {
    for(int i=0; i<height; i++)
        {
            for(int j=0; j<width; j++)
            cout << "B";
        cout<<endl;
        }

}

class Square: public Shape
{
private:
   int width_square;
   int height_square;
public:
   Square();
   Square(int newx, int newy, int heightS,int widthS);
   int getHeightS();
   int getWidthS();
   void setHeightS(int heightS);
   void setWidthS(int widthS);
   void draw();

};

Square :: Square (){};
Square::Square(int newx, int newy, int heightS, int widthS): Shape(newx, newy) {
   setHeightS(heightS);
   setWidthS(widthS);
}

int Square :: getHeightS(){return height_square;}
int Square :: getWidthS(){return width_square;}
void Square::setHeightS(int heightS) { height_square = heightS;}
void Square::setWidthS (int widthS) { width_square = widthS; }


void Square :: draw ()
{
    for (int i=1; i<=width_square; i++)
    {
        for (int j=1; j<=width_square; j++)
            cout << "C";
        cout << endl;
    }
}

class Triangle: public Shape
{
private:
   int height_T;
   int width_T;
public:
   Triangle();
   Triangle(int newx, int newy, int heightT,int widthT);
   int getHeightT();
   int getWidthT();
   void setHeightT(int heightT);
   void setWidthT(int widthT);
   void draw();

};

Triangle::Triangle(int newx, int newy, int heightT, int widthT): Shape(newx, newy) {
   setHeightT(heightT);
   setWidthT(widthT);
}

int Triangle :: getHeightT(){return height_T;}
int Triangle:: getWidthT(){return width_T;}
void Triangle::setHeightT(int heightT) { height_T = heightT;}
void Triangle::setWidthT (int widthT) { width_T = widthT; }


void Triangle :: draw ()
{
    for(int i = 1, k = 0; i <= width_T; ++i, k = 0)
    {
        for(height_T = 1; height_T <= width_T-i; ++height_T)
        {
            cout <<"  ";
        }

        while(k != 2*i-1)
        {
            cout << "* ";
            ++k;
        }
        cout << endl;
    }
}



int main()
{
    int height = 0;
    int length= 0;
    int count = 0;

    Shape temp(0,0);
    ifstream source("Batch.txt");

    if (source.is_open())
    {

       while (source >>height >> length)
        {
           Shape s;
           s.setX(height);
           s.setY(length);
           s.draw();

           Rectangle r;
           r.setWidth(length);
           r.setHeight(height);
           r.draw();

        }
    }
    else
      cout << "Source file failed to open.\n";
    source.close();

    return 0;
}
Your program doesn't make sense.
How can you know what shape the numbers represent?
What shape is "shape"? It seems to just be a rectangle. Usually it wouldn't be any shape at all and would have pure virtual functions.
Why does "square" have a width and a height. Wouldn't they always be the same?

You don't seem to know anything at all about how to write a program like this. Don't you have a book/example that you are following? Or are you just making it up as you go along?

The usual idea is to load the shapes into a vector of Shape pointers and then loop through the vector, calling the draw function on each shape.
Last edited on
closed account (iN8poG1T)
actually the question is about inheritance. i need to do shape inheritance. yes i edit my square shape and how can i output my program into different shape? for example, rectangle, square and triangle? at one time. Thank you
Last edited on
actually the question is about inheritance

Actually the question seems to be about run-time polymorphism.

how can i output my program into different shape

I have no idea what that means.

You haven't answered my questions:
1. What is shape supposed to be? It seems to be a rectangle. Does that make sense to you?
2. How can you tell what shape the input lines refer to? They are all just two numbers.
3. Are you just making it up as you go along or do you have an example/book/anything?

Last edited on
closed account (iN8poG1T)
the output of the question should be like this

........BBBBB..........D............
...A...BBBBB........DDD..........
..AAA......CCC...DDDDD.........
.AAAAA...CCC..DDDDDDD.......
.............CCC.DDDDDDDDD....
.................DDDDDDDDDDD...


.............. -> indicate the shape
B-> indicate the rectangle
C->indicate square
A-> indicate triangle

i should use the input value that store from batch.txt and the shape depends in the input txt file.

my teacher said we need to create shape triangle etc and inherit it from shape base class. but, i stuck in combining the shape and also reading the input text file





closed account (iN8poG1T)
this is my updated code

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

class Shape
{
private :
    int x;
    int y;

public :
    Shape();
    Shape(int newx, int newy);
    int getX ();
    int getY();
    void setX(int newx);
    void setY (int newy);
    void draw();
    void randomShape();


};

Shape :: Shape (){}
Shape :: Shape(int newx, int newy){}
int Shape :: getX(){return x;}
int Shape :: getY(){return y;}
void Shape :: setX(int newx){x= newx;}
void Shape :: setY(int newy){y= newy;}
void Shape :: randomShape (){
    {
        x=rand();
        for( int i = 0; i < 100; ++i )
        {
            cout << ( rand() % 10 ) + 1 << " ";
        }
        cout << endl;
    }
}

void Shape :: draw(){
    {
        for(int i=0; i<x; i++)
        {
            for(int j=0; j<y; j++)
                cout << ".";
            cout<<endl;
        }
    }
}

class Rectangle : public Shape
{
private:
   int width;
   int height;
public:
   Rectangle();
   Rectangle(int newx, int newy, int widthR, int heightR);
   int getWidth();
   int getHeight();
   void setWidth(int widthR);
   void setHeight(int heightR);
   void draw();

};

Rectangle :: Rectangle (){};
Rectangle::Rectangle(int newx, int newy, int widthR, int heightR):Shape(newx, newy) {
   setWidth(widthR);
   setHeight(heightR);
}

int Rectangle :: getWidth(){return width;}
int Rectangle :: getHeight(){return height;}
void Rectangle::setWidth(int widthR) { width = widthR; }
void Rectangle::setHeight(int heightR) { height = heightR;}

void Rectangle::draw() {
    for(int i=0; i<height; i++)
        {
            for(int j=0; j<width; j++)
            cout << "B";
        cout<<endl;
        }

}

class Square: public Shape
{
private:
   int height_square;
public:
   Square();
   Square(int newx, int newy, int heightS);
   int getHeightS();
   void setHeightS(int heightS);
   void draw();

};

Square :: Square (){};
Square::Square(int newx, int newy, int heightS): Shape(newx, newy) {
   setHeightS(heightS);
}

int Square :: getHeightS(){return height_square;}
void Square::setHeightS(int heightS) { height_square = heightS;}


void Square :: draw ()
{
    for (int i=1; i<=height_square; i++)
    {

        for (int j=1; j<=height_square; j++)
            cout << "C";
        cout << endl;
    }
}

class Triangle: public Shape
{
private:
   int height_T;
   int width_T;
public:
   Triangle();
   Triangle(int newx, int newy, int heightT,int widthT);
   int getHeightT();
   int getWidthT();
   void setHeightT(int heightT);
   void setWidthT(int widthT);
   void draw();

};

Triangle::Triangle(int newx, int newy, int heightT, int widthT): Shape(newx, newy) {
   setHeightT(heightT);
   setWidthT(widthT);
}
Triangle :: Triangle (){};
int Triangle :: getHeightT(){return height_T;}
int Triangle:: getWidthT(){return width_T;}
void Triangle::setHeightT(int heightT) { height_T = heightT;}
void Triangle::setWidthT (int widthT) { width_T = widthT; }


void Triangle :: draw ()
{
    int space;
    for(int i = 1, k = 0; i <= width_T; ++i, k = 0)
    {
        for(space = 1; space <= width_T-i; ++space)
        {
            cout <<" ";
        }

        while(k != 2*i-1)
        {
            cout << "A";
            ++k;
        }
        cout << endl;
    }


}



int main()
{

// i stuck at this point in where should i output my random placement that should look like the question

}



PSA : the question is Use random placement to place the Shape objects in the area. For each Shape object,
randomly choose one spot from all the viable placement spots left in the area to place the
Shape object.
Last edited on
Duplicate:

http://www.cplusplus.com/forum/beginner/248650/

Please don't do this, it's a waste of time for those who reply.
closed account (iN8poG1T)
ok im sorry
The way you describe your assignment makes no sense to me.
Maybe someone else can figure it out.

If you have a link to an online description, you should post it.
Topic archived. No new replies allowed.