C++ 题目 求助

来源:百度知道 编辑:UC知道 时间:2024/06/25 19:41:30
判断说明题(阅读下列程序,判断其有无错误,若有错,请指出错误之处并说明理由;若无错,只需说明该程序正确。)
1 声明类date
class date{
int day;
int month;
int year;
public:
void date(int y,int m,int d);
void ~date(int y,int m,int d);
……………………………………
};

2 #include<iostream.h>
class B
{public:
B(int nb,float fb){n=nb;f=fb;}
B(B &b){n=b.n;f=b.f;}
~B(){}
private:
int n;
float f;
};
void main(){
B b1;
B b2=b1;
}

3 class test
{
private:
static int x=0;
public:
static int fun=();
};

4 #include<iostream>
using namespace std;
class A{
int x;
public:
A(int a):x(a){cout<<”Constructing A\n”;}
};
class B:public A{
public:
B(int a){cout<<”Constructing B\n”;}
}
void main(){
A a(5);
}

1.data类中的构造函数跟析构函数错误.这两个函数没有返回类型,并且构造函数最好要初始化数据成员
2.在类中你定义了一个自己的构造函数,但是B b1;这个需要默认的构造函数,因为你定义了自己的构造函数,所以编译器不会自己给你合成,所以还得自己在写一个
3.静态数据成员要在类外赋值
4.在继承中,如果基类中有显示的构造函数,那么子类中必须显示定义构造函数,并且要初始化基类.你这里的B(int a)没有初始化A类中的数据成员,所以错

1 有错 构造和析构函数不能有返回类型,析构函数不能带有参数
2 缺少构造函数,因为你的对象是没初始化值
3静态数据在必须类外初始化,但是static const int x=0;这是正确的,这是有点特殊啊
4B(int a){cout<<”Constructing B\n”;} 这句也错啦,有继承,要把基类的数据成员初始化,可以改为B(int a):A(a){cout<<"coustructing B"<<endl;}