C++基础题

来源:百度知道 编辑:UC知道 时间:2024/05/31 02:49:53
#include<iostream>
using namespace std;
class Boy;
class Girl
{
char name[25];
int age;
public:
Girl(char N[],int A)
{
strcpy(name,N);
age=A;
cout<<"Girl constructing..."<<endl;
}
~Girl()
{
cout<<"Girl destructing..."<<endl;
delete[]name;
}
void Print()
{
cout<<"Girl's name:"<<name<<endl;
cout<<"Girl's age:"<<name<<endl;
}
VisitBoy(Boy &); //在其中访问Boy类的私有成员
};
Girl::VisitBoy(Boy &x)
{
cout<<"Boy's secrets~"<<x.name<<" "<<x.age<<endl;
}
class Boy
{
char name[25];
int age;
friend Girl;
public:
Boy(char N[],int A)
{
strcpy(name,N);
age=A;
cout<<"Boy constructi

1. new生成的,才可以用delete 故那个delete不需要
2. Girl中的visitboy的函数,boy还有没有定义,要移到boy的定义后面.
3. friend Girl; 这里的分号要半角的分号
4. Boy A("孙锋",20);
Girl B("米莱",24); 这两句的逗号要半角的逗号
5. A.VisitBoy(B); Boy中的没有函数VisitBoy()
若改为VisitGirl()
则visitGirl还没有函数体,且声明中,没有函数值类型
可暂时改为 void VisitGirl(Girl &p) { }

这样改后就没有错了.

#include<iostream>
using namespace std;
class Boy;

class Girl
{
char name[25];
int age;
public:
Girl(char N[],int A)
{
strcpy(name,N);
age=A;
cout<<"Girl constructing..."<<endl;
}
~Girl()
{
cout<<"Girl destructing..."<<endl;
}
void Print()
{
cout<<"Girl's name:"<<name<<endl;
cout<<"Girl's age:"<<name<<endl;
}
VisitBoy(Boy &); //在其