C++问题(哪里出错了?怎么改?)
#include<iostream>#include<cstring>usingnamespacestd;classEmployee;//前向声明类Employeescl...
#include<iostream>#include<cstring>using namespace std;class Employee; //前向声明类Employeesclass Manager{ public: Manager(int pid,char* pname,int page) { id=pid; name=new char[strlen(pname)+1]; if(name!=0) strcpy(name,pname); age=page; } ~Manager() { delete[] name; } void printEmployee(Employee& e); friend void printManager(Manager& m); private: int id,age; char* name; //start // 各成员定义,友元函数 //end};void Manager::printEmployee(Employee& e){ cout<<"this is a empylee: id: "<<id<<", name: "<<name<<", age: "<<age<<endl;}void printManager(Manager& m){ cout<<"this is a manager: id: "<<m.id<<", name: "<<m.name<<", age: "<<m.age<<endl;}class Employee{ friend class Manager; int id,age; char* name; public: Employee(int pid,char* pname,int page) { id=pid; name=new char[strlen(pname)+1]; if(name!=0) strcpy(name,pname); age=page; } ~Employee() { delete[] name; }};int main(){ Manager m1(1001, "zhangsan", 22); printManager(m1); //调用友元函数输出Manager信息 Employee e1(2001, "lisi", 18); m1.printEmployee(e1); //通过友元类Manager输出Employee信息 return 0;}
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询