Experiment 4 Classes and Objects 2

1. Experiment content 2

graph.h

#ifndef GRAPH_H
#define GRAPH_H

// Declaration of class Graph 
class Graph {
     public :
        Graph( char ch, int n);    // Constructor with parameters 
        void draw();      // Draw graph 
    private :
         char symbol;
         int size;
};


#endif

graph.cpp

// implementation of class graph 
#include " graph.h "  
#include <iostream>
 using  namespace std;
 // implementation of constructor with parameters 
Graph::Graph( char ch, int n): symbol(ch), size( n) {
}
// implementation of member function draw()
 // Function: draw size line, display the specified graphic style with the character as symbol 
 //        size and symbol are private member data of class Graph 
void Graph::draw() {
     int i,j;
     for (i= 0 ;i<size;i++ ){
         for (j= 1 ;j<= 2 *size- 1 ;j++ ){
             if (j<=size+i&&j>=size- i)
            cout<<symbol;
            else 
            cout<<" ";
        }cout<<endl;
    }                                            
}

main.cpp

#include <iostream>
#include "graph.h"
using namespace std;


int main() {
    Graph graph1( ' * ' , 5 ), graph2( ' $ ' , 7 ) ;   // Define Graph class objects graph1, graph2 
    graph1.draw(); // Call the public interface draw() through the object graph1 to draw the graph on the screen 
    graph2.draw(); // Draw the graph on the screen by calling the public interface draw() through the object graph2
    
    return 0; 
}

screenshot

2. Experiment content 3

Fraction.h

class Fraction {
public:
    Fraction();            
    Fraction(int x, int y);
    Fraction( int x);                  
     void p(Fraction &f1); // addition 
    void q(Fraction &f1); // subtraction 
    void r(Fraction &f1); // multiplication 
    void t(Fraction &f1); // division 
    void o(Fraction f1, Fraction f2); // Compare size 
    void s(); // Output 
private :
     int top; 
     int bottom;   
};

Fraction.cpp

#include<iostream>
#include"Fraction.h"
using namespace std;
Fraction::Fraction() {      
    top = 0; bottom = 1;
}
Fraction::Fraction(int x, int y) { 
    top = x; bottom = y;
}
Fraction::Fraction(int x) {        
    top = x; bottom = 1;
}
void Fraction::p(Fraction &f1) {   //加法 
     Fraction f2;
     f2.top = top * f1.bottom + f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.s();
}
 void Fraction::q(Fraction &f1) {  //减法 
     Fraction f2;
     f2.top = top * f1.bottom - f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.s();
 }
 void Fraction::r(Fraction &f1) {  //乘法 
     Fraction f2;
     f2.top =top*f1.top;
     f2.bottom =bottom*f1.bottom;
     f2.s();
 }
 void Fraction::t(Fraction &f1) {  //除法 
     Fraction f2;
     f2.top =top*f1.bottom;
     f2.bottom = bottom * f1.top;
     f2.s();
 }
 void Fraction::o(Fraction f1, Fraction f2)//比较大小 
 {
     float a = float(f1.top) / float(f1.bottom); float b = float(f2.top) / float(f2.bottom);
     if (a<b)
     {
         cout<<f1.top<<"/"<<f1.bottom<<"<";
         f2.s();
         cout<<endl;
     }
    else if (a>b)
      {
          cout<<f1.top<<"/"<<f1.bottom<<">";
          f2.s();
          cout<<endl;
      }
    else if (a=b)
      {
          cout<<f1.top<<"/"<<f1.bottom<< "=";
          f2.s();
          cout<<endl;
      }  
  }
  void Fraction::s(){  //输出 
      cout<<top<<"/"<<bottom<<endl;
  }

main.cpp

#include<iostream>
#include"Fraction.h"
using namespace std;
int main()
{
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    a.s();
    b.s();
    c.s();
    Fraction d(1,3);
    Fraction e(2,3);
    d.p(e);
    d.q(e);
    d.r(e);
    d.t(e);
    to the);
    return 0;
}

screenshot

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730772&siteId=291194637