C++ 实验3 类和对象
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 类和对象的更多相关文章
- 【C++ 实验5 类和对象】
1. #include <iostream> #include <vector> #include <string> using namespace std; // ...
- c++实验3类和对象
实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...
- 【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引用到一个 ...
随机推荐
- thinkphp5和thinkphp3.2.3中URL重写出现No input file specified
查询后解决办法是打开public目录下的.htaccess文件,把:RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 改为:RewriteRule ^(.*)$ i ...
- 一条命令关掉centos所有不必要的服务和端口号
centos作为服务器开放的服务多了,难免一些服务软件有漏洞,开放的端口号越多,上线的服务器越危险,所以我们必须在服务器上线之前把centos里面不必要的服务全部干掉,不让坏人有可乘之机. 首先看一下 ...
- 最短路径HDU3790(Dijkstra)
准备考研,荒废了好多东西,希望做了正确的决定 /********************************************************* *author:chen xin * ...
- Frist one
2017.11.27 10:20am 今天也许只是普通的一个周一上午 但是我希望在多年以后 在我回望这些年的努力学习中 今天会是最浓墨重彩的一笔 我时常感到焦虑 对未来的焦虑 对感情的焦虑 以至于 我 ...
- generator mybatis逆向工程
mybatis逆向工程:根据数据库中的表在项目中生成对应的 实体类,dao接口与mapper.xml映射文件 在eclipse中,以插件的形式的存在,把设置好的配置文件,直接通过eclipse中的插件 ...
- java this关键字的使用
this关键字 this关键字只能在方法内部使用,表示对"调用方法的那个对象"的引用. this的三个用法: 1.调用本类中的其他方法 如果在方法 ...
- Hyperledger Fabric-CA学习
Hyperleder Fabric系统架构核心逻辑包括MemberShip.Blockchain和Chaincode 其中上述3个核心逻辑中,Membership服务用来管理节点身份.隐私.confi ...
- Python机器学习(基础篇---监督学习(集成模型))
集成模型 集成分类模型是综合考量多个分类器的预测结果,从而做出决策. 综合考量的方式大体分为两种: 1.利用相同的训练数据同时搭建多个独立的分类模型,然后通过投票的方式,以少数服从多数的原则作出最终的 ...
- LINUX 学习笔记 账号与群组的管理
LINUX 账号与群组的管理 UID:UserID 保存文件:/etc/passwd GID:GroupID 保存文件:/etc/group /etc/passwd 文件结构 一行代表一个账号,里面还 ...
- python网络编程(UDP+广播)
UDP广播案例,一端发送,多端接受: 发送端: # UDP广播案例 from socket import * from time import sleep # 设定目标地址 dest=('176.21 ...