| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1561 人关注过本帖
标题:帮我看下这题错哪了?急!!!
只看楼主 加入收藏
smltq
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:566
专家分:400
注 册:2008-1-21
收藏
 问题点数:0 回复次数:11 
帮我看下这题错哪了?急!!!
#include<iostream.h>
class A
{
protected:
   int a;
pudlic:
   void SetData(int x){a=x;}
   int GetData() {return a;}
};
classs B
{
protected:
    int b;
public:
    void setData(int y){a=y;}
    int GetData() {return b;}
};
class C : public A, public B
{
public:
    void setData(int x,int y){ a=x; b=y; }
};
void main()
{
    C c;
    c.setData(30,70);
    cout<<"a="<<c.GetData()<<",b="<<c.GetData()<<endl;
}
搜索更多相关主题的帖子: int void public setData GetData 
2008-04-04 17:12
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
除开语法错误.这个程序最在的问题在于,多重类的函数调用不明确.
    C c;
    c.setData(30,70);
    cout<<"a="<<c.GetData()<<",b="<<c.GetData()<<endl;
c现在不知道是调用A类的还是应该调用B类的

=×&D o I p R e E n C g T l X&×=
2008-04-04 17:22
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
#include <iostream>
using namespace std;

class A
{
protected:
   int a;
public:
   void SetData(int x){a=x;}
   int GetData() {return a;}
};
class B
{
protected:
    int b;
public:
    void setData(int y){b=y;}
    int GetData() {return b;}
};
class C : public A, public B
{
public:
    void setData(int x,int y){ a=x; b=y; }
};
void main()
{
    C * c = new C;
    c->setData(30,70);
    cout<<"a="<<static_cast<A*>(c)->GetData()<<",b="<<static_cast<B*>(c)->GetData()<<endl;
    delete c;
}

=×&D o I p R e E n C g T l X&×=
2008-04-04 17:26
smltq
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:566
专家分:400
注 册:2008-1-21
收藏
得分:0 
谢谢!我没学C++不是很懂,,,,朋友问我的
谢谢2楼的朋友!
2008-04-04 17:31
smltq
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:566
专家分:400
注 册:2008-1-21
收藏
得分:0 
在帮我看下这些题有没有错!谢谢
以下程序有什么错误?如有请给予修改.
(1)使用静态成员函数.
#include<iostream.h>
#include<string.h>
classpeson{
public:
   char m_strName[20];
   long m_ID;
public:
   person(char*strName,long ID){strcpy(m_strName);m_ID=ID;}
   static long GetID() {return m_ID;}
};
void main ()
{
  person personl ("LiuJun",1101640524);
cout<<"ID="<person::GetID()<<'\n';
}
(2)派生类的构造函数调用基类的构造函数。
#include<iostream.h>
class point
{
protected:
   int x,y;
public:
   point(int a ,int b){a=x;y=b;}
   int getX(){return x;}
   int getY(){return y;}
};
 class Cirrcle :public point
{
protected
   int radius;
public:
   Circle(int a=0,int b=0,int r=o){redius=r;}
   int getRadius() {return redius;}
};
vodi main()
{
   Circle c(100,150,200)
  cout<<"x="<<c.getX()<<",y="<<c.getY()<<", radius="<<c.get Radius()<<endl;
}
(3)关于常对象和常对象成员。
#include<iostream.h>
class Sample
{
private:
   int n;
public:
   Aample(int x){n=x;}
   void SetValue(int x){n=x;}
   void Display(){cout<<"n="<<end1;}
};
void main()
{
   const Sample a(100);
   a.SetValue(0);
   a.Display();
}
2008-04-04 17:35
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
又看了下.应该这样改才最好的
#include <iostream>
using namespace std;

class A
{
protected:
   int a;
public:
   void SetData(int x){a=x;}
   int GetData() {return a;}
};
class B
{
protected:
    int b;
public:
    void setData(int y){b=y;}
    int GetData() {return b;}
};
class C : public A, public B
{
public:
    void setData(int x,int y){ a=x; b=y; }
};
void main()
{
    C c;
    c.setData(30,70);
    cout<<"a="<<static_cast<A>(c).GetData()<<",b="<<static_cast<B>(c).GetData()<<endl;
}

=×&D o I p R e E n C g T l X&×=
2008-04-04 17:38
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
(1)使用静态成员函数.改好(我现在正无聊)
#include <iostream>
using namespace std;
#include<string.h>
class person{
public:
   char m_strName[20];
   static long m_ID;
public:
   person(char*strName,long ID){strcpy(m_strName, strName);m_ID=ID;}
   static long GetID() {return m_ID;}
};
long person::m_ID;
void main ()
{
  person personl ("LiuJun",1101640524);
cout<<"ID="<<person::GetID()<<'\n';
}

(2)派生类的构造函数调用基类的构造函数。
#include <iostream>
using namespace std;
class point
{
protected:
   int x,y;
public:
   point(int a ,int b){x=a;y=b;}
   int getX(){return x;}
   int getY(){return y;}
};
class Circle :public point
{
protected:
   int radius;
public:
    Circle(int a=0,int b=0,int r=0) : point(a, b) {radius=r;}
   int getRadius() {return radius;}
};
void main()
{
   Circle c(100,150,200);
  cout<<"x="<<static_cast<point>(c).getX()<<",y="<<static_cast<point>(c).getY()<<", radius="<<c.getRadius()<<endl;
}

(3)关于常对象和常对象成员。
#include <iostream>
using namespace std;
class Sample
{
private:
   int n;
public:
   Sample(int x){n=x;}
   void SetValue(int x){n=x;}
   void Display(){cout<<"n="<<n<<endl;}
};
void main()
{
   const Sample * a = new Sample(100);
   const_cast<Sample*>(a)->SetValue(0);
   const_cast<Sample*>(a)->Display();
   delete a;
}

=×&D o I p R e E n C g T l X&×=
2008-04-04 17:53
smltq
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:566
专家分:400
注 册:2008-1-21
收藏
得分:0 
这C与C++完全不一样...
语法都不同的...哎
2008-04-04 17:55
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
我是一个无聊的人

=×&D o I p R e E n C g T l X&×=
2008-04-04 17:57
smltq
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:566
专家分:400
注 册:2008-1-21
收藏
得分:0 
朋友...干脆你留个QQ,我让他直接请教你好吗?
2008-04-04 17:58
快速回复:帮我看下这题错哪了?急!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012994 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved