// 類graph的實現
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 帶參數的構造函數的實現 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成員函數draw()的實現
// 功能:繪制size行,顯示字符為symbol的指定圖形樣式 
//       size和symbol是類Graph的私有成員數據 
void Graph::draw() {
    int x,y,z; 
    for(int x=1;x<=size;x++){
        for(int y=1;y<=size-x;y++)
            cout<<" ";
        for(int z=1;z<=2*x-1;z++)   //使每行個數為等差奇數 
            cout<<symbol;
        cout<<endl;
        }
    // 補足代碼,實現「實驗4.pdf」文檔中展示的圖形樣式 
}
#ifndef GRAPH_H
#define GRAPH_H

// 類Graph的聲明 
class Graph {
    public:
        Graph(char ch, int n);   // 帶有參數的構造函數 
        void draw();     // 繪制圖形 
    private:
        char symbol;
        int size;
};


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


int main() {
    Graph graph1('*',5), graph2('$',7) ;  // 定義Graph類對象graph1, graph2 
    graph1.draw(); // 通過對象graph1調用公共接口draw()在屏幕上繪制圖形 
    graph2.draw(); // 通過對象graph2調用公共接口draw()在屏幕上繪制圖形
    
    return 0; 
} 

內容三

//fraction.h
class Fraction{
    public:
        Fraction(int t=0,int b=1);
        void show();
        void jia(Fraction &x);
        void jian(Fraction &x);
        void cheng(Fraction &x);
        void chu(Fraction &x); 
    private:
        int top;      //分子 
        int bottom;   //分母 
}; 
//fraction.cpp
#include"fraction.h"
#include<iostream>
 using namespace std;
Fraction::Fraction(int t,int b):top(t),bottom(b){
}
void Fraction::jia(Fraction &x){
 top=top*x.bottom+x.top*bottom;
 bottom=bottom*x.bottom;
} 
void Fraction::jian(Fraction &x){
 top=top*x.bottom-x.top*bottom;
 bottom=bottom*x.bottom;
}
void Fraction::cheng(Fraction &x){
 top=top*x.top;
 bottom=bottom*x.bottom;
}
void Fraction::chu(Fraction &x){
 top=top*x.bottom;
 bottom=bottom*x.top;
}
void Fraction::show(){
 cout<<top<<'/'<<bottom<<endl;
}
//main.cpp 
#include"fraction.h"
#include <iostream>
using namespace std;
int main() {
 Fraction a;
 Fraction b(3,4);
 Fraction c(5);
 a.show();b.show();
 c.show();
 a.jia(a);a.show();
 b.jian(b);b.show();
 c.cheng(c);c.show();
 c.chu(c);c.show();
 return 0;
}

小結:在做第二道題時,前幾次不知怎么的,運行的結果會向右對其,后來不知怎么弄的又能運行出正確的結果了,可是我并沒有改變代碼啊,求解

得出的錯誤結果如右圖