in out?

Discussion in 'Mac Programming' started by chris200x9, Dec 10, 2007.

  1. chris200x9 macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    Code:
     #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    class Shape
    {
    protected:
    	int x, y;
    public:
    	Shape();
    	Shape( Shape& ref );
    	void SetOrigin( const int xx, const int yy );
    	virtual void Display();
    	virtual double Area();
    	virtual void read ( ifstream& );
    
    };
    
    class Rectangle : public Shape
    {
    private:
    	int length, width;
    public:
    	Rectangle();
    	Rectangle( int l, int w );
    	void Display();
    	double Area();
    };
    
    class Triangle : public Shape
    {
    private:
    	int base, height;
    public:
    	Triangle();
    	Triangle( int b, int h );
    	void Display();
    	double Area();
    };
    
    class Circle : public Shape
    {
    private:
    	int radius;
    public:
    	Circle();
    	double Area();
    	void Display();
    	void read(ifstream&);
    };
    void  Circle::read ( ifstream& infile )
    	{
    		infile  >> x;
    			infile >> y;
    			infile >> radius;
    		}
    	
    
    Triangle::Triangle()
    {
    	base = 0;
    	height = 0;
    }
    
    Triangle::Triangle( int b, int h )
    {
    	base = b;
    	height = h;
    }
    
    double Triangle::Area()
    {
    	return( 0.5 * base * height );
    }
    
    void Triangle::Display()
    {
    	cout << "The triangle is at ";
    	cout << "( " << x << ", " << y << " )";
    	cout << endl;	
    }
    
    
    Rectangle::Rectangle()
    {
    	length = 0;
    	width = 0;
    }
    
    Rectangle::Rectangle( int l, int w )
    {
    	length = l;
    	width = w;
    }
    
    double Rectangle::Area()
    {
    	return( length * width );
    }
    
    void Rectangle::Display()
    {
    	cout << "The rectangle is at ";
    	cout << "( " << x << ", " << y << " )";
    	cout << endl;	
    }
    
    Circle::Circle()
    {
        radius = 0;
    }
    
    double Circle::Area()
    {
        return (3.14159*radius*radius);
    }
    
    void Circle::Display()
    {
        cout << "The circle is at ";
    	cout << "( " << x << ", " << y << " )";
    	cout << endl;	
    }
    
    double Shape::Area()
    {
    	return 0;
    }
    
    Shape::Shape()
    {
    	x = 0;
    	y = 0;
    }
    
    Shape::Shape( Shape& ref )
    {
    	x = ref.x;
    	y = ref.y;
    }
    
    void Shape::SetOrigin( const int xx, const int yy )
    {
    	x = xx;
    	y = yy;
    }
    
    void Shape::Display()
    {
    	cout << "The shape is located at ";
    	cout << "( " << x << ", " << y << " )";
    	cout << endl;
    }
    
    void Shape::read(ifstream&)
    {
    }
    
    int main()
    {
    	int shapenum;
    	string filename;
    	Shape* p[1000]; 
    	ifstream infile("infile.txt");
    		char c;
    	
    
    
    
    
    
    
    
    
    
    
    	while ( infile)
    	{
    		switch (c) {
    			case 'C':
    	p[shapenum] = new Circle();
    		break;
    			case 'R':
    	p[shapenum] = new Rectangle();
    		break;
    			case 'T':
    	p[shapenum] = new Triangle();
    		break;
    		}
    	}
    p[shapenum] -> read (infile);
    }
    
    I need some help filling out the code of what to do once the file is open.


    the program should run from a file with input:

    R 0 0 2 3
    T 0 0 2 4
    C 0 0 3

    The first line defines a rectangle with origin at (0, 0), length 2, and width 3. The second line defines a triangle with origin at (0,0), base 2, and height 4. The third line defines a circle with origin at (0,0) and radius 3.
     
  2. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    Try this modified code:
    Code:
     #include <iostream>
    #include <string>
    #include <fstream>
     
    using namespace std;
     
    class Shape
    {
    protected:
        int x, y;
    public:
        Shape();
        Shape( Shape& ref );
        void SetOrigin( const int xx, const int yy );
        virtual void Display();
        virtual double Area();
        virtual void read ( ifstream& );
     
    };
     
    class Rectangle : public Shape
    {
    private:
        int length, width;
    public:
        Rectangle();
        Rectangle( int l, int w );
        void Display();
        double Area();
    };
     
    class Triangle : public Shape
    {
    private:
        int base, height;
    public:
        Triangle();
        Triangle( int b, int h );
        void Display();
        double Area();
    };
     
    class Circle : public Shape
    {
    private:
        int radius;
    public:
        Circle();
        double Area();
        void Display();
        void read(ifstream&);
    };
    void  Circle::read ( ifstream& infile )
        {
            infile  >> x;
                infile >> y;
                infile >> radius;
            }
     
     
    Triangle::Triangle()
    {
        base = 0;
        height = 0;
    }
     
    Triangle::Triangle( int b, int h )
    {
        base = b;
        height = h;
    }
     
    double Triangle::Area()
    {
        return( 0.5 * base * height );
    }
     
    void Triangle::Display()
    {
        cout << "The triangle is at ";
        cout << "( " << x << ", " << y << " )";
        cout << endl;    
    }
     
    void  Triangle::read ( ifstream& infile )
        {
            infile  >> x;
                infile >> y;
                infile >> base;
                infile >> height;
            }
     
    Rectangle::Rectangle()
    {
        length = 0;
        width = 0;
    }
     
    Rectangle::Rectangle( int l, int w )
    {
        length = l;
        width = w;
    }
     
    double Rectangle::Area()
    {
        return( length * width );
    }
     
    void Rectangle::Display()
    {
        cout << "The rectangle is at ";
        cout << "( " << x << ", " << y << " )";
        cout << endl;    
    }
     
    void  Rectangle::read ( ifstream& infile )
        {
            infile  >> x;
                infile >> y;
                infile >> length;
                infile >> width;
            }
     
    Circle::Circle()
    {
        radius = 0;
    }
     
    double Circle::Area()
    {
        return (3.14159*radius*radius);
    }
     
    void Circle::Display()
    {
        cout << "The circle is at ";
        cout << "( " << x << ", " << y << " )";
        cout << endl;    
    }
     
    double Shape::Area()
    {
        return 0;
    }
     
    Shape::Shape()
    {
        x = 0;
        y = 0;
    }
     
    Shape::Shape( Shape& ref )
    {
        x = ref.x;
        y = ref.y;
    }
     
    void Shape::SetOrigin( const int xx, const int yy )
    {
        x = xx;
        y = yy;
    }
     
    void Shape::Display()
    {
        cout << "The shape is located at ";
        cout << "( " << x << ", " << y << " )";
        cout << endl;
    }
     
    void Shape::read(ifstream&)
    {
    }
     
    int main()
    {
        int shapenum;
        string filename;
        Shape* p[1000]; 
        ifstream infile("infile.txt");
            char c;
     
     
     
     
     
     
     
     
     
     
     
        while ( infile)
        {
            switch (c) {
                case 'C':
        p[shapenum] = new Circle();
            break;
                case 'R':
        p[shapenum] = new Rectangle();
            break;
                case 'T':
        p[shapenum] = new Triangle();
            break;
            }
        }
    p[shapenum] -> read (infile);
    }
    
    I added read methods for the Circle and Rectangle classes, based on the one you made for Triangle.
     

Share This Page