标签:成员 pac 编写 poi double out private 之间 ble
编写 C++程序完成以下功能:
1)定义一个 Point 类,其属性包括点的坐标,提供计算两点之间距离的方法;
2)定义一个 圆形 类,其属性包括圆心和半径;
3)创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。
Point.h
#pragma once
class Point
{
public:
void setXY(int _x, int _y);
int getX();
int getY();
double getD(Point &p1);
private:
int x;
int y;
};
Point.cpp
#include "Point.h"
#include <math.h>
void Point::setXY(int _x, int _y)
{
x = _x;
y = _y;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
double Point::getD(Point &p1)
{
int dd;
dd = (p1.getX() - x) * (p1.getX() - x) + (p1.getY() - y) * (p1.getY() - y);
return sqrt(dd);
}
Circle.h
#pragma once
// (2)定义一个 圆形 类,其属性包括圆心和半径;
class Circle
{
public:
void setXYR(int _x, int _y, int _r);
int getX();
int getY();
int getR();
private:
int x;
int y;
int r;
};
Circle.cpp
#include "Circle.h"
void Circle::setXYR(int _x, int _y, int _r)
{
x = _x;
y = _y;
r = _r;
}
int Circle::getX()
{
return x;
}
int Circle::getY()
{
return y;
}
int Circle::getR()
{
return r;
}
test.cpp
#include "iostream"
using namespace std;
#include "Point.h"
#include "Circle.h"
void main()
{
// (3)创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。
Circle c1, c2;
cout << "请输入圆A的圆心坐标和半径" << endl;
int x, y, r;
cin >> x >> y >> r;
c1.setXYR(x, y, r);
cout << "请输入圆B的圆心坐标和半径" << endl;
cin >> x >> y >> r;
c2.setXYR(x, y, r);
Point p1, p2;
p1.setXY(c1.getX(), c1.getY());
p2.setXY(c2.getX(), c2.getY());
if (c1.getR() > c2.getR() ? (c1.getR() - c2.getR()):(c2.getR() - c1.getR()) > p1.getD(p2) > (c1.getR() + c2.getR()))
{
cout << "圆A与圆B不相交" << endl;
}
else
{
cout << "圆A与圆B相交" << endl;
}
system("pause");
}
设计并测试一个名为 Rectangle 的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算出矩形的面积
Rectangle.h
#pragma once
// 设计并测试一个名为 Rectangle 的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算出矩形的面积
class Rectangle
{
public:
void setP1P2(int x1, int y1, int x2, int y2);
int getS();
private:
int l_lX;
int l_lY;
int u_rX;
int u_rY;
};
Rectangle.cpp
#include "Rectangle.h"
void Rectangle::setP1P2(int x1, int y1, int x2, int y2)
{
l_lX = x1;
l_lY = y1;
u_rX = x2;
u_rY = y2;
}
int Rectangle::getS()
{
return (u_rX - l_lX) * (u_rY - l_lY);;
}
test.cpp
void main()
{
Rectangle r1;
r1.setP1P2(1, 2, 3, 5);
cout << "矩形面积为:"<< r1.getS() << endl;
system("pause");
}
定义一个 Tree 类,有成员 ages(树龄),成员函数 grow(int years)对 ages 加上 years, age()显示 tree 对象的 ages 的值
class Tree
{
public:
void grow(int years)
{
ages = years;
}
int age()
{
return ages;
}
private:
int ages;
};
void main()
{
Tree tree;
tree.grow(100);
cout << tree.age() << endl;
system("pause");
}
标签:成员 pac 编写 poi double out private 之间 ble
原文地址:https://www.cnblogs.com/2dx3906/p/13149867.html