//============================================================================// 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);
}