c++实验3类和对象
实 验 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类和对象的更多相关文章
- 【C++ 实验5 类和对象】
1. #include <iostream> #include <vector> #include <string> using namespace std; // ...
- C++ 实验3 类和对象
Part 2 #ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); pri ...
- 【C++/实验三】类和对象
1.定义一个矩形类,有长,宽两个属性,有成员函数计算矩形的面积. 在该矩形类中,我做了5个主要的测试. 构造函数带默认值参数,利用默认值参数计算矩形面积:rectangle(double x=2.0, ...
- 第四周总结和实验二Java简单类与对象
实验目的 掌握类的定义,熟悉属性.构造函数.方法的使用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实列的方法和属性: 理解static修饰对类. ...
- C++ Daily 《6》---- 类静态对象与函数静态对象
C++ 的一个哲学基础是,你不应该为你使用的东西付出代价. class 拥有一个 static 成员,即使从未被用到,它也会被构造和析构: 而 函数拥有一个 static 成员, 如果这个函数从未被调 ...
- iOS RunTime运行时(1):类与对象
Objective-C语言是一门动态语言,他将很多静态语言在编译和链接期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码更具有灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一下 ...
- JAVA入门第二季 第一章 类和对象
面向对象编程 Object Oriented Programming OOP 第一.什么是类和对象 在具体说明类和对象之前,先说说别的. 眼睛在人类身体上最为有用的器官.如果一个没有了眼睛,这个人与世 ...
- php学习小记2 类与对象
php类的一些特性: 1. 伪变量$this.$this是一个到主叫对象的引用.取值:该方法所从属的对象,可能是另外的对象(前提,当该方法被静态调用时).$this变量存在于一个类的非静态方法中,在静 ...
- 非常易于理解‘类'与'对象’ 间 属性 引用关系,暨《Python 中的引用和类属性的初步理解》读后感
关键字:名称,名称空间,引用,指针,指针类型的指针(即指向指针的指针) 我读完后的理解总结: 1. 我们知道,python中的变量的赋值操作,变量其实就是一个名称name,赋值就是将name引用到一个 ...
随机推荐
- sparkStreamming原理
一.Spark Streamming 是基于spark流式处理引擎,基本原理是将实时输入的数据以时间片(秒级)为单位进行拆分,然后经过spark引擎以类似批处理的方式处理每个时间片数据. 二.Spar ...
- 2018.11.15 Nginx服务器的使用
Nginx简单教程 1.什么是Nginx? Nginx(engine x)是一款轻量级的Web服务器.反向代理服务器及电子邮件(IMAP/POP3)代理服务器 什么是反向代理服务器? 反向代理方式是指 ...
- 去掉谷歌浏览器 video标签下的下载按钮
一.判断浏览器版本(区分谷歌和360浏览器) function myBrowser(){ var userAgent = navigator.userAgent; //取得浏览器的userAgent字 ...
- 【luogu P1262 间谍网络】 题解
题目链接:https://www.luogu.org/problemnew/show/P1262 注意: 1.缩点时计算出入度是在缩完点的图上用color计算.不要在原来的点上计算. 2.枚举出入度时 ...
- PHP IDE PHPStorm配置支持友好Laravel代码提示方法
PHPStorm神器可以支持更友好的laravel框架代码提示(点击查看),只需要执行如下才做:第一步:在项目的composer.json中添加如下一行 代码如下: "require&quo ...
- img的空白内容如何处理
给img加一个 vertical-align: bottom;
- Eslint代码规范
- Interview Question Overload、Refactoring和Override?
Overload Overload我们百度翻译知道是超载的意思,不过我们一般称其为重载,在这里我们不纠结于它的翻译,我们来讲讲重载是什么意思,重载的好处.在下面我们以Overload来代表重载(为了记 ...
- 使用 Cordova(PhoneGap)构建Android程序
移动终端开发随着智能手机的普及变得越来越热,什么项目都想移动一把,但反观要去学这些各个终端的控件使用,实属不易,又特别是每个终端的控件及所用语言都各不相同,使得这种学习变得更加困难. 有没有一种简单的 ...
- IE浏览器关于ajax的缓存机制
IE浏览器对于同一个URL只返回相同结果.因为,在默认情况下,IE会缓存ajax的请求结果.对于同一个URL地址,在缓存过期之前,只有第一次请求会真正发送到服务端.大多数情况下,我们使用ajax是希望 ...