实验三 类和对象
一、实验内容
1、graph类内容补充
代码如下:
#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
// 类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() {int i, j, k;for (i = 1;i <= size;i++) {for (j = 1;j <= size - i;j++)cout << ' ';for (k = 1;k <= 2 * i - 1;k++)cout << symbol;cout << endl;} }
graph.cpp
#include <iostream> #include "graph.h" using namespace std;int main() {Graph graph1('*',5);graph1.draw();system("pause");system("cls");Graph graph2('$',7);graph2.draw();return 0; }
main.cpp
效果如下:
2、分数类Fraction加减乘除及比较
#ifndef FRA_H #define FRA_Hclass Fraction { public:Fraction(int t0 = 0, int b0 = 1);friend Fraction add(Fraction &f1, Fraction &f2);//加法计算friend Fraction sub(Fraction &f1, Fraction &f2);//减法计算friend Fraction mul(Fraction &f1, Fraction &f2);//乘法计算friend Fraction div(Fraction &f1, Fraction &f2); //除法计算friend void com(Fraction &f1, Fraction &f2); //比较大小void sim( ); //化简void transform(); //转化void input(); //输入void output(); //输出 private:int top;int bottom; }; #endif
Fraction.h
#include"Fraction.h" #include<iostream> #include<iomanip> #include<math.h> using std::cout; using std::cin; using std::endl;Fraction::Fraction(int t0, int b0):top(t0),bottom(b0){if (b0 = 0) {cout << "worrong fraction!";exit(0);} }Fraction add(Fraction &f1, Fraction &f2) {Fraction ad;ad.bottom = f1.bottom*f2.bottom;ad.top = f1.top*f2.bottom + f2.top*f1.bottom;ad.sim();return ad; }//加法 Fraction sub(Fraction &f1, Fraction &f2) {Fraction su;su.bottom = f1.bottom*f2.bottom;su.top = f1.top*f2.bottom - f2.top*f1.bottom;su.sim();return su; }//减法 Fraction mul(Fraction &f1, Fraction &f2) {Fraction mu;mu.bottom = f1.bottom*f2.bottom;mu.top = f1.top*f2.top;mu.sim();return mu; }//乘法 Fraction div(Fraction &f1, Fraction &f2) {Fraction di;di.bottom = f1.bottom*f2.top;di.top = f1.top*f2.bottom;di.sim();return di; }//除法void com(Fraction &f1, Fraction &f2) {f1.sim();f2.sim();if (f1.top*f2.bottom > f1.bottom*f2.top)cout << f1.top << "/" << f1.bottom << " > " << f2.top << "/" << f2.bottom << endl;else if (f1.top*f2.bottom < f1.bottom*f2.top)cout << f1.top << "/" << f1.bottom << " < " << f2.top << "/" << f2.bottom << endl;else cout << f1.top << "/" << f1.bottom << " = " << f2.top << "/" << f2.bottom << endl; }//比较void Fraction::sim() {int i;if (top != 0) {for (i = fabs(top);i >= 1;i--) {if (top%i == 0 && bottom%i == 0)break;}top /= i;bottom /= i;}if (bottom*top < 0) {top = -fabs(top);bottom = fabs(bottom);}else if (bottom*top > 0) {top = fabs(top);bottom = fabs(bottom);} }//化简void Fraction::transform() {double p;p = top*1.0 / bottom;cout << std::setprecision(3) << p << endl; }//转化void Fraction::input() {cin >> top >> bottom; }//输入void Fraction::output() {if (top != 0) {if (bottom != 1)cout << top << "/" << bottom;else if (bottom == 1)cout << top;}else cout << "0"; }//输出
Fraction.cpp
#include<iostream> #include"Fraction.h" using std::cout; using std::endl;int main() {Fraction a,b;Fraction c1, c2,c3,c4;cout << "Enter two Fraction:";a.input();b.input();cout << "a= "; //输出a的分数和小数形式 a.output();cout << "= ";a.transform();cout << endl;cout << "b= "; //输出b的分数和小数形式 b.output();cout << "= ";b.transform();cout << endl;c1=add(a, b); //a,b相加cout << "a+b= ";c1.output();cout << endl;c2=sub(a, b); //a,b相减cout << "a-b= ";c2.output();cout << endl;c3=mul(a, b); //a,b相乘cout << "a*b= ";c3.output();cout << endl;c4=div(a, b); //a,b相除cout << "a/b= ";c4.output();cout << endl;com(a, b); //比较a,b大小 system("pause");return 0; }
main.cpp
效果如下:
二、实验反思
- graph实验需要将图形转化为代数,发现行列间的规律。
- Fraction.h中需要考虑是否将函数设为友元函数,是否需要带参数,前前后后修改了很多次。
- Fraction.cpp中多处需要分类讨论,后来修改的时间用的比一开始的框架时间长,考虑要周全。
- 最后提出疑问,友元函数不能在主函数中引用吗?还是我使用的格式不对?
转载于:https://www.cnblogs.com/zuiyankh/p/10737504.html
实验三 类和对象相关推荐
- Java(实验三)类与对象-定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性
一.实验目的: 1.学会定义并实现类. 2.学会定义并创建类的对象,通过类的对象访问类的成员属性与方法. 3.学会定义并实现派生类,学会使用派生类的对象. 4.理解并学会使用类的多态性. 二.实验环境 ...
- C++程序设计基础实验-实验三 类和对象
一. 实验目的 掌握类的定义及实例化 掌握类的几种构造函数和析构函数 掌握类的成员访问控制 二.实验内容 设计点类 Point,能够表示平面当中的任意点 (1)数据成员包括两点坐标(x,y),成员函数 ...
- 实验三 类与对象(zxt)
//以下为课上的实现虚数相加的内容,以及我的疑惑(懵逼) 这个代码存在问题,只能运行整数不能运行浮点数,以下为2.0版本 这回的又有一些问题,这个源代码是老师ppt上的,main函数中的部分是我写的. ...
- 实验三 类的继承和多态性
实验三 类的继承和多态性 1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积.int getCircumference():获得图形的周长 ( ...
- 实验四 类和对象;类的继承和派生;多态性; 接口;构造器应用
实验四 类和对象:类的继承和派生:多态性: 接口:构造器应用 一.实验目的 1. 掌握类与对象的关系: 2. 掌握类的定义: 3. 掌握对象的声明及使用: 4. 掌握构造方法的概念及调用时机: 5. ...
- 面向对象程序设计实验 - 实验2 类和对象:类的构建
实验二 类和对象--类的构建 目录 实验二 类和对象--类的构建 2.1 实验目的 2.2 实验内容 2.2.1程序阅读 2.2.2 程序设计 2.3思考题 2.1 实验目的 1.类的定义: 2.类对 ...
- 淮阴工学院C语言考试题库,淮阴工学院c++实验报告实验九类和对象
<淮阴工学院c++实验报告实验九类和对象>由会员分享,可在线阅读,更多相关<淮阴工学院c++实验报告实验九类和对象(9页珍藏版)>请在装配图网上搜索. 1.淮阴工学院c+实验报 ...
- JAVA类与对象tank_实验四 类与对象
实验四类与对象 1.实验目的 1.使用类来封装对象的属性和行为: 2.掌握对象的组合以及参数传递: 3.掌握类变量与实例变量,以及类方法与实例方法的区别 2.实验内容 1.参考实验指导书中P17-25 ...
- java类与对象实验_JAVA类与对象实验报告
<JAVA类与对象实验报告>由会员分享,可在线阅读,更多相关<JAVA类与对象实验报告(6页珍藏版)>请在人人文库网上搜索. 1.面向对象程序设计实验报告实验三.类与对象(1) ...
最新文章
- shell正则表达二
- 导航菜单(移动出现子菜单)
- 汇总内表数据:at end of方法和collect方法
- mysql 5.6.30 添加用户_mysql5.6创建账户不能本地登录
- Delphi实现类似Android锁屏的密码锁控件
- 前端学习(2398):回顾
- 基于linux环境采用update-alternatives 方式进行python版本切换
- 话里话外: 信息化与高层参与度的关系
- NumPy基本操作快速熟悉
- Java 设计模式六大原则
- 使用注解匹配Spring Aop切点表达式
- 论文笔记_SLAM_Simultaneous Localization And Mapping: A Survey of Current Trends in Autonomous Driving
- [项目管理]-第十章:配置管理
- 实现winfrom进度条及进度信息提示,winfrom程序假死处理
- 系统监控+流量监控+抓包分析
- 利用丁香园数据生成疫情分布地图(R语言)
- 抖音下载量超 Facebook;华为新款手机陷“绿屏”门;苹果又遭起诉 | 极客头条...
- Graham扫描法求解二维凸包问题
- File类,字节字符输入输出流,缓冲流,标准流,对象序列化流
- vue前端自动生成编号或者订单单号(日期+随机数)