友元:
友元函数:
#include
using namespace std ;
class Time {
public:
Time (int,int ,int );
friend voiddisplay (Time &);
private:
inthour;
intminute;
int sec;
};
Time ::Time(int h , int m ,int s )
{
hour = h;
minute = m;
sec =s;
}
void display( Time & t)
{
cout<<t.hour <<endl << t.minute<< t.sec ;
}
int main()
{
Timet(10,13,56);
display(t);
return 0;
}
友元成员函数
#include
using namespace std ;
class Date ;
class Time {
public:
Time (int,int ,int );
voiddisplay(Date &);
private:
inthour;
intminute;
int sec;
};
class Date{
public:
friend voidTime::display(Date &);//声明time类中的display函数为本类的友元成员函数
Date(int ,int ,int );
private:
intmonth;
int day;
int year;
};
Time ::Time(int h , int m ,int s )
{
hour = h;
minute = m;
sec =s;
}
//注意:void Time::display( Date & t)必须声明在classDate后,因为他们要使用Date里面的成员。
void Time::display( Date & t)
{
cout<<t.month <<endl << t.day<<endl<< t.year<<endl ;
}
Date::Date(int m, int d ,int y )
{
month = m;
day =d;
year = y;
}
int main()
{
Timet(10,13,56);
Date dl(12,25,2004);
t.display(dl);
return 0;
}
类的提前引用:
在一般情况下,类必须先声明,然后才能使用他。但是在特殊情况下,在正式生命之前,需要使用该类名。但是注意,类的提前声明使用范围是有限的。只有在正式声明一个类以后才能定义他去定义对象。
因为在定义对象时要为这些对象分配存储空间,在正式声明类之前,编译系统无法确定应为对象分配多大的空间。编译系统只有在“见到”类体后,才能确定应该为对象预留多大的空间。在对一个类做了提
前声明后,可以使用该类的名字去定义指向该类型对象的指针变量或者对象的引用(),因为指针变量和引用本身的大小是固定的,以它指向的类对象的大小无关。
友元类:
#include
using namespace std ;
class Date ;
class Time {
public:
Time (int,int ,int );
voiddisplay(Date &);
private:
inthour;
intminute;
int sec;
};
class Date{
public:
friend classTime;//声明time类中的display函数为本类的友元成员函数
Date(int ,int ,int );
private:
intmonth;
int day;
int year;
};
Time ::Time(int h , int m ,int s )
{
hour = h;
minute = m;
sec =s;
}
void Time::display( Date & t)
{
cout<<t.month <<endl << t.day<<endl<< t.year<<endl ;
}
Date::Date(int m, int d ,int y )
{
month = m;
day =d;
year = y;
}
int main()
{
Timet(10,13,56);
Date dl(12,25,2004);
t.display(dl);
return 0;
}
友元函数:
#include
using namespace std ;
class Time {
};
Time ::Time(int h , int m ,int s )
{
}
void display( Time & t)
{
}
int main()
{
}
友元成员函数
#include
using namespace std ;
class Date ;
class Time {
};
class Date{
};
Time ::Time(int h , int m ,int s )
{
}
//注意:void Time::display( Date & t)必须声明在classDate后,因为他们要使用Date里面的成员。
void Time::display( Date & t)
{
}
Date::Date(int m, int d ,int y )
{
}
int main()
{
}
类的提前引用:
因为在定义对象时要为这些对象分配存储空间,在正式声明类之前,编译系统无法确定应为对象分配多大的空间。编译系统只有在“见到”类体后,才能确定应该为对象预留多大的空间。在对一个类做了提
前声明后,可以使用该类的名字去定义指向该类型对象的指针变量或者对象的引用(),因为指针变量和引用本身的大小是固定的,以它指向的类对象的大小无关。
友元类:
#include
using namespace std ;
class Date ;
class Time {
};
class Date{
};
Time ::Time(int h , int m ,int s )
{
}
void Time::display( Date & t)
{
}
Date::Date(int m, int d ,int y )
{
}
int main()
{
}