1 January 2021
cpp -- Object-Oriented Thinking(2)
by Jerry Zhang
Instance and Static Members
A static variable is shared by all objects of the class. A static function cannot access instance members of the class.
UML中,static用下划线表示。
声明class时,在.h文件中,在变量或函数前加上static关键字。
example:
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
public:
Circle();
Circle(double);
double getArea();
double getRadius();
void setRadius(double);
static int getNumberOfObjects();
private:
double radius;
static int numberOfObjects;
};
#endif
实现class时,在.cpp文件中,static的变量可以定义成全局变量,比如下面第二行int Circle::numberOfObjects = 0;。
#include "CircleWithStaticDataFields.h"
int Circle::numberOfObjects = 0;
// Construct a circle object
Circle::Circle()
{
radius = 1;
numberOfObjects++;
}
// Construct a circle object
Circle::Circle(double newRadius)
{
radius = newRadius;
numberOfObjects++;
}
// Return the area of this circle
double Circle::getArea()
{
return radius * radius * 3.14159;
}
// Return the radius of this circle
double Circle::getRadius()
{
return radius;
}
// Set a new radius
void Circle::setRadius(double newRadius)
{
radius = (newRadius >= 0) ? newRadius : 0;
}
// Return the number of circle objects
int Circle::getNumberOfObjects()
{
return numberOfObjects;
}
Constant Member Functions
C++ also enables you to specify a constant member function to tell the compiler that the function should not change the value of any data fields in the object.
在函数的参数前加const,那么在这个函数里,该参数就不能被修改。比如:
void printCircle(const Circle& c)
{
cout << "The area of the circle of "
<< c.getRadius() << " is " << c.getArea() << endl;
}
也可以用const关键字定义一个“常量函数”,告诉编译器这个函数不能修改该对象的数据,比如get方法,一般不修改对象的数据。const关键字放在函数头的末尾。
函数声明:
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
public:
Circle();
Circle(double);
double getArea() const;
double getRadius() const;
void setRadius(double);
static int getNumberOfObjects();
private:
double radius;
static int numberOfObjects;
};
#endif
函数实现:
#include "CircleWithConstantMemberFunctions.h"
int Circle::numberOfObjects = 0;
// Construct a circle object
Circle::Circle()
{
radius = 1;
numberOfObjects++;
}
// Construct a circle object
Circle::Circle(double newRadius)
{
radius = newRadius;
numberOfObjects++;
}
// Return the area of this circle
double Circle::getArea() const
{
return radius * radius * 3.14159;
}
// Return the radius of this circle
double Circle::getRadius() const
{
return radius;
}
// Set a new radius
void Circle::setRadius(double newRadius)
{
radius = (newRadius >= 0) ? newRadius : 0;
}
// Return the number of circle objects
int Circle::getNumberOfObjects()
{
return numberOfObjects;
}
- Note: 只能把实例函数定义为const,不能把static的函数定义为const。
Thinking in Objects
The procedural paradigm focuses on designing functions. The object-oriented paradigm couples data and functions together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects.
Object Composition
An object can contain another object. The relationship between the two is called composition.
Composition is actually a special case of the aggregation relationship.
如果一个对象是由另一个对象独占的,那就叫做composition,否则叫做aggregation。比如“一个学生有一个名字”,那么这个名字只是这个学生拥有的,就叫composition。比如“一个学生有一个地址”,那么这个地址也可以是其他人的,就叫aggregation。
UML图中,用实心的菱形放在拥有者的一边,来代表composition;用空心的菱形代表aggregation。
