猿问

求高手帮忙,主函数和头文件exp3_1.h在问题补充里了!

#include<iostream>
using namespace std;
#include"exp3_1.h"
int main()
{
Girl g("sss",20);
Boy b("sll",18);
g.Print();
b.Print();
g.VisitBoy(b);
// b.VisitGirl(g);
return 0;
}

//头文件exp3_1.h
#include<iostream>
#include<string>
using namespace std;
class Boy; //向前引用
class Girl
{
char name[20]; //类数据成员默认私有
int age;
public:
Girl(char N[],int A); //构造函数
~Girl() //析构函数
{
cout<<"Girl destructing...\n";
}
void Print();
void VisitBoy(Boy &); //在其中访问Boy类的私有成员
};

class Boy
{
char name[20];
int age;
friend Girl; //将Girl类作为Boy类的友元类
public:
Boy(char N[],int A);
~Boy()
{
cout<<"Boy destructing...\n";
}
void Print();
// void VisitGirl(Girl &);
};

//Girl类的成员函数

Girl::Girl(char N[],int A) //Girl类的构造函数的实现代码
{
strcpy(name,N);
age=A;
cout<<"Girl constructing...\n";
}
void Girl::Print() //Girl类的输出函数实现代码
{
cout<<"Girl's name: "<<name<<endl;
cout<<"Girl's age: "<<age<<endl;
}
void Girl::VisitBoy(Boy &boy) //通过Girl类中的VisitBoy函数访问Boy类中的数据成员
{
cout<<"Boy's name: "<<boy.name<<endl;
cout<<"Boy's age: "<<boy.age<<endl;
}

//Boy类的成员函数

Boy::Boy(char N[],int A)
{
strcpy(name,N);
age=A;
cout<<"Boy constructing...\n";
}
void Boy::Print()
{
cout<<"Boy's name: "<<name<<endl;
cout<<"Boy's age: "<<age<<endl;
}
/*
Boy::VisitGirl(Girl &girl)
{
cout<<"Girl's name: "<<girl.name<<endl;
cout<<"Girl's age: "<<girl.age<<endl;
}
*/

牛魔王的故事
浏览 143回答 1
1回答

交互式爱情

Girl constructing...Boy constructing...Girl's name: sssGirl's age: 20Boy's name: sllBoy's age: 18Boy's name: sllBoy's age: 18Boy destructing...Girl destructing...Press any key to continue***************************************以上是输出结果 是不是你所建的不对啊完全没有错误啊 //头文件exp3_1.h 下面的代码 放于你所建c/c++ Header file 中 然后主函数文件放于你建的source file 中,cpp 文件
随时随地看视频慕课网APP
我要回答