#include<iostream>
using namespace std;
// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
};
// 类graph的实现
// 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}
// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
// size和symbol是类Graph的私有成员数据
void Graph::draw() {
// 补足代码,实现「实验4.pdf」文档中展示的图形样式
for(int j=1;j<=size;j++) //j控制行
{
for(int i=1;i<=size-j+1;i++) cout<<' '; //i控制每行中的每个数
for(int i=1;i<=2*j-1;i++) cout<<symbol;
cout<<endl;
}
}
int main() {
Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2
graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形
graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
return 0;
}
//graph.h
#ifndef GRAPH_H
#define GRAPH_H
// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
};
#endif
// 类graph的实现,graph.cpp
#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() {
// 补足代码,实现「实验4.pdf」文档中展示的图形样式
for(int j=1;j<=size;j++)
{
for(int i=size;i>=1;i--) cout<<' ';
for(int i=1;i<=2*size-1;i++) cout<<symbol;
cout<<endl;
}
}
//主函数,main.cpp
#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;
}
#include<iostream> using namespace std; class Fraction{ public: Fraction(); Fraction(int t,int b); Fraction(int b); Fraction(Fraction &f0); void show(); void add(Fraction &f0); void min(Fraction &f0); void mul(Fraction &f0); void div(Fraction &f0); void comp(Fraction &f0); private: int top; int bottom; }; Fraction::Fraction():top(0),bottom(1){} Fraction::Fraction(int t,int b):top(t),bottom(b){} Fraction::Fraction(int b):top(1),bottom(b){} void Fraction::show(){ cout<<top<<"/"<<bottom<<endl; } void Fraction::add(Fraction &f0){ top=top*f0.bottom+f0.top*bottom; bottom=bottom*f0.bottom; show(); } void Fraction::min(Fraction &f0){ top=top*f0.bottom-f0.top*bottom; bottom=bottom*f0.bottom; show(); } void Fraction::mul(Fraction &f0){ top=top*f0.top; bottom=bottom*f0.bottom; show(); } void Fraction::div(Fraction &f0){ top=top*f0.bottom; bottom=bottom*f0.top; show(); } void Fraction::comp(Fraction &f0){ double i,j; i=top/bottom; j=f0.top/f0.bottom; if(i>j) cout<<i<<">"<<j; else if(i<j) cout<<i<<"<"<<j; else cout<<i<<"="<<j; } int main(){ Fraction a; Fraction b(3,4); Fraction c(5); a.show(); b.show(); c.show(); b.add(c); b.min(c); b.mul(c); b.div(c); b.comp(c); return 0; }
我的运行结果,,,前面都正常,但是后面的减法用的是上面那个加法的结果,乘法用的是减法的,除法用的是乘法的,求教大佬,如何让上一函数的结果不影响到下面?
还有一种《我不写参量对象f0,在各类函数中具体化对象运算,应该就不会这么烦吧。。。》