Classes and Objects - 2

Fourth, the experimental conclusion

1. Experiment content 2

class definition

//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

 

class implementation

//graph.cpp

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;
 for( i=0;i<size;i++)
 {
  for(int j=size-i-1;j>=0;j--)
   cout<<" ";
  for(int j=0;j<=2*i ;j++)
   cout<<symbol;
  cout<<endl;
 }
}

 

main function

//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 of running result

The operating environment is devc++

2. Experiment content 3

class definition

//fraction.h
class fraction
{
    public:
        fraction();
        fraction(int x,int y);
        fraction(int x);
        void show();
        void add(fraction &b);//
        void min(fraction &b);//
        void mul(fraction &b);//
        void div(fraction &b);//
        void cmp(fraction &b,fraction &c);//比较 
    private:
        int top;
        int bottom;
};

 

class implementation

//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::show()
{
 cout<<top<<'/'<<bottom<<endl;
}

void fraction::add(fraction &b)

 fraction a;
 a.top=top*b.bottom+b.top*bottom;
 a.bottom=bottom*b.bottom;
 a.show();
}

void fraction::min(fraction &b)
{
 fraction a;
 a.top=top*b.bottom-b.top*bottom;
 a.bottom=bottom*b.bottom;
 a.show(); 
}
void fraction::mul(fraction &b)
{
 fraction a;
 a.top=top*b.top;
 a.bottom=bottom*b.bottom;
 a.show(); 
}
void fraction::div(fraction &b)
{
 fraction a;
 a.top=top*b.bottom;
 a.bottom=bottom*b.top; 
 a.show();
}
void fraction::cmp(fraction &b,fraction &c)
{
 double m,n;
 m=b.top/b.bottom;
 n=c.top/c.bottom;
 if(m<n)
 {
  cout <<b.top<<'/'<<b.bottom<<'<';c.show();cout<<endl;
 }
 else if(m==n)
 {
  cout <<b.top<<'/'<<b.bottom<<'=';c.show();cout<<endl;
 } 
 else
 {
  cout <<b.top<<'/'<<b.bottom<<'>';c.show();cout<<endl;
 }
}

 

 

main function

//main.cpp

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

int main()
{
 fraction a;
 fraction b(3,4);
 fraction c(5);
 a.show();
 b.show();
 c.show();
 c.add(b);
 c.min(b);
 c.mul(b);
 c.div(b);
 b.cmp(b,c);
 return 0;
}

 

Screenshot of running result

 

The operating environment is devc++

V. Experiment summary and experience

Divide a complete program about classes and objects into several parts to make a project. The "class implementation" unit needs to include the header file #include<iostream>, as well as the "class implementation" and "main function" units. need to include the "class definition" unit in the header file.

Other than that, there are no new knowledge points compared to the previous homework.

 

Guess you like

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