多文件结构 帮忙改成多文件结构 本人刚刚接触这类问题 请帮忙

xjtrab 2009-03-30 11:10:35
#include <iostream.h>

class rectangle{
private:
int x1,x2,y1,y2;
public:

void setPointP(int x,int y);

void setPointQ(int x,int y);

int calculate();

};

void rectangle::setPointP(int x,int y) {
x1 = x;
y1 = y;
}

void rectangle::setPointQ(int x,int y) {
x2 = x;
y2 = y;
}

int rectangle::calculate(){
int length;
int wide;
length = x1-x2;
if(length<0) length=-length;
wide = y1-y2;
if(wide<0) wide=-wide;
return (wide*length);
}


void main() {
int p1,p2,q1,q2,result;
cout<<"请分别输入两个点的x,y的值:";
cin>>p1>>p2>>q1>>q2;
rectangle t;
t.setPointP(p1,p2);
t.setPointQ(q1,q2);
result=t.calculate();
cout<<endl<<"矩形面积为:";
cout<<result;
}
帮忙改成多文件结构
...全文
91 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgccaa 2009-03-31
  • 打赏
  • 举报
回复

//rectangle.h
#ifndef __RECTANGLE_H_H
#define __RECTANGLE_H_H
class rectangle{
private:
int x1,x2,y1,y2;
public:

void setPointP(int x,int y);

void setPointQ(int x,int y);

int calculate();

};
#endif __RECTANGLE_H_H






//rectangle.cpp
#include "rectangle.h"

void rectangle::setPointP(int x,int y) {
x1 = x;
y1 = y;
}

void rectangle::setPointQ(int x,int y) {
x2 = x;
y2 = y;
}

int rectangle::calculate(){
int length;
int wide;
length = x1-x2;
if(length <0) length=-length;
wide = y1-y2;
if(wide <0) wide=-wide;
return (wide*length);
}





//main.cpp
#include <iostream.h>
#include "rectangle.h"

void main() {
int p1,p2,q1,q2,result;
cout < <"请分别输入两个点的x,y的值:";
cin>>p1>>p2>>q1>>q2;
rectangle t;
t.setPointP(p1,p2);
t.setPointQ(q1,q2);
result=t.calculate();
cout < <endl < <"矩形面积为:";
cout < <result;
}

pengzhixi 2009-03-31
  • 打赏
  • 举报
回复
class rectangle的声明放到Rectangle.h文件。
class rectangle里面函数定义放到Rectangle.cpp中开头用#include"Rectangle.h"
main函数放到main.cpp中开头#include"Rectangle.h"
楼主是想这样么?
lgccaa 2009-03-31
  • 打赏
  • 举报
回复
楼主什么意思,是想把程序写成有头文件,源文件的格式吗?
xjtrab 2009-03-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lgccaa 的回复:]
C/C++ code
//rectangle.h
#ifndef __RECTANGLE_H_H
#define __RECTANGLE_H_H
class rectangle{
private:
int x1,x2,y1,y2;
public:

void setPointP(int x,int y);

void setPointQ(int x,int y);

int calculate();

};
#endif __RECTANGLE_H_H







C/C++ code
//rectangle.cpp
#include "rectangle.h"

void rectangle::setPointP(int x,int y) {
x1 = x;
y1 = y;
}

void r…
[/Quote]


谢谢 可是通过 这样一拆分 放在同一个文件夹理

原来可以运行的程序不能运行了

不是知道是我的程序自身原因还是什么
品茶 2009-03-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lgccaa 的回复:]
C/C++ code
//rectangle.h
#ifndef __RECTANGLE_H_H
#define __RECTANGLE_H_H
class rectangle{
private:
int x1,x2,y1,y2;
public:

void setPointP(int x,int y);

void setPointQ(int x,int y);

int calculate();

};
#endif __RECTANGLE_H_H







C/C++ code
//rectangle.cpp
#include "rectangle.h"

void rectangle::setPointP(int x,int y) {
x1 = x;
y1 = y;
}

void r…
[/Quote]
应该就是这个意思了。
我本来以为是MFC的多文档体系结构的问题。
s79947171 2009-03-31
  • 打赏
  • 举报
回复
UP
Jarrys 2009-03-30
  • 打赏
  • 举报
回复
up

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧