@[TOC]模块化程序
**
点和圆的关系
**
在黑马程序员课程4.1.3成员属性设置为私有 课后案例 点和圆的关系中 谈到了文件的封装
此案例是判断点与圆的关系,重点是以另外一个类作为本类中的成员;
在比较大的开发中,会有许多个类,以及许多个函数。因此,以封装的思想,来模块化程序。
#include<iostream>
using namespace std;
class Point
{
public:
void setX(int x)
{
m_X = x;
}
int getX()
{
return m_X;
}
void setY(int y)
{
this->m_Y = y;
}
int getY()
{
return m_Y;
}
private:
int m_X;
int m_Y;
};
class Circle
{
public:
void setR(int r)
{
m_R = r;
}
int getR()
{
return m_R;
}
void setC(Point center)
{
m_Center = center;
}
Point getC()
{
return m_Center;
}
private:
int m_R;
Point m_Center;
};
void judge( Circle &c, Point &p)
{
int a;
a = (c.getC().getX() - p.getX()) * (c.getC().getX() - p.getX()) + (c.getC().getY() - p.getY()) * (c.getC().getY() - p.getY());
if ( a == (c.getR())* (c.getR())) { cout << "点在圆上" << endl; return; }
if (a > (c.getR()) * (c.getR())) { cout << "点在圆外" << endl; return;}
if (a < (c.getR()) * (c.getR())) { cout << "点在圆内" << endl; return;}
return;
}
void test01()
{
Circle C;
Point P;
Point cc;
int cx, cy,r, px, py;
cout << "请设置圆心横坐标X:" << endl;
cin >> cx;
cout << "请设置圆心纵坐标Y:" << endl;
cin >> cy;
cout << "请设置圆的半径R:" << endl;
cin >> r;
cout << "请设置点的横坐标X:" << endl;
cin >> px;
cout << "请设置点的纵坐标Y:" << endl;
cin >> py;
cc.setX(cx);
cc.setY(cy);
C.setC(cc);
C.setR(r);
P.setX(px);
P.setY(py);
judge(C,P);
}
int main()
{
for (int i = 0; i < 3; i++) { test01(); }
system("pause");
return 0;
}
在程序中有point类和Circle类,
在项目中新创建两个文件point.cpp和point.h,以及circle.cpp和circle.h。简单来说就是把类的成员函数的声明放在头文件里,把类的成员函数的实现放在源文件中。
需要注意的是,在头文件中要有#pragma once 以避免重复包含头文件
主源文件
#include<iostream>
#include"point.h"
#include"circle.h"
using namespace std;
void judge( Circle &c, Point &p)
{
int a;
a = (c.getC().getX() - p.getX()) * (c.getC().getX() - p.getX()) + (c.getC().getY() - p.getY()) * (c.getC().getY() - p.getY());
if ( a == (c.getR())* (c.getR())) { cout << "点在圆上" << endl; return; }
if (a > (c.getR()) * (c.getR())) { cout << "点在圆外" << endl; return;}
if (a < (c.getR()) * (c.getR())) { cout << "点在圆内" << endl; return;}
return;
}
void test01()
{
Circle C;
Point P;
Point cc;
int cx, cy,r, px, py;
cout << "请设置圆心横坐标X:" << endl;
cin >> cx;
cout << "请设置圆心纵坐标Y:" << endl;
cin >> cy;
cout << "请设置圆的半径R:" << endl;
cin >> r;
cout << "请设置点的横坐标X:" << endl;
cin >> px;
cout << "请设置点的纵坐标Y:" << endl;
cin >> py;
cc.setX(cx);
cc.setY(cy);
C.setC(cc);
C.setR(r);
P.setX(px);
P.setY(py);
judge(C,P);
}
int main()
{
for (int i = 0; i < 3; i++) { test01(); }
system("pause");
return 0;
}
point.h
#pragma once
#include<iostream>
using namespace std;
class Point
{
public:
void setX(int x);
int getX();
void setY(int y);
int getY();
private:
int m_X;
int m_Y;
};
point.cpp
#include"point.h"
void Point::setX(int x)
{
m_X = x;
}
int Point::getX()
{
return m_X;
}
void Point::setY(int y)
{
m_Y = y;
}
int Point::getY()
{
return m_Y;
}
circle.h
#pragma once
#include<iostream>
#include"Point.h"
using namespace std;
class Circle
{
public:
void setR(int r);
int getR();
void setC(Point center);
Point getC();
private:
int m_R;
Point m_Center;
};
circle.cpp
#include"circle.h"
void Circle::setR(int r)
{
m_R = r;
}
int Circle::getR()
{
return m_R;
}
void Circle::setC(Point center)
{
m_Center = center;
}
Point Circle::getC()
{
return m_Center;
}
类的成员的声明与实现的分开写。
案例 - 职工管理系统
switch语句里面,default表明默认分支走向,当所有的case条件都不符合时,程序走default分支方向。
现在需要弄明白的就是,如何在堆区创建数组对象。
对于该系统的思路呢,先创建一个父类对象woker,然后创建子类对象employee等,用woker在堆区创建对象数组。woker是一个抽象类,无法实例化对象,那么这个对象数组的类型是?多态的触发,是通过父类的指针或引用接收子类的对象,数组中存放的是相同类型的内容,为了统一三者,用父类woker的指针来接受三者的对象。问题:**可不可以用一个数组来管理父类指针呢?**当然可以,那么该数组应该放在栈区还是堆区呢?如果将数组开辟到栈区,那么该函数执行完,栈区内存释放,对象数组中的数据也就丢失了。所以要以指针的方式,在堆区创建对象数组。要开辟到堆区,手动管理释放。而在堆区开辟的数据会返回相应数据类型的指针,如,返回 boss * manager * employee * ,但三者都继承woker,因此可用woker 来返回子类对象,因此 woker后要跟两个。外面一个是用指针接收堆区返回的数据,里面的指针使用父类指针来接收子类的对象,从而触发多态。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)