【C++类与对象】实验四(二)
- 实现画图类
#ifndef GRAPH_H
#define GRAPH_H // 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
void changecolor();
private:
char symbol;
int size;
char selcolor;
}; #endifgraph.h
// 类graph的实现
#include<cstdio>
#include "graph.h"
#include <iostream>
using namespace std; // 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
} // 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
// size和symbol是类Graph的私有成员数据
void Graph::draw() {
int i,j,k;
for(i=;i<=size;i++){ k=size-i;
while(k--){
cout<<' ';
}
k=;
while(k<*i-){
cout<<symbol;
k++;
}
k=size-i;
while(k--){
cout<<' ';
}
cout<<endl;
}
}graph.cpp
#include <iostream>
#include "graph.h"
#include"graph.cpp" using namespace std; int main() {
Graph graph1('*',), graph2('$',) ; // 定义Graph类对象graph1, graph2
graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形
graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
char a[];
system("color 19");//系统设置颜色
return ;
}mian.cpp
- 运行截图:其中关于修改颜色,我用的函数库里面的函数system(“color背景色/前景色”)

- 实现分数类
#include <iostream>
#include<cmath>
#include"fraction.h"
using namespace std; // 带参数的构造函数的实现
Fraction::Fraction(int t, int b): top(t), bottom(b) {
}
Fraction::Fraction(): top(), bottom() {
}
Fraction::Fraction(int t): top(t), bottom() {
}
Fraction::Fraction(Fraction &f1): top(f1.top), bottom(f1.bottom) {
}
void Fraction::shuru(){
cout<<"请输入分子,分母:";
int a,b;
cin>>a>>b; top=a;
bottom=b;
}
void Fraction::jia(Fraction &f1){
if(bottom==f1.bottom){
top+=f1.top;
}
else{
top=top*f1.bottom+bottom*f1.top;
bottom=bottom*f1.bottom;
}
}
void Fraction::jian(Fraction &f1){
if(bottom==f1.bottom){
top-=f1.top;
}
else{
top=top*f1.bottom-bottom*f1.top;
bottom=bottom*f1.bottom;
}
}
void Fraction::cheng(Fraction &f1){
bottom*=f1.bottom;
top*=f1.top;
}
void Fraction::chu(Fraction &f1){
bottom*=f1.top;
top*=f1.bottom;
}
void Fraction::printfraction(){
int a,b,r;
a=top;
b=bottom;
r=a%b;
while(r){
a=b;
b=r;
r=a%b;
}
top=top/b;
bottom=bottom/b;
if(top<&&bottom>||top>&&bottom<)
cout<<"-"<<abs(top)<<"/"<<abs(bottom)<<endl;
if(top>=&&bottom>||top<=&&bottom<)
cout<<abs(top)<<"/"<<abs(bottom)<<endl; }
void Fraction::printinteger(){
double k;
k=double(top)/(double(bottom));
cout<<k<<endl;
} #
fraction.cpp
#ifndef FRACTION_H
#define FRACTION_H // 类Graph的声明
class Fraction {
public:
Fraction(int t,int b);//构造函数
Fraction();
Fraction(int t);
Fraction(Fraction &f1);
void shuru();//输入
void jia(Fraction &f1);//加法
void jian(Fraction &f1);// 减法
void cheng(Fraction &f1);//乘法
void chu(Fraction &f1);//除法
void printfraction();//输出分数
void printinteger(); //输出整数 private:
int top;
int bottom;
}; #endif
fraction.h
#include <iostream>
#include"fraction.h" using namespace std; int main()
{
Fraction a,b(,),c(),d,f;//初始化任意两个对象
a.printfraction();
b.printfraction();
c.printfraction();
//d(2,5); 为啥在定义一个普通变量,比如int a; a=1;但是定义类,fraction a; a(,) 是错的,因为a有默认的参数值。 a.shuru();
b.shuru();
cout<<"分数相加:";
a.jia(b);
a.printfraction();
cout<<"分数相减:";
a.jian(b);
a.printfraction();
cout<<"分数相乘:";
a.cheng(b);
a.printfraction();
cout<<"分数相除:";
a.chu(b);
a.printfraction();
a.printinteger(); return ;
}
mian.cpp
3.体会:
int a;
a=2;//对的
(class)Fraction a;
a(2,1);//出错

我认为原因是,1.a有默认构造函数,在定义时,就默认初始化a的值。2.a(,)在类中并没有定义这个函数。
【C++类与对象】实验四(二)的更多相关文章
- JAVA类与对象(四)----成员变量与局部变量 、成员方法、构造方法
类体中的变量分为两部分.变量定义部分定义的变量为类的成员变量,在方法体中定义的变量和方法中涉及的变量称为局部变量. 成员变量和局部变量的区别: (1).成员变量在整个类中都有效,局部变量只在定义它的方 ...
- JavaSE-反射-获取类或者对象的四种方法
1.使用Class类的静态方法Class.forName("xxxx"); 新建一个要想要获取的类 package org.burning.sport.javase.classlo ...
- 第四周总结和实验二Java简单类与对象
实验目的 掌握类的定义,熟悉属性.构造函数.方法的使用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实列的方法和属性: 理解static修饰对类. ...
- oc语言学习之基础知识点介绍(二):类和对象的进一步介绍
一.类.对象在内存中的存储 /* 内存分区: 栈:局部变量 堆:程序员自己写代码申请开辟的 程序员自己维护,编译器现在帮我们自动优化了,它在合适的给我们加上了释放空间的语句,所以我们现在写的对象不会造 ...
- Programming In Scala笔记-第四章、类和对象
类似于Java,Scala中也有类和对象的概念. 一.类.属性和方法 1.类 类是对一类事物的抽象,当一个类被定义后,就可以以该定义为模板,定义该类的一系列对象.比如说有以下一个模板 人类: 有姓名: ...
- 类与对象 - Java学习(二)
弄清楚类与对象的本质与基本特征,是进一步学习面向对象编程语言的基本要求.面向对象程序设计与面向过程程序设计在思维上存在着很大差别,改变一种思维方式并不是一件容易的事情. 一.面向对象程序设计 程序由对 ...
- 孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习
孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.close() 当一个file对象执行此方法时,将关闭当前 ...
- MOOC C++笔记(二):类和对象基础
第二周:类和对象基础 面向对象程序设计的四个基本特点 抽象.封装.继承.多态. 面向对象程序设计的过程 1.从客观事物抽象出类 抽象出的事物带有成员函数与成员变量(类似于带函数的结构体) 成员变量和成 ...
- Java学习日记基础篇(四)——类,对象之成员变量,成员方法,构造方法
面向对象(Object Oriented) 一.面向对象杂谈 面向对象(Object Oriented),我的翻译是以物体为目标的,就是说编程的时候是建立一个物体,然后对这个物体进行操作. Java语 ...
- C++中的类和对象(二)
一,对象的动态建立和释放 1.什么是对象的动态建立和释放 通常我们创建的对象都是由C++编译器为我们在栈内存中创建的,我们无法对其进行生命周期的管理.所以我们需要动态的去建立该对象,因此我们需要在堆内 ...
随机推荐
- SSH error ( Read from socket failed: Connection reset by peer ) and it's solution
SSH error ( Read from socket failed: Connection reset by peer ) and it's solution ssh cann't connect ...
- C++ 中的不定参数与格式化字符串 # ## vsprintf
日志打印或者格式字符串时,可能会用到不定参数的使用,这里记录一下. 格式化字符串有很多方法: snprintf std::stringstream # ##的使用 ##是一个连接符号,用于把参数连在一 ...
- 发现一个“佛系记账本”
因为这是一款微信小程序,张小龙大力推崇的"用完即走"完美地适合记账应用. 不用下载.不用安装.不用注册.不用各种授权,只要从微信进入,就能记账,账本只与微信关联. 换手机.换PAD ...
- Print all attributes and values in a Javascript Object
function printObject(o) { var out = ''; for (var p in o) { out += '\n' + ':: ' + p + '(' + typeof(o[ ...
- wsl install lamp
sudo apt-get update sudo apt-get install lamp-server^ /etc/init.d/apache2 start /etc/init.d/mysql st ...
- [Python设计模式] 第19章 分公司=部门?——组合模式
github地址:https://github.com/cheesezh/python_design_patterns 组合模式 组合模式,将对象组合成树形结构以表示"部分-整体" ...
- PowerShe 消息提示框测试
1. 使用powerShell 弹出一个简单的消息框,代码如下,创建test.ps1脚本文件. $ConfirmPreference = 'None' $ws = New-Object -ComObj ...
- linux 目录/sys 解析
今天搞树莓派,遇到/sys这个目录,不太清楚,先对/sys目录知识进行一个整理 首先,对 /sys目录下的各个子目录进行具体说明: /sys下的子目录 内容 /sys/devices 该目录下是全局设 ...
- Mosquitto --topic
订阅树的概念 Mosquitto通过订阅树的方式来管理所有的topic以及客户端的订阅关系,它首先将所有的topic按照/分割并组织成一棵树结构,从根节点到树中的每个节点即组成该节点所对 ...
- Atitit 文员招募规范 attilax总结
Atitit 文员招募规范 attilax总结 1. 概念 2 2. 文员招募范文 2 3. 重大意义 3 3.1. 第一层 文章撰写 能力 3 3.2. 第二次 文档管理能力 文档索引 检索查找 ...