实 验 3:

part 1:验证

part 2:graph

#include <iostream>
#include "graph.h"
using namespace std; int main() {
Graph graph1('*',);
graph1.draw(); system("pause");
system("cls"); Graph graph2('$',);
graph2.draw();
system("pause"); return ;
}

main.cpp

// 类graph的实现

#include "graph.h"
#include <iostream>
using namespace std; // 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
} // 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
void Graph::draw() {
for(int i=;i<=size;i++)
{for(int j=;j<=size-i;j++)
cout<<" ";
for(int k=;k<*i-;k++)
cout<<symbol;
cout<<endl;
}
}

graph.cpp

#ifndef GRAPH_H
#define GRAPH_H // 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
}; #endif

graph.h

运行结果:

part 3:分数的加减乘除

#ifndef Fraction_H
#define Fraction_H
// 类Fraction的声明
class Fraction {
public:
Fraction(int t=, int b=):top(t),bottom(b){
}// 带有参数的构造函数
void add(Fraction a, Fraction b);
void sub(Fraction a, Fraction b);
void mul(Fraction a, Fraction b);
void div(Fraction a, Fraction b);
void compare(Fraction a, Fraction b);
void show();
private:
int top;
int bottom;
}; #endif

fraction.h

#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{int x,y,m,n;
Fraction a;
a.show();
Fraction b(,);
b.show();
Fraction c();
c.show();
cout<<"Input two score: "<<endl;
cin>>x>>y;
cin>>m>>n;
while(y==||n==)
{cout<<"ERROR!Please try again!"<<endl;
cin>>x>>y;
cin>>m>>n;
}
Fraction d(x,y);
Fraction e(m,n);
a.add(d,e);
a.sub(d,e);
a.mul(d,e);
a.div(d,e);
a.compare(d,e);
return ;
}

main.cpp

#include"fraction.h"
#include<iostream>
using namespace std;
void Fraction::show(){
if (top == ) cout << <<endl;
else if (bottom == ) cout << top << endl;
else cout << top << "/" << bottom << endl;
}
void Fraction::add(Fraction a, Fraction b){
int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
gbs=a.bottom*b.bottom/n;
fz=a.top*(gbs/a.bottom)+b.top*(gbs/b.bottom);
fm=gbs;
cout<<"add: "<<fz<<"/"<<fm<<endl;
}
void Fraction::sub(Fraction a, Fraction b){
int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
gbs=a.bottom*b.bottom/n;
fz=a.top*(gbs/a.bottom)-b.top*(gbs/b.bottom);
fm=gbs;
cout<<"sub: "<<fz<<"/"<<fm<<endl;
}
void Fraction::mul(Fraction a, Fraction b){
int fz,fm;
fz=a.top*b.top;
fm=a.bottom*b.bottom;
cout<<"mul: "<<fz<<"/"<<fm<<endl;
}
void Fraction::div(Fraction a, Fraction b){
int fz,fm;
fz=a.top*b.bottom;
fm=b.top*a.bottom;
cout<<"div: "<<fz<<"/"<<fm<<endl;
} void Fraction::compare(Fraction a, Fraction b){
int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
gbs=a.bottom*b.bottom/n;
fz=a.top*(gbs/a.bottom)-b.top*(gbs/b.bottom);
if(fz<)
cout<<"compare: "<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
else if(fz>)
cout<<"compare: "<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
else
cout<<"compare: "<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
}

fraction.cpp

运行结果:

总结:

1、part3中有许多代码是重复的,目前还想不到好的解决方案。

2、在做题时,还是会翻阅书,参考样例,不够熟练。

3、学习了用项目。

评论:

1、https://www.cnblogs.com/shenqidetao/p/10742384.html

2、https://www.cnblogs.com/qsxsc/p/10742704.html

3、https://www.cnblogs.com/csc13813017371/p/10743961.html

c++实验3类和对象的更多相关文章

  1. 【C++ 实验5 类和对象】

    1. #include <iostream> #include <vector> #include <string> using namespace std; // ...

  2. C++ 实验3 类和对象

    Part 2 #ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); pri ...

  3. 【C++/实验三】类和对象

    1.定义一个矩形类,有长,宽两个属性,有成员函数计算矩形的面积. 在该矩形类中,我做了5个主要的测试. 构造函数带默认值参数,利用默认值参数计算矩形面积:rectangle(double x=2.0, ...

  4. 第四周总结和实验二Java简单类与对象

    实验目的 掌握类的定义,熟悉属性.构造函数.方法的使用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实列的方法和属性: 理解static修饰对类. ...

  5. C++ Daily 《6》---- 类静态对象与函数静态对象

    C++ 的一个哲学基础是,你不应该为你使用的东西付出代价. class 拥有一个 static 成员,即使从未被用到,它也会被构造和析构: 而 函数拥有一个 static 成员, 如果这个函数从未被调 ...

  6. iOS RunTime运行时(1):类与对象

    Objective-C语言是一门动态语言,他将很多静态语言在编译和链接期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码更具有灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一下 ...

  7. JAVA入门第二季 第一章 类和对象

    面向对象编程 Object Oriented Programming OOP 第一.什么是类和对象 在具体说明类和对象之前,先说说别的. 眼睛在人类身体上最为有用的器官.如果一个没有了眼睛,这个人与世 ...

  8. php学习小记2 类与对象

    php类的一些特性: 1. 伪变量$this.$this是一个到主叫对象的引用.取值:该方法所从属的对象,可能是另外的对象(前提,当该方法被静态调用时).$this变量存在于一个类的非静态方法中,在静 ...

  9. 非常易于理解‘类'与'对象’ 间 属性 引用关系,暨《Python 中的引用和类属性的初步理解》读后感

    关键字:名称,名称空间,引用,指针,指针类型的指针(即指向指针的指针) 我读完后的理解总结: 1. 我们知道,python中的变量的赋值操作,变量其实就是一个名称name,赋值就是将name引用到一个 ...

随机推荐

  1. VMware,win7与linux centos6.4文件互传,linux下挂载windows共享文件夹,vmware tools安装方法

    本方法是以win7,VMware9.0.1 ,centos6.4为基础实验的. 对于linux的初级使用阶段,都会Windows中使用linux虚拟机VMWare或者其它的.在Windows与linu ...

  2. github desktop项目版本控制

    [git版本控制-笔记]by lijun   0.推荐学习网址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b ...

  3. ACM-ICPC(9/25)

    DP专题 记忆化搜索与递推(方式) DAG模型 记忆化搜索: 用d[状态] 的特殊值表示是否计算过. 用vis[状态]是否访问过 DAG模型: 矩形嵌套:d(i) 以 i 结点开始的最长长度,​ 存在 ...

  4. CF235C 【Cyclical Quest】

    厚颜无耻的发一篇可能是全网最劣解法 我们发现要求给定的串所有不同的循环同构出现的次数,可以直接暴力啊 因为一个长度为\(n\)的串,不同的循环同构次数显然是不会超过\(n\)的,所以我们可以直接对每一 ...

  5. react中PropTypes与DefaultProps的应用

    每个组件都有自己的props参数,这参数是从父组件接收的一些属性,那么如何对参数的类型作校验.如何定义参数的默认值.这里涉及到两个基础的概念,叫做proptypes 和 defaultprops.子组 ...

  6. 应用性能指数(APDEX)是如何计算出来的?

    应用性能指数(APDEX)是如何计算出来的?在应用性能管理领域聚合指标是一种常见手段,主要是用来把成百上千的指标通过某种计算方法聚合成一个或几个指标,用来反映应用的整体健康状态.在这些聚合指标中,比较 ...

  7. RabbitMQ .NET Client 实战实验

    由于公司业务需求,最近想上RabbitMQ,之前我研究了一段时间微软的MSMQ.开源队列有很多,各有优劣.就先拿RabbitMQ练练手吧.本篇着重代码部分,至于怎么安装,怎么配置不在赘述.而且代码是在 ...

  8. Gulp 方法

    Gulp有5个基本方法:src.dest.task.run.watch Gulp.src()      gulp模块的src方法,用于产生数据流.它的参数表示索要处理的文件,一般有以下几种形式: js ...

  9. linux保留旧版本python,安装python3

    1.备份老版本 mv /usr/bin/python /usr/bin/python.bak 2. 下载python3 wget https://www.python.org/ftp/python/3 ...

  10. 小白袍 -- Chapter 1.1 避不开的编解码

    1.1  避不开的编解码 能阅读本文的想开都是从事计算机开发工作的,那么弱弱的问自己一下,有没有受到过编码的纠缠呢?有没有动过心思,如果没有编码该多好? 1.1.1  这个翻译你得捏着鼻子用 要想说明 ...