C++ 基础 不能在一个类中定义另一个类的成员函数 课本5.11

来源:互联网 发布:淘宝怎么查年花费 编辑:程序博客网 时间:2024/05/03 14:58
//============================================================================
// Name        : 11.cpp
// Author      : zhaoming
// Version     :
// Copyright   : copyright to zhaoming
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;


class INTSET;  //不完整声明或前向声明
class REALSET{
float *elems;
int card,maxcard;
public:
REALSET(INTSET &s);
int sum(REALSET s);
~REALSET(){
cout <<"aaaa";
delete elems;
}
};
class INTSET{
int *elems,card,maxcard;
//注意:不能在一个类的内部定义另一个类的构造函数,但可以声明
/*
* 课本上是这样写的
* friend  REALSET::REALSET(INTSET &s)//自动成为inline函数
{
//本函数声明为INTSET的友元后,可直接访问INTSET的成员
elems = new float[maxcard = s.maxcard];
card = s.card;
for(int i = 0; i < card; i++){
elems[i] = s.elems[i];
}
}
*/

friend int sum(REALSET s)
{
s.~REALSET();
return 0;
}
friend  REALSET::REALSET(INTSET &s);//自动成为inline函数


public:
INTSET(int maxcard);
~INTSET(){
delete elems;
}
int getcard(){
return card;
}
int getelems(int i){
return elems[i];
}
};
REALSET::REALSET(INTSET &s)
{
//本函数声明为INTSET的友元后,可直接访问INTSET的成员
elems = new float[maxcard = s.maxcard];
card = s.card;
for(int i = 0; i < card; i++){
elems[i] = s.elems[i];
}
}
INTSET::INTSET(int max){
elems = new int[maxcard = max];
card = 0;
}
int main()
{
INTSET iset(20);
REALSET rset(iset);
}

原创粉丝点击