c++-面向对象类的示例-求周长面积,判断体积相等-文件操作和一般操作
面向对象编程示例:求周长和面积
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//圆的周长
double getCircleGirth(double r)
{
return 2 * 3.14*r;
}
//源的面积
double getCircleArea(double r)
{
return 3.14*r*r;
}
//用面向对象实现
//圆类
class Circle
{
public:
void setR(double r)
{
m_r = r;
}
double getR()
{
return m_r;
}
double getGirth()
{
return 2 * 3.14 *m_r;
}
double getArea()
{
return m_r*m_r*3.14;
}
private:
double m_r; //圆的私有成员 半径
};
class Circle2
{
public:
void setR(double r)
{
m_r = r;
}
double getR()
{
return m_r;
}
double getArea()
{
m_area = m_r*m_r*3.14;
return m_area;
}
double getGirth()
{
m_girth = m_r * 2 * 3.14;
return m_girth;
}
private:
double m_r;
double m_girth; //周长
double m_area;//面积
};
int main(void)
{
double r = 10; //圆的半径
double g = 0;
double a = 0;
g = getCircleGirth(r);
a = getCircleArea(r);
cout << "圆的半径是" << r << endl;
cout << "圆的周长是" << g << endl;
cout << "圆的面积是" << a << endl;
cout << "------" << endl;
Circle c;
c.setR(10);
cout << "圆的半径是" << c.getR() << endl;
cout << "圆的周长是" << c.getGirth() << endl;
cout << "圆的面积是" << c.getArea() << endl;
cout << "------------" << endl;
Circle2 c2;
c2.setR(10);
cout << "圆的半径是" << c2.getR() << endl;
cout << "圆的周长是" << c2.getGirth() << endl;
cout << "圆的面积是" << c2.getArea() << endl;
return 0;
}
上面那个多文件形式(模块化?(▽))
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Circle.h"
using namespace std;
int main(void)
{
Circle c;
c.setR(10);
cout << "Ãæ»ý" << c.getArea() << endl;
return 0;
}
Circle.h
#pragma once
#if 0
#ifndef __CIRCLE_H_
#define __CIRCLE_H_
#endif
#endif
class Circle
{
public:
//ÉèÖð뾶£º
void setR(double r);
//µÃµ½°ë¾¶
double getR();
double getArea();
double getGirth();
private:
double m_r;
double m_area;
double m_girth;
};
Circle.cpp
#include "Circle.h"
void Circle::setR(double r)
{
m_r = r;
}
double Circle::getR()
{
return m_r;
}
double Circle::getArea()
{
m_area = m_r *m_r *3.14;
return m_area;
}
double Circle::getGirth()
{
m_girth = m_r * 2 * 3.14;
return m_girth;
}
示例:判断立方体相等
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//立方体类
class Cube
{
public:
void setABC(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
int getArea()
{
return (m_a*m_b) * 2 + (m_a*m_c) * 2 + (m_b*m_c) * 2;
}
int getVolume()
{
return (m_a*m_b*m_c);
}
int getA()
{
return m_a;
}
int getB()
{
return m_b;
}
int getC()
{
return m_c;
}
//同类之间无私处
bool judgeCube(Cube &another)
{
if (m_a == another.m_a &&
m_b == another.getB() &&
m_c == another.getC()) {
return true;
}
else {
return false;
}
}
private:
int m_a;
int m_b;
int m_c;
};
//全局函数
bool judgeCube(Cube &c1, Cube &c2)
{
if (c1.getA() == c2.getA() &&
c1.getB() == c2.getB() &&
c1.getC() == c2.getC()) {
return true;
}
else {
return false;
}
}
int main(void)
{
Cube c1;
c1.setABC(10, 20, 30);
Cube c2;
c2.setABC(10, 20, 30);
cout << "c1 的体积是" << c1.getVolume() << endl;
cout << "c1 的面积是" << c1.getArea() << endl;
if (judgeCube(c1, c2) == true) {
cout << "相等" << endl;
}
else {
cout << "不相等" << endl;
}
cout << " ------ " << endl;
if (c1.judgeCube(c2) == true) {
cout << "相等" << endl;
}
else {
cout << "不相等" << endl;
}
return 0;
}
示例:求点是否在圆内
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//点类
class Point
{
public:
void setXY(int x, int y)
{
m_x = x;
m_y = y;
}
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
private:
int m_x;
int m_y;
};
//圆类
class Circle
{
public:
void setXY(int x, int y)
{
x0 = x;
y0 = y;
}
void setR(int r)
{
m_r = r;
}
//提供一个判断点是否在圆内
//true 在内部
//false 在外部
bool judgePoint(Point &p)
{
int dd;
dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
if (dd > m_r*m_r) {
return false;
}
else {
return true;
}
}
private:
int x0;
int y0;
int m_r;
};
int main(void)
{
Circle c;
c.setXY(2, 2);
c.setR(4);
Point p;
p.setXY(8, 8);
if (c.judgePoint(p) == true) {
cout << "圆的内部" << endl;
}
else {
cout << "圆的外部" << endl;
}
return 0;
}
上面代码的文件形式
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Circle.h"
#include "Point.h"
using namespace std;
int main(void)
{
Circle c;
c.setR(4);
c.setXY(2, 2);
Point p;
p.setXY(8, 8);
if (c.judgePoint(p) == true) {
cout << "nei" << endl;
}
else {
cout << "wai" << endl;
}
return 0;
}
Circle.h
#pragma once
#include "Point.h"
class Circle
{
public:
void setXY(int x, int y);
void setR(int r);
//提供一个判断点是否在圆内
//true 在内部
//false 在外部
bool judgePoint(Point &p);
private:
int x0;
int y0;
int m_r;
};
Circle.cpp
#include "Circle.h"
void Circle::setXY(int x, int y)
{
x0 = x;
y0 = y;
}
void Circle::setR(int r)
{
m_r = r;
}
//提供一个判断点是否在圆内
//true 在内部
//false 在外部
bool Circle::judgePoint(Point &p)
{
int dd;
dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
if (dd > m_r*m_r) {
return false;
}
else {
return true;
}
}
Point.h
#pragma once
class Point
{
public:
void setXY(int x, int y);
int getX();
int getY();
private:
int m_x;
int m_y;
};
Point.cpp
#include "Point.h"
void Point::setXY(int x, int y)
{
m_x = x;
m_y = y;
}
int Point::getX()
{
return m_x;
}
int Point::getY()
{
return m_y;
}
c++-面向对象类的示例-求周长面积,判断体积相等-文件操作和一般操作的更多相关文章
- C# 定积分求周长&面积原理 代码实现
前言: 前些日子,因为工作原因,接触到了求解曲线周长,真的是搞了很久,学生时代真的很简单,但是如今的我来说,忘记了....很多人跟我应该一样. 所以来巩固加强一下记忆.一开始的时候,求周长嘛,找公式呗 ...
- 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有
package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...
- 按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有确定位
package com.hanqi.test; public class Rect { ; ; public double getWidth() { return width; } public vo ...
- 编写一个Shape类,具有属性:周长和面积; 定义其子类三角形和矩形,分别具有求周长的方法。 定义主类E,在其main方法中创建三角形和矩形类的对象, 并赋给Shape类的对象a、b,使用对象a、b来测试其特性。
package shape; public class Shape { //定义成员变量 private double zhouchang; private double mianji; public ...
- C#使用多态求方形面积周长和圆的面积周长
class class1 { public static void Main(string[] args) { //使用多态求矩形面积与周长和圆的面积与周长 Shape cl = ); double ...
- java求三角形面积以及周长---封装
/*时间: 2012-10-08作者: 烟大程序要求: 1.封装一类三角形对象Triangle,该类对象具有三条边的属性, 具有初始化三角形的功能.修改边长的功能.判断三条边能否构成三角形的功能. 求 ...
- (1)定义闭合图形抽象类ClosedFigure定义属性:1.形状;2.定义构造方法,给形状赋值;3.定义两个抽象方法:计算面积和计算周长;4.定义一个显示方法:显示图像形状,周长,面积;
题目显示不全,完整题目描述: (1)定义闭合图形抽象类ClosedFigure定义属性:1.形状:2.定义构造方法,给形状赋值:3.定义两个抽象方法:计算面积和计算周长:4.定义一个显示方法:显示图像 ...
- java学习-初级入门-面向对象④-类与对象-类与对象的定义和使用2
我们继续学习类与对象,上一篇我们定义了 坐标类(Point), 这次我们在Point的基础上,创建一个圆类(Circle). 案例:创建一个圆类 题目要求: 计算圆的周长和面积:求判断两个圆的位置关 ...
- Java面向对象~类和对象&方法,类方法
面向对象 概念: 1.同一类事物的抽象描述,不是具体的 2.类和对象的关系: 类 是抽象的. 对象 是具体的. 3.对象的体征,称为"属性&q ...
随机推荐
- 选择了uniapp开发app
7月份打算做一简单app,之前公司做app的时候简单用过Dcloud公司的mui,当时由于uniapp刚出来,最终选择了mui.对uniapp的 了解几乎没有. 做app对我来说几乎是零基础的,当然是 ...
- scikit-learn_cookbook1: 高性能机器学习-NumPy
源码下载 在本章主要内容: NumPy基础知识 加载iris数据集 查看iris数据集 用pandas查看iris数据集 用NumPy和matplotlib绘图 最小机器学习配方 - SVM分类 介绍 ...
- Python的os,shutil和sys模块
*********OS*********** os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'n ...
- android 活动监听是否点击某个view
前述(写给做过web前端的人) 在web H5,如果如果判断当前是否点击某个元素,一般会这样写. window.addEventListener("touchstart",func ...
- day20191109spring
笔记: 1.Idea构建maven项目之web应用项目 src main java文件夹中定义 Java源程序 resources文件中定义 资源配置文件信息 test文件夹中定义 测试Java程序 ...
- centos 6.x 系统基础优化简版
Centos 6.x 系统基础优化 1.更换国内yum源 删除系统带的centos官方yum源 rm -rf /etc/yum.repos.d/* 使用国内阿里云源 curl -o /etc/yum. ...
- PAT乙级练习(1001)
1001 害死人不偿命的(3n+1)猜想 卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n + 1) 砍掉一半.这样一直反复砍下去,最 ...
- Vue&Cesium&Ribbon界面: 将桌面GIS搬进浏览器
上一篇文章在这里:vue集成cesium,webgis平台第一步 把界面改了一下,开始实际填充功能. Ribbon是一种以面板及标签页为架构的用户界面(User Interface),原先出现在Mic ...
- 统计学习方法与Python实现(一)——感知机
统计学习方法与Python实现(一)——感知机 iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 1.定义 假设输入的实例的特征空间为x属于Rn的n维特征向量, ...
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查
使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...