Part 2

#ifndef GRAPH_H
#define GRAPH_H
class Graph {
public:Graph(char ch, int n);void draw();
private:char symbol;int size;
};#endif

graph.h

#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 = 0;i < size;i++){for (j = 0;j < size - 1 - i;j++)cout << " ";for (k = 0;k < 2 * i + 1;k++)cout << symbol;cout << endl;}
}

graph.app

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

main.app

part 3

#ifndef FRACTION_H
#define FRACTION_Hclass Fraction {
public:Fraction(int t = 0, int b = 1) : top(t), bottom(b) {}Fraction(const Fraction &fr) : top(fr.top), bottom(fr.bottom) {}void fractionadd(Fraction &f, Fraction &p);void fractionmin(Fraction &f, Fraction &p);void fractionmul(Fraction &f, Fraction &p);void fractiondiv(Fraction &f, Fraction &p);void fractioncom(Fraction &f, Fraction &p);void show();
private:int top;int bottom;
};
#endif // !FRACTION_H#pragma once

fraction.h

#include"fraction.h"
#include<iostream>
using namespace std;
void Fraction::show() {if (top == 0) cout << 0 << endl;else if (bottom == 1) cout << top << endl;else if (top / bottom < 0) cout << "-" << top << "/" << bottom << endl;else cout << top << "/" << bottom << endl;
}void Fraction::fractionadd(Fraction &f, Fraction &p) {int t1, b1, t2, b2, m, n, temp, x, y, z;t1 = f.top;t2 = p.top;b1 = f.bottom;b2 = p.bottom;y = b1 * b2;x = t1 * b2 + t2 * b1;m = x;n = y;if (m < n){temp = m;m = n;n = temp;}for (z = n;z >= 1;z--){if (x%z == 0 && y%z == 0) break;}x = x / z;y = y / z;cout << x << "/" << y << endl;
}void Fraction::fractionmin(Fraction &f, Fraction &p) {int t1, t2, b1, b2, x, y, m, n, temp, z;t1 = f.top;t2 = p.top;b1 = f.bottom;b2 = p.bottom;y = b1 * b2;x = t1 * b2 - t2 * b1;m = x;n = y;if (m < n){temp = m;m = n;n = temp;}for (z = n; z >= 1; z--){if (x%z == 0 && y%z == 0) break;}x = x / z;y = y / z;cout << x << "/" << y << endl;
}void Fraction::fractionmul(Fraction &f, Fraction &p) {int t1, t2, b1, b2, x, y, m, n, temp, z;t1 = f.top;t2 = p.top;b1 = f.bottom;b2 = p.bottom;y = b1 * b2;x = t1 * t2;m = x;n = y;if (m < n){temp = m;m = n;n = temp;}for (z = n; z >= 1; z--){if (x%z == 0 && y%z == 0) break;}x = x / z;y = y / z;cout << x << "/" << y << endl;
}void Fraction::fractiondiv(Fraction &f, Fraction &p) {int t1, t2, b1, b2, x, y, m, n, temp, z;t1 = f.top;t2 = p.bottom;b1 = f.bottom;b2 = p.top;y = b1 * b2;x = t1 * t2;m = x;n = y;if (m < n){temp = m;m = n;n = temp;}for (z = n; z >= 1; z--){if (x%z == 0 && y%z == 0) break;}x = x / z;y = y / z;cout << x << "/" << y << endl;
}void Fraction::fractioncom(Fraction &f, Fraction &p) {int t1, t2, b1, b2, x, y;t1 = f.top;t2 = p.top;b1 = f.bottom;b2 = p.bottom;y = b1 * b2;x = t1 * b2 - t2 * b1;if (x < 0) cout << f.top << "/" << f.bottom << "<" << p.top << "/" << p.bottom << endl;else if (x > 0) cout << f.top << "/" << f.bottom << ">" << p.top << "/" << p.bottom << endl;else if (x == 0) cout << f.top << "/" << f.bottom << "=" << p.top << "/" << p.bottom << endl;
}

fraction.app

#include"fraction.h"
#include<iostream>
using namespace std;
int main() {Fraction a;a.show();Fraction b(3, 4);b.show();Fraction c(5);c.show();int x, y;cin >> x >> y;Fraction d(x, y);d.show();a.fractionadd(b, d);a.fractionmin(b, d);a.fractionmul(b, d);a.fractiondiv(b, d);a.fractioncom(b, d);system("pause");
}

mian.cpp

转载于:https://www.cnblogs.com/wyy0204/p/10744290.html

C++ 实验3 类和对象相关推荐

  1. 实验四 类和对象;类的继承和派生;多态性; 接口;构造器应用

    实验四 类和对象:类的继承和派生:多态性: 接口:构造器应用 一.实验目的 1. 掌握类与对象的关系: 2. 掌握类的定义: 3. 掌握对象的声明及使用: 4. 掌握构造方法的概念及调用时机: 5. ...

  2. 面向对象程序设计实验 - 实验2 类和对象:类的构建

    实验二 类和对象--类的构建 目录 实验二 类和对象--类的构建 2.1 实验目的 2.2 实验内容 2.2.1程序阅读 2.2.2 程序设计 2.3思考题 2.1 实验目的 1.类的定义: 2.类对 ...

  3. 淮阴工学院C语言考试题库,淮阴工学院c++实验报告实验九类和对象

    <淮阴工学院c++实验报告实验九类和对象>由会员分享,可在线阅读,更多相关<淮阴工学院c++实验报告实验九类和对象(9页珍藏版)>请在装配图网上搜索. 1.淮阴工学院c+实验报 ...

  4. JAVA类与对象tank_实验四 类与对象

    实验四类与对象 1.实验目的 1.使用类来封装对象的属性和行为: 2.掌握对象的组合以及参数传递: 3.掌握类变量与实例变量,以及类方法与实例方法的区别 2.实验内容 1.参考实验指导书中P17-25 ...

  5. java类与对象实验_JAVA类与对象实验报告

    <JAVA类与对象实验报告>由会员分享,可在线阅读,更多相关<JAVA类与对象实验报告(6页珍藏版)>请在人人文库网上搜索. 1.面向对象程序设计实验报告实验三.类与对象(1) ...

  6. java实验二 类和对象

    类和对象 [实验目的] 1. 掌握如何定义类. 2. 掌握如何定义类的成员变量.成员方法. 3. 掌握如何创建对象.使用对象. 4. 掌握关键字static的用法. 5. 掌握类成员的访问权限. [实 ...

  7. Java(实验三)类与对象-定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性

    一.实验目的: 1.学会定义并实现类. 2.学会定义并创建类的对象,通过类的对象访问类的成员属性与方法. 3.学会定义并实现派生类,学会使用派生类的对象. 4.理解并学会使用类的多态性. 二.实验环境 ...

  8. 【C++实验】类和对象(两个分数相加并且化简)

    类和对象进一步讨论 面向对象程序设计中的几个名词: st1是对象 display()是方法 st1.dispaly()是消息 构造函数:对类的成员进行初始化(为对象分配内存) 带参数/使用默认参数的构 ...

  9. 本科课程【java程序设计】实验2 - 类与对象编程练习

    大家好,我是[1+1=王], 热爱java的计算机(人工智能)渣硕研究生在读. 如果你也对java.人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!! Good better best, never ...

  10. C++程序设计基础实验-实验三 类和对象

    一. 实验目的 掌握类的定义及实例化 掌握类的几种构造函数和析构函数 掌握类的成员访问控制 二.实验内容 设计点类 Point,能够表示平面当中的任意点 (1)数据成员包括两点坐标(x,y),成员函数 ...

最新文章

  1. centos 7 部署k8s集群
  2. Spring Boot之 Configuration Annotation Proessor not found in classpath解决方法
  3. 查找字符串中首个非重复字符
  4. 【转】selector函数指针回调机制
  5. VMware卸载有残留,再安装时报错提示MSI Failed
  6. 线性代数应用于计算机科学例子,为什么计算机科学家们应该了解量子计算?(三):算法棱镜折射出的科学...
  7. mysql存储过程 try_mysql存储过程之异常处理篇
  8. word 产生很多temp 不显示_Word与PPT互转,怎样才能30秒内搞定?教程来了
  9. cad中tk什么意思_cad绘图tk命令技巧
  10. 机器人领域期刊会议汇总
  11. xingtai -斗罗大陆图片下载
  12. 想进大公司先测你EQ
  13. sql bigint 转varchar_SQL 优化案例一则
  14. 方程组在原点附近解matlab,Matlab计算题:求解下列非线性方程组在原点附近的根: 9x^2 + 36y^2 + 4z^2 =36 X^2 -2y^2- 20z =0 16x –...
  15. exe4j将jar包转成exe文件
  16. Eclipse - Code Templates
  17. EasyRecovery 简体中文版
  18. 根据PyTorch学习CONV1D
  19. 【图像加密】基于matlab GUI正交拉丁方置乱+混沌图像加密解密【含Matlab源码 636期】
  20. python 判断当前日期是否为股票交易日

热门文章

  1. QuickBI助你成为分析师-仪表板钻取的实现
  2. dom文档对象模型图
  3. [ZigBee] 10、ZigBee之睡眠定时器
  4. 网上答题及其自动评测系统
  5. Python入门篇之字符串使用
  6. javascript:Location对象的使用简介
  7. Python网络编程(Socket)
  8. SQLite学习手册(索引和数据分析/清理)-转
  9. Linux串口编程-转
  10. select模型使用例子