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

graph.app

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

main.app

part 3

#ifndef FRACTION_H
#define FRACTION_H class Fraction {
public:
Fraction(int t = , int b = ) : 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 == ) cout << << endl;
else if (bottom == ) cout << top << endl;
else if (top / bottom < ) 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 >= ;z--)
{
if (x%z == && y%z == ) 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 >= ; z--)
{
if (x%z == && y%z == ) 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 >= ; z--)
{
if (x%z == && y%z == ) 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 >= ; z--)
{
if (x%z == && y%z == ) 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 < ) cout << f.top << "/" << f.bottom << "<" << p.top << "/" << p.bottom << endl;
else if (x > ) cout << f.top << "/" << f.bottom << ">" << p.top << "/" << p.bottom << endl;
else if (x == ) 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(, );
b.show();
Fraction c();
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

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

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

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

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

     实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...

  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. 嵌入式单片机,ATmega328P,外部中断INT0,INT1,INT2,中断标志位介绍

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. python enumerate用法总结

    enumerate()说明enumerate()是python的内置函数enumerate在字典上是枚举.列举的意思对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumera ...

  3. SQL优化清单

    SQL优化清单 1.from 语句中包含多个表的情况下,把记录数少的表放在前面 2.where 语句中包含多个条件时,将刷选多的条件放前面 3.避免使用select * ,因为这样会去查询所有列的数据 ...

  4. CentOS7.6配置do.cker和K.B.S

     方法一: 节点及功能 主机名 IP Master.etcd.registry K8s-01 10.8.8.31 Node1 K8s-02 10.8.8.32 Node2 K8s-03 10.8.8. ...

  5. python 列表list相关知识

    List的元素可以是Python的任意数据类型(Boolean,Number,String,List,Dict,Set……) List同样可以使用索引和切片,切片得到的结果也是列表. print(li ...

  6. c++给数组整体赋初值

    1.memset memset是计算机中C/C++语言初始化函数.作用是将某一块内存中的内容全部设置为指定的值, 这个函数通常为新申请的内存做初始化工作. 头文件: #include<cstri ...

  7. html+css+javascript之间的关系与作用

    三者间的关系 一个基本的网站包含很多个网页,一个网页由html, css和javascript组成. html是主体,装载各种dom元素:css用来装饰dom元素:javascript控制dom元素. ...

  8. java 五十条数据分为一组

    public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int ...

  9. MySQL存储过程中实现执行动态SQL语句

    sql语句中的任何部分都可以作为参数. DROP PROCEDURE if exists insertdata; delimiter //CREATE PROCEDURE insertdata(IN ...

  10. 网络协议中HTTP,TCP,UDP,Socket,WebSocket的优缺点/区别

    先说一下网络的层级:由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层 1.TCP和UDP TCP:是面向连接的一种传输控制协议.属于传输层协议.TCP连接之后,客户端和服务器可 ...