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引用到一个 ...
 
随机推荐
- 网络拓扑_华三H3C的路由器+交换机
			
最近在弄公司网络,目前的拓扑图长这样:点击查看网络拓扑图 华三的路由器和交换机都可以通过Console口进行配置,如下: 用SecureCRT.或者putty.或者windows的超级终端,打开ser ...
 - github同一账户+多个库
			
目标 我的情况是,既要向自己的public库提交代码,又要向别人的private库提交代码 网上搜到的情况一:github上有多个账号,都要向自己的库提交代码 网上搜到的情况二:多个git托管源(比如 ...
 - python基础之作业3----三级菜单小练习
			
data = { "华为技术":{ "产品与解决方案":{ "云核心网":{"云核心网研发管理部","云核心网 ...
 - FreeType使用的总结
			
http://www.cppblog.com/liangairan/archive/2016/09/11/214270.html 这里说一下Freetype的关键点,比较基础的在很多文章已经有说明,这 ...
 - ubuntu 谷歌浏览器打开时需要输入密码来解锁密码环
			
问题: ubuntu14.04, 设置系统自动登陆账户,但每次开机打开 google chromium 浏览器,会要求输入一次密码,来解锁登录密钥环.很麻烦. 解锁登录密钥环:输入密码以解锁您的登录密 ...
 - Android四大组件之Service --- 如何启动和停止Service?
			
启动和停止方法主要是通过Intent来实现 以上一篇中的ServiceTest项目为例来启动和停止MyService这个服务 首先修改activity_main.xml中的代码,如下所示:<Li ...
 - spring Ioc容器之使用XML配置Bean
			
1.项目截图 2.创建xml文件 3.打印机接口 package com.example.demo.computerTest; public interface Printer { void init ...
 - python字符串编码理解(转载)
			
(转载)字符编码和python使用encode,decode转换utf-8, gbk, gb2312 (http://www.cnblogs.com/jxzheng/p/5186490.html) A ...
 - 移动端弹出层加遮罩后禁止body滑动
			
//实现滚动条无法滚动 var mo=function(e){e.preventDefault();}; /***禁止滑动***/ function stop(){ document.body.sty ...
 - Python第八章(北理国家精品课 嵩天等)
			
程序设计方法 8.1体育竞技分析实例 from random import random def printIntro(): print("这个程序模拟两个选手A和B的某种竞技比赛" ...