以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  c++题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=62710)


--  作者:20060314
--  发布时间:5/16/2008 7:00:00 PM

--  c++题
1.以下程序有什么错误?如有请予以修改。
(1)使用静态成员函数。
#include <iostream.h>
#include <string.h>
class person{
public:
    char m_strName[20];
    long m_ID;
public:
    person (char* strName, long ID) { strcpy(m_strName, 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) {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) {radius=r;}
   int getRadius() { return radius;}
};
void main ()
{
   Circle c(100,150,200);
   cout<<"x="<<c.getX()<<",y="<<c.getY()<<",radius="<<c.getRadius()<<endl;
}
(3)关于常对象和常对象成员。
#include <iostream.h>
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(100);
  a.SetValue(0);
  a.Display();
}
2.指出以下程序中的错误,并加以修改。
#include <iostream.h>
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="<<c.GetData()<<",b="<<c.GetData()<<endl;
}
3.可以在哪两个函数中重绘视图?它们有何区别?
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms