实 验 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. python入门12 列表list

    列表使用率较高,方法也多. 列表的定义 #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanhua 列表 " ...

  2. Ubuntu 配置java环境变量

    1.使用如下命令,打开/etc/profile: $sudo vi /etc/profile 2.进入编辑模式,在末尾添加: #developer enviroment, add by myself ...

  3. 【转】netstat 命令当中的内部地址和外部地址分别代表的是什么?

    RT,用自己的话解释,不要搬网上的内容.尽量可以简洁,谢谢 2015-03-07 17:41 提问者采纳   网络连接是有2方参与,自己就是“内部地址”,别人就是“外部地址”,就这么简洁. 再举个例子 ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】

    任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...

  5. 【转】iOS保持界面流畅的技巧

    原文链接:iOS保持界面流畅的技巧 这篇文章会非常详细的分析 iOS 界面构建中的各种性能问题以及对应的解决思路,同时给出一个开源的微博列表实现,通过实际的代码展示如何构建流畅的交互. Index演示 ...

  6. LOJ 2172 「FJOI2016」所有公共子序列问题——序列自动机

    题目:https://loj.ac/problem/2172 在两个序列自动机上同时走,这样暴搜. 先走字典序小的字符,一边搜一边输出,就是按字典序排序的. 方案数很多,需要高精度?空间很小,要压位. ...

  7. 机器学习基础(HGL的机器学习笔记1)

    统计学习:统计学习是关于计算机基于数据构建概率统计模型并运用模型对数据进行预测与分析的一门学科,统计学习也成为统计机器人学习[1]. 统计学习分类:有监督学习与无监督学习[2]. 统计学习三要素:模型 ...

  8. 【luogu P1993 小K的农场】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1993 1.差分约束: 对于a - b <= c 有一条 b-->a 权值为c 对于a - b & ...

  9. centos安装nodejs二进制包

    1.下载nodejs的二进制包 wget https://nodejs.org/dist/v6.3.1/node-v6.3.1-linux-x64.tar.xz 2.解压下载的安装包 tar xf n ...

  10. linux上部署redis实现与Python上的redis交互(有坑)

    1.概念 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件 2.linux安装redis 2.1yum源安装redis(不推荐) #前提得配置好阿里 ...