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

函数继承

hzj199603 发布于 2015-05-04 21:28, 1013 次点击
#include<iostream>
using namespace std;
class Dot
{
private:
    float x,y;
public:
    Dot(float a,float b):x(a),y(b){};
    float getsx()
    {
        return (x);
    }
    float getsy()
    {
        return (y);
    }
    void Show(){cout<<"x="<<getsx()<<" "<<"y="<<getsy()<<endl;}
};
class Line:public Dot
{
    Dot d1,d2;
public:   
    float x,y;
    Line(Dot dot1,Dot dot2):d1(dot1),d2(dot2)
    {
    x=(d1.getsx()+d2.getsx())/2;   
    y=(d1.getsy()+d2.getsy())/2;
    }
void Showl()
{
  cout<<"Dot1:";
   d1.Show();
   cout<<"Dot2:";
   d2.Show();
   cout<<"Center:"<<"x="<<x<<" "<<"y="<<y<<endl;
}
};
int main()
{   float a,b;
    cout<<"Input Dot1:"<<endl;
    cin>>a>>b;
    Dot dot1(a,b);
    cout<<"Input Dot2:"<<endl;
    cin>>a>>b;
    Dot dot2(a,b);
    Line line(dot1,dot2);
    line.Showl();
    return 0;
}
10 回复
#2
peach54602015-05-05 13:56
你想说什么...
#3
hzj1996032015-05-07 13:00
回复 2楼 peach5460
有错误,不会改,求帮助
#4
peach54602015-05-07 13:01
错在哪里...
#5
诸葛欧阳2015-05-07 13:31
派生类的构造函数错了
#6
hzj1996032015-05-07 21:03
回复 5楼 诸葛欧阳
该怎么改呢?
#7
小狼烟2015-05-09 19:04
改了下,看有什么不合适的?
程序代码:
#include<iostream>
using namespace std;
class Dot
{
private:
    float x,y;
public:
    Dot(){}
    Dot(float a,float b)
    {
        x=a;
        y=b;
    }
    Dot
    float getsx()
    {
        return (x);
    }
    float getsy()
    {
        return (y);
    }
    void Show(){cout<<"x="<<getsx()<<" "<<"y="<<getsy()<<endl;}
};
class Line:public Dot
{
    Dot d1,d2;
public:   
    float x,y;

    Line(Dot dot1,Dot dot2):d1(dot1),d2(dot2)
    {
    x=(d1.getsx()+d2.getsx())/2;  
    y=(d1.getsy()+d2.getsy())/2;
    }
   
void Showl(Dot d1,Dot d2)
{
  cout<<"Dot1:";
   d1.Show();
   cout<<"Dot2:";
   d2.Show();
   cout<<"Center:"<<"x="<<x<<" "<<"y="<<y<<endl;
}
};
int main()
{   float a,b;
    cout<<"Input Dot1:"<<endl;
    cin>>a>>b;
    Dot dot1(a,b);
    cout<<"Input Dot2:"<<endl;
    cin>>a>>b;
    Dot dot2(a,b);
    Line line(dot1,dot2);
    line.Showl(dot1,dot2);
    return 0;
}

#8
zcdjt2015-05-10 21:36
回复 7楼 小狼烟
他除了多了;号以外还有那错了,是大小写不对照吗?你指出来一下谢谢。
#9
小狼烟2015-05-11 09:45
回复 8楼 zcdjt
不是大小写不对照啊,你主函数里面Line调用Dot类里面没有对应的构造函数,你自己定义的DOT(x,y)不能用于Line的,跟大小写没关系
#10
hzj1996032015-05-11 19:43
回复 9楼 小狼烟
不是已经继承了么,为什么Line类里不能用Dot定义
#11
q2152362132015-05-12 11:56
子类继承父类,子类的构造函数却没实现父类的构造。。。。
1