注册 登录
编程论坛 C++教室

再来一题很简单的,帮忙找哈错!

lzywin 发布于 2009-11-11 17:02, 545 次点击
谢谢 各位!!
//Point类的定义
//Point.h
#ifndef POINT_H
#define POINT_H
#include<iostream.h>
class Point{
public:
    Point(int a=0,int b=0);
    Point(Point&);
    ~Point();
    int get_x()const;
    int get_y()const;
    void put_x(int a);
    void put_y(int b);

    Point operator=(const Point& p1);//重载赋值运算符:当前对象=p1
    Point operator+(const Point& p1);//重载加法运行符:当前对象+p1
    Point operator*(int i);            //重载乘法运算符:当前对象*i
private:
    int x,y;
};
#endif
//Point.cpp
此处是成员函数的实现。
#include “Point.h”
Point operator=(const Point& p1)
{   Point p;
    p.x=p1.x;
    p.y=p1.y;
return p;
}
Point operator+(const Point& p1)
{ Point p;
 p.x=++p1.x;
 p.x=++p1.y;

return p;}
Point operator*(int i)
{  Point p;
p.x=x*p1.x-y*p1.y;
p.y=x*p1.y+y*p1.x;
return p;}
重载函数的测试
//main.cpp
#include <iostream>
Using namespace std;
#include “Point.h”
Int main{
Point c(2,3);
Point p;
Cin>>p;
Cout<<c<<endl;
Return 0;
}




5 回复
#2
flyingcloude2009-11-11 20:13
Int main{
Point c(2,3);
Point p;
Cin>>p;  //应该对读入输出进行重载
Cout<<c<<endl;
Return 0;
}
#3
qlc002009-11-11 21:18
#include “Point.h”
Point operator=(const Point& p1)//这里应该是Point Point::operator=(const Point& p1)
{   Point p;
    p.x=p1.x;
    p.y=p1.y;
return p;
}
Point operator+(const Point& p1)//Point Point::operator+(const Point& p1)
{ Point p;
p.x=++p1.x;
p.x=++p1.y;

return p;}
Point operator*(int i)//Point Point::operator*(int i)
{  Point p;
p.x=x*p1.x-y*p1.y;//这里的p1.y没有定义
p.y=x*p1.y+y*p1.x;
return p;}
重载函数的测试
//main.cpp
#include <iostream>
Using namespace std;//using的u应该小写
#include “Point.h”//引号的输入法错了
Int main{//应该是int
Point c(2,3);
Point p;
Cin>>p;//应该是cin,而且>>和<<应该重载
Cout<<c<<endl;//应该是cout
Return 0;//应该是return
}

#4
一旋无风2009-11-12 16:17
//main.cpp
#include "Point.h"
void main()
{
Point c(2,3);
Point p;
cout<<"请输入P:"<<endl;
cin>>p;
cout<<"c:"<<c;
cout<<"p:"<<p;
p=(p+c);
cout<<"p:"<<p;
}
//point.cpp
#include"Point.h"
Point Point:: operator=(const Point &p1)
{   x=p1.x;y=p1.y;
    return *this;
}
Point Point:: operator+(const Point &p1)
{ Point p;
  p.x=x+p1.x;
  p.y=y+p1.y;
return p;
}
Point Point:: operator*(const Point &p1)
{  Point p;
p.x=x*p1.x-y*p1.y;
p.y=x*p1.y+y*p1.x;
return p;
}
//Point.h
#ifndef POINT_H
#define POINT_H
#include<iostream>
using namespace std;
class Point
{
public:
    Point(int a=0,int b=0){x=a;y=b;}
    Point(const Point&p1){x=p1.x;y=p1.y;}
    ~Point(){}
    int get_x()const{return x;}
    int get_y()const{return y;}
    Point operator=(const Point &p1);//重载赋值运算符:当前对象=p1
    Point operator+(const Point &p1);//重载加法运行符:当前对象+p1
    Point operator*(const Point &p1);//重载乘法运算符:当前对象*i
    friend istream operator >>(istream &in,Point &p1)
    {
    in>>p1.x>>p1.y;
    return in;
    }
    friend ostream operator <<(ostream &out,Point &p1)
    {
        cout<<"x="<<p1.x<<"  y="<<p1.y<<endl;
        return out;
    }
private:
    int x,y;
};
#endif
附:楼主不是我要说你,你真的很粗心,编程大小写不分,而且很多地方犯一些低级错误,改你的程序还真要细心点,费时间
#5
chengUFO2009-11-12 21:26
4楼说的很有道理。。编程序真的得细心。。。经验之谈。。。
#6
lzywin2009-11-13 22:35
//Point类的定义
//Point.h
#ifndef POINT_H
#define POINT_H
#include<iostream.h>
class Point{
public:
    Point(int a=0,int b=0);
    Point(Point&);
    ~Point();
    int get_x()const;
    int get_y()const;
    void put_x(int a);
    void put_y(int b);

    Point operator=(const Point& p1);//重载赋值运算符:当前对象=p1
    Point operator+(const Point& p1);//重载加法运行符:当前对象+p1
    Point operator*(int i);            //重载乘法运算符:当前对象*i
private:
    int x,y;
};
#endif
只是写成员函数的实现及测试这三个成员函数的主函数
1