#include#include "graph.h"using namespace std;int main() { Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2 graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形 graph1.setcolor('y','g'); graph1.draw(); graph2.setcolor('r','w'); graph2.draw(); return 0; }
// 类graph的实现 #include "graph.h" #include#include using namespace std;// 带参数的构造函数的实现 Graph::Graph(char ch, int n): symbol(ch), size(n) {}void Graph::draw(){ int i,j; for(i=0;i =size-i&&j<=size+i) cout<
// 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 void setcolor(char fore,char back); private: char symbol; int size; unsigned forecolor,backcolor; int str(char a); };
#include"Fraction.h"#include#include using namespace std;int Fraction::gct(int a,int b){ return a%b==0?b:gct(b,a%b);}Fraction::Fraction():top(0),bottom(1){}Fraction::Fraction(int t):top(t),bottom(1){}Fraction::Fraction(int t,int b):top(t),bottom(b){}void Fraction::add(Fraction &p){ top=top*p.bottom+p.top*bottom; bottom=bottom*p.bottom; form(); op();}void Fraction::sub(Fraction &p){ top=top*p.bottom-p.top*bottom; bottom=bottom*p.bottom; form(); op();}void Fraction::mul(Fraction &p){ top=top*p.top; bottom=bottom*p.bottom; form(); op();}void Fraction::div(Fraction &p){ top=top*p.bottom; bottom=p.top*bottom; form(); op();}void Fraction::form(){ if(bottom<0){ bottom*=-1; top*=-1; } int g=gct(abs(top),abs(bottom)); top/=g; bottom/=g;}void Fraction::ip(){ cout<<"输入分子"< >top; int bo; cout<<"输入分母"< >bo; while(bo==0){ cout<<"请重新输入"< >bo; } bottom=bo;}void Fraction::op(){ form(); if(bottom!=1) cout< <<"/"< <
#include"Fraction.h"#include#include using namespace std;int main(){ Fraction a; Fraction b(3,4); Fraction c(5); cout<<"输出a的初值"<
class Fraction { private: int top; int bottom; int gct(int a,int b); public: Fraction(); Fraction(int t); Fraction(int t,int b); void add(Fraction &p); void sub(Fraction &p); void mul(Fraction &p); void div(Fraction &p); void for
m(); void ip(); void op(); };