如何归档i / o成员函数,或重载&lt; <operator?

时间:2016-03-09 19:20:06

标签: c++ file-io operator-overloading member-functions

=“”

所以我在编写程序之前首先编写了我的程序以在命令提示符下工作将其输出到文件中。它在命令提示符下完美运行,但在尝试将成员函数数据输出到文件时遇到了重大问题。在momemnt来循环这个简短的命令,我只是在main中声明了一个int temp变量来存储get函数值。然后我归档我需要的每个值的临时值。然而,有一个函数(define)获取长度和宽度值,并确定它是否为正方形或是否为矩形。这在命令行中运行得很好,但它只是说输出文件中的所有内容都是正方形。

我听说过通过重载(&lt;&lt;&lt;运算符)直接输出成员函数的方法,但是当我查看书中的示例时,我无法解决语法问题。如果我能够写“box_out&lt;&lt; wrap();”这将解决我的所有问题。 wrap()函数将所有值包装到一个语句中并显示消息。

基本上我需要帮助的是要么改进我的档案,要么重载&lt;&lt;允许我说box_out&lt;运算符的运算符

      /*
  c++ programming
  Asmt2
  box_new
  Takes in defined object parameters and user defined parameters and gives you the lenth width area parameter and whether or not it is a square.
  *-*-*Stand-alone file*-*-*
  */
  #include <fstream>
  #include <iostream>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string>
  #include <cmath>
  using namespace std;

  class Rectangle{
    public:
      Rectangle();
      Rectangle(float len);
      Rectangle(float len, float wid);
      ~Rectangle();


      void setLength(float len);
      void setWidth(float wid);
      float getLength();
      float getWidth();
      float getPerimeter(void);
      float getArea(void);
      int define(float width, float length);
      void wrap();

      friend std::ostream &operator << ( std::ostream &, Rectangle & );
    private:
      float length;
      float width;
      float area;
      float Perimeter;

    };


    Rectangle::Rectangle(){
      setLength(1.0);
      setWidth(1.0);
    }

    Rectangle::Rectangle(float length){
      setLength(length);
      setWidth(1.0);

    }

    Rectangle::Rectangle(float length, float width){
      setLength(length);
      setWidth(width);
    }



      void Rectangle:: setLength (float len){
        if(len > 0.0 && len < 20.0){
          length = len;
        }else{
          cout << "\n*-*-*Invalid input setting to default setting of 1.0*-*-*"<<endl;
          len = 1.0;
          length = len;
        }
      }



      void Rectangle:: setWidth (float wid){
        if(wid > 0.0 && wid < 20.0){
          width = wid;
        }else{
          cout << "\n*-*-*Invalid input setting to default setting of 1.0*-*-*"<<endl;
          wid = 1.0;
          width = wid;
        }
      }


      float Rectangle:: getLength(){
        return length;
      }

      float Rectangle:: getWidth(){
        return width;
      }

      float Rectangle:: getPerimeter (void){
        getLength();
        getWidth();
        Perimeter = (2 * length) + (2 * width);
        return Perimeter;
      }

      float Rectangle:: getArea (void){
        area = length * width;
        return area;
      }

      int Rectangle:: define (float width, float length){
        if(fabs(width - length) < .0001){
          cout <<"You have a Square!"<<endl;
          return 1;
        }else{
          cout <<"You have a Rectangle!"<<endl;
          return 2;
        }
      }

      void Rectangle:: wrap(){
        cout << "The length is: " << length << endl << "The width is: " << width << endl;
        cout << "The box has a Perimeter of: "<< getPerimeter() << endl << "The box has an area of: " << getArea() << endl;
        define( length, width);
      }

      Rectangle::~Rectangle(){
        cout << "The object with a lenth of " << length << " and a width of " << width << " has gone out of scope. \n";
      }
      ostream &operator << (ostream &output, Rectangle &display){
            output << "Your lenth is: " << display.getLength()<< "Your width is: " << display.getWidth()
                   << "Your area is: " << display.getArea() << "Your Perimeter is: " << display.getPerimeter();
          }



  int main(void){

    ofstream box_out;
    box_out.open("box_out.txt");

    Rectangle box1;
    Rectangle box2(7.1, 3.2);
    Rectangle box3(6.3);
    Rectangle box4(30,50);
    Rectangle box5=box2;
    Rectangle box6;//user defined

  /*  float Perimeter = 0;
    float area = 0;
    float length = 0;
    float width = 0;*/

    float temp = 15;

  //first box
    cout << "*-*-*Your first box*-*-*\n";
    box1.wrap();
  //file output
    box_out << "*-*-*Your fist box!*-*-*";
    temp = box1.getLength();
    box_out << "   Your Length is: " << temp << "\n";
    temp = box1.getWidth();
    box_out << "   Your Width is: " << temp << "\n";
    temp = box1.define(box1.getLength(), box1.getWidth());
    if(temp == 1){
      box_out << "   You have a Square!";
    }else if(temp == 2){
      box_out << "   You have a Rectangle!";
    }
    temp = box1.getPerimeter();
    box_out <<"   Your Perimeter is: " << temp;
    temp = box1.getArea();
    box_out <<"   Your Area is: " << temp;
  //second box
    cout << "\n*-*-*Your second box*-*-*\n";
    box2.wrap();
  //file outout
    box_out << "   *-*-*Your second box!*-*-*";
    temp = box2.getLength();
    box_out << "   Your Length is: " << temp << "\n";
    temp = box2.getWidth();
    box_out << "   Your Width is: " << temp << "\n";
    temp = box2.define(box1.getLength(), box1.getWidth());
    if(temp == 1){
      box_out << "   You have a Square!";
    }else if(temp == 2){
      box_out << "   You have a Rectangle!";
    }
    temp = box2.getPerimeter();
    box_out <<"   Your Perimeter is: " << temp;
    temp = box2.getArea();
    box_out <<"   Your Area is: " << temp;
  //third box
    cout << "\n*-*-*Your third box*-*-*\n";
    box3.wrap();
  //file output
    box_out << "   *-*-*Your third box!*-*-*";
    temp = box3.getLength();
    box_out << "   Your Length is: " << temp << "\n";
    temp = box3.getWidth();
    box_out << "   Your Width is: " << temp << "\n";
    temp = box3.define(box1.getLength(), box1.getWidth());
    if(temp == 1){
      box_out << "   You have a Square!";
    }else if(temp == 2){
      box_out << "   You have a Rectangle!";
    }
    temp = box3.getPerimeter();
    box_out <<"   Your Perimeter is: " << temp;
    temp = box3.getArea();
    box_out <<"   Your Area is: " << temp;
  //fourth box
    cout << "\n*-*-*Your fourth box*-*-*\n";
    box4.wrap();
  //file output
    box_out << "   *-*-*Your fourth box!*-*-*";
    temp = box4.getLength();
    box_out << "   Your Length is: " << temp << "\n";
    temp = box4.getWidth();
    box_out << "   Your Width is: " << temp << "\n";
    temp = box4.define(box1.getLength(), box1.getWidth());
    if(temp == 1){
      box_out << "   You have a Square!";
    }else if(temp == 2){
      box_out << "   You have a Rectangle!";
    }
    temp = box4.getPerimeter();
    box_out <<"   Your Perimeter is: " << temp;
    temp = box4.getArea();
    box_out <<"   Your Area is: " << temp;
  //fifth box
    cout << "\n*-*-*Your fifth box*-*-*\n";
    box5.wrap();
  //file output
    box_out << "   *-*-*Your fifth box!*-*-*";
    temp = box5.getLength();
    box_out << "   Your Length is: " << temp << "\n";
    temp = box5.getWidth();
    box_out << "   Your Width is: " << temp << "\n";
    temp = box5.define(box1.getLength(), box1.getWidth());
    if(temp == 1){
      box_out << "   You have a Square!";
    }else if(temp == 2){
      box_out << "   You have a Rectangle!";
    }
    temp = box5.getPerimeter();
    box_out <<"   Your Perimeter is: " << temp;
    temp = box5.getArea();
    box_out <<"   Your Area is: " << temp;


    cout << "\n*-*-*User-defined box*-*-*\n";
    cout << "Enter your Length dimension"<<endl;
    cin >>temp;
    box6.setLength(temp);
    cout << "Enter your Width dimension"<<endl;
    cin >>temp;
    box6.setWidth(temp);

    box6.define(box6.getLength(),box6.getWidth());

    cout << "Perimeter of box is : " << box6.getPerimeter() <<endl;


    cout << "Area of Box is: " << box6.getArea() <<endl;



    return 0;
  }

0 个答案:

没有答案
相关问题