皮德常面向对象的程序设计课程ppt

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

cout << "y: "<< obj1.gety( ) <<" "<< obj2.gety( ) << endl ;
9.1.2 静态函数成员
– 静态函数成员是类中的一个函数,有static修饰。 – 静态函数成员和静态数据成员类似,在对象生成
之前也已经存在。这就是说在对象产生之前,静 态的函数成员就能访问其它静态成员。 – 类外代码可以使用类名和作用域操作符来调用静 态成员函数。 – 静态成员函数只能引用属于该类的静态数据成员 或静态成员函数。见例【例9-2】。
5
// budget2.h文件的内容 。
class Budget
{ static float corpBudget;
float divBudget;
public:
Budget( ) { divBudget = 0; }
void addBudget( float b)
{
divBudget += b;
corpBudget += moffice; }
// Contents of auxi1.cpp #include "auxi1.h" #include "budget3.h"
// Definition of member function mainOffice.
void Aux::addBudget(float b, Budget &div)
cout << "Here are the division budget requests:\n"; for (i = 0; i < 4; i++) {
cout << "\tDivision " << (i + 1) << "\t\t\t "; cout << setw(7); cout << divisions[i].getDivBudget( ) << endl; cout << "\tAuxilary Office of Division " <<(i+1); cout << "\t"; cout << auxOffices[i].getDivBudget( ) << endl;
不采用友元 如何解决?
cout<< Distance(p1, p2) <<endl; }
9-3.cpp
9.2.2 类的成员函数作为另外一个类的友元
其他类的成员函数声明为一个类的友元函数,这 个成员函数也称为友元成员。
友元成员不仅可以访问自己所在类对象中的私有 成员和公有成员,还可以访问friend声明语句所在类对 象中的私有成员和公有成员,这样能使两个类相互合 作完成某一任务。 例:将Aux类的函数addBudget声明为Budget类的友元 函数。
class Point
{
int xPos, yPos ;
public:
Point(int xx=0, int yy=0 )
{ xPos=xx; yPos=yy; }
int GetXPos( ) { return xPos; }
int GetYPos( ) { return yPos; }
friend double Distance(Point &a, Point &b);
9.2.3 一个类作为另外一个类的友元
• 若一个类为另一个类的友元,则此类的所有成员 都能访问对方类的私有成员。
class A
{ int x;
friend class B; 不好 public:
的方
void Display( ){ cout<<x<<endl;}
法, } ;
勿模 class B
仿
{ A a;
};
12
double Distance( Point & a, Point & b) {
double dx=a.xPos-b.xPos; double dy=a.yPos-b.yPos; return sqrt(dx*dx+dy*dy); }
void main( ) {
Point p1(3, 5), p2(4, 6);
class Budget // Budget class declaration {
static float corpBudget; float divBudget; public: Budget( ) { divBudget = 0; } void addBudget(float B) { divBudget += B; corpBudget += divBudget; } float getDivBudget( ) { return divBudget; } float getCorpBudget( ) { return corpBudget; } static void mainOffice( float ); friend void Aux::addBudget(float, Budget &); };
public:
void Set(int i) { a.x = i; }
void Display( ) { a.Display( ); }
corpBudget += divBudget;
}
static void mainOffice( float );
float getDivBudget( ) { return divBudget; }
float getCorpBudget( ){ return corpBudget;}
};
// Contents of budget2.cpp #include "budget2.h"
float Budget::corpBudget = 0 ;
// Definition of static member function. void Budget::mainOffice(float moffice) {
corpBudget += moffice; }
//主程序pr9-2.cpp的内容 #include " budget2.h“ void main( ) { float amount;
int i; float bud;
cout << "Enter main office's budget request: "; cin >> amount; Budget::mainOffice(amount); Budget divisions[4];
for ( i = 0; i < 4; i++) { cout << "Enter the budget for Division ";
// Contents of budget3.cpp #include "budget3.h"
// Definition of static member of Budget class float Budget::corpBudget = 0;
// Definition of static member function mainOffice. void Budget::mainOffice(float moffice ) {
2
9.1.1 静态数据成员
1. 用关键字static声明; 2. 同一个类中的所有对象都共享该变量; 3. 必须在类外定义和初始化,用(::)来指明所属的类。 4. 静态变量不依赖于对象而存在,无论是否定义该类的
对象,这种类型的变量都存在。静态数据成员实际 上是在类外定义的一个变量,它的生存期和整个程 序的生存期一样,在定义对象之前,静态数据成员 就已经存在。
主要内容
9.1 静态成员 9.2 友元 9.3 对象赋值问题 9.4 拷贝构造函数 9.5 运算符重载 9.6 对象组合
1
9.1 静态成员
• 例:一个学生类,定义其对象张三、李四,他 们分别维护着类成员的一份副本(学号、姓名、 籍贯等)。
• 如果要统计一个班学生总数? – 用类外的变量记录,违背了数据封装。 – 用类的一个数据成员记录,导致多个副本, 不仅冗余,而且势必造成数据不一致。
cout << (i + 1) << " " ; cin >> bud; divisions[i].addBudget(bud); }
cout << "\n Here are the division budget :\n"; for ( i = 0; i < 4; i++) {
cout << "\t Division" << (i + 1) << "\t $ "; cout << divisions[i].getDivBudget( ) << endl; }
} cout << "\tTotal Requests (including main office):"; cout << divisions[0].getCorpBudget( ) << endl; }
auxi1.h budget3.h auxi1.cpp budget3.cpp 9-3.cpp
cout << "\t Total Requests: "; cout << divisions[0].getCorpBudget( ) << endl; }
budget2.h budget2.cpp 9-2.cpp
1. 对于静态的函数成员,是通过类名和作用域分辨符 调用的。
2. 也可以采用对象点的方式调用
};
int StaticDemo::x ; // 静态变量x将被StaticDemo类的所有对象共享,例如:
StaticDemo obj1, obj2 ;
obj1.putx(5) ;
obj1.puty( l0 ) ;
obj2.puty(20 ) ;
cout << "x: "<< obj1.getx( ) << " " << obj2.getx( ) << endl ;
{
auxBudget += b;
div.corpBudget += auxBudget;
}
Attention
Plz
// Contents of main program #include "budget3.h“
void main( ) { float amount;
int i; float bud;
9.2 友元函数
• 引入友元的原因?
1. 友元函数不是类中的函数成员,但它和类的函数成员一 样,可以访问类中定义的私有成员。
2. 友元函数可以是一个外部函数,也可以是另外一个类的 函数成员。
3. 将某个函数声明为一个类的友元方式,前面加friend 。
11
9.2.1 外部函数作为类的友元
【例9-3】求两个点之间的距离。
14
class Budget ; // 对Budget类超前使用说明
class Aux // Aux类的定义 { private:
float auxBudget ; public:
Aux( ) { auxBudget = 0 ; } void addBudget( float , Budget & ) ; float getDivBudget( ) { return auxBudget ; } };
cout << "Enter the main office's budget: ";
cin >> amount;
Budget::mainOffice(amount);
Budget divisions[4];
Aux
auxOffices[4];
for (i = 0; i < 4; i++) {
cout << "Enter the budget request for division "; cout << (i + 1) << ": "; cin >> bud; divisions[i].addBudget(bud); cout << "Enter the budget request for division"; cout << (i + 1) << "'s auxiliary office: "; cin >> bud; auxOffices[i].addBudget(bud, divisions[i]); }
3
class Leabharlann BaidutaticDemo
{ static int x ;
int y ;
public: void putx( int a){ x=a ; }
理解它!
void puty( int b ){ y=b ; }
int getx( ) { return x ; }
int gety( ) { return y ; }
相关文档
最新文档