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 ...
随机推荐
- Windows Server 2008 服务器重启后卡死在Windows Update 页面问题处理
Windows Update 服务器 服务器是联想RD640 操作系统Windows Server 2008 R2 Enterprise版 补丁版本是SP1 远程windows服务器时,一直处于远程建 ...
- day 41 css固定位置 以及小米商城项目
.如何让一个绝对定位的盒子居中 left:%; margin-left:- 宽度的一半 .固定定位 position: fixed; ()脱标 参考点:浏览器的左上角 作用:固定导航栏 返回顶部 小广 ...
- day 35 协程 IO多路复用
0.基于socket发送Http请求 import socket import requests # 方式一 ret = requests.get('https://www.baidu.com/s?w ...
- day 24 组合的补充
一.组合的补充: 1.类或对象可以做字典的key 2.对象中到底有什么? # class Foo(object): # # def __init__(self,age): # self.age = a ...
- spark graphX作图计算
一.使用graph做好友推荐 import org.apache.spark.graphx.{Edge, Graph, VertexId} import org.apache.spark.rdd.RD ...
- 使用Cap解决.Netcore分布式事务
一.什么是Cap CAP 是一个基于 .NET Standard 的 C# 库,它是一种处理分布式事务的解决方案,同样具有 EventBus 的功能,它具有轻量级.易使用.高性能等特点. 在我们 ...
- CCNA 之 十一 NAT 子网地址转换
NAT 网络地址转换 全称:Network Address Translation 为什么需要NAT? 因为公网IP(IPv4)地址紧缺,内容地址通过NAT转换成一个公有地址去访问公网资源: 如下图展 ...
- 虚拟机中linux系统常用命令解释及vim3种命令模式详解
1.man man 加上一个命令可以打开此命令具体使用方法,方便我们更好的了解新命令的使用(下图为我输入命令“man ls”虚拟机界面) 2.cd 切换目录 cd ..(返回上一级目录) cd ~( ...
- Python使用itchat获取微信好友信息~
最近发现了一个好玩的包itchat,通过调用微信网页版的接口实现收发消息,获取好友信息等一些功能,各位可以移步itchat项目介绍查看详细信息. 目标: 获取好友列表 统计性别及城市分布 根据好友签名 ...
- jQuery中detach&&remove&&empty三种方法的区别
jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...