类和对象Chapter5ClassesandObjects-PPT精品
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// do something; } double f1() {
// return a number }
dinoluinbeledAo:u:fb2l(e) A::f2() {
// return a number }
double f2();
};
RIengliunlearfufunnctciotionn
(ClassName::)var_name…
17
Static Functions
Member function that can be called without creating an instance of the class
To declare:
static type functionName(parameters){}
TotalArea
Run
16
Static/Class Variables (静态/类变量)
Data fields shared by all the objects of the same class Stored in static memory region To declare:
“static” appears in declaration file
To call: ClassName::functionName(arguments);
18
Example of Static Members
circle1
Circle
-radius: double -numberOfObjects: int +getNumberOfObjects(): int +getArea(): double
9
Pointers to Members
A class member pointer can be declared using the operator ::* with the class name.
For example:
class A { private:
int m; public:
cout<<largest()<<"\n";
}
14
§ 5.3 Memory Allocation and Static Members
Memory of member functions -- Common for all objects -- Created/Allocated when defined
Member function1
Member function2
Memory of member variables -- specific to an object -- Created/Allocated when object created
Object1: Member variable1 Object1: Member variable2 Object1: Member variable1
The following members are accessible outside the class
The following members are NOT accessible outside the class
No initial value in declaration!
class item {
Chapter 5 Classes and Objects
§5.1 Specifying a class §5.2 Defining Member Functions §5.3 Memory Allocation and Static Members §5.4 Passing Objects to/from Functions §5.5 Friendship
Getter/Accessor
A function usually named getXxx:
returnType getPropertyName() bool isPropertyName()
Setter/Mutator
A function usually named setXxx, e.g.:
Data fields and functions
objectName.dataField --references a data field in the object.
objectName.function(arguments) --invokes a function on the object.
radius = 1 numberOfObjects = 2
Instantiate (实例化)
circle2
UML Notation: underline: static variables or functions
radius = 5 numberOfObjects = 2
Memory 1
radius
c: Circle
radius: 5.0
Copy myCircle to c myCircle: Circle
radius: 5.0
c is an alias for myCircle
myCircle: Circle
radius: 5.0
c: addressof
myCircle
Normally: data in private; functions in public Some functions in private sections
Can only be called by member function of its class To protect data from modifications by outsiders
ClassName variableName;
For example, item x;
Memory for x is created
item x, y, z;
Data type vs. Variable Class vs. Object
4
Accessing Objects
To access the members of a class/object
and the function prototypes.
Implementation(实现):
In classname .cpp file, conventionally Implements the constructors and functions.
Circle.h
Circle.cpp
Run
13
Nesting of Member Functions
#include <iostream>
using namespace std;
class set{
int m, n;
A member function can be
public:
called by another member
void display(void);
§ 5.1 Specifying a Class
Object(对象)
Abstraction of entities State + Behavior
Class (类)
A user-defined data type that declare objects Variables + Functions
private: int number; float cost;
public: void getdata(int a, float b); //function declaration void putdata(void);
}; 3
Creating Objects
The syntax to create an object
5
Data hiding in class
class xyz {
int x; int y; public: int z; };
int main(){ xyz p; p.x = 0; //error, x is private p.z =10; //OK, z is public
}
6
Private Member Functions
11
Inline Declaration
A member function can be implemented inside a class declaration
automatically an inline function
class A {
public: A() {
Inline function
void show(); };
int A::* ip = &A::m;
10
§ 5.2 Defining Member Functions
return-type class-name :: function-name (parameters) {
Function body }
Scope resolution operation Identity label
class sample {
int m; void read(void); public: void update(void); void write(void); };
7
Getter and Setter
For users/clients of a class to retrieve and modify a data field encapsulated by “private”
Object1: Member variable2
15
Array of Objects
employee manager[3];
name
Manager[0]
age
Manager[1]
name age
Manager[2]
name age
Circle circleArray[3] = {Circle(3), Circle(4), Circle(5)};
2 numberOfObjects
5 radius
Circle5.h
19
Circle5.cpp Run
TestCircle5.cpp
§ 5.4 Passing Objects to/from Functions
You can pass objects by value or by reference/address.
void setPropertyName(dataType propertyValue)
8
A Simple Circle Class
To demonstrate creating objects, accessing data, and using functions
TestCircle
Run
static type var_name;//In declaration To initialize:
type ClassName::var_name = 0; //re-declaห้องสมุดไป่ตู้e it with initial value in implementation
To access/reference:
int largest(void); function of the same class
};
•Via function name directly (no
int set::largest(void){
object name)
if(m<=n) return m;
else return n;
}
void set::display(){
radius, getArea()
2
Declaring a Class
class class_name { private:
variable declarations; function declarations; public: variable declarations; function declarations; };
12
Separating Declaration from Implementation
Similar to the definition of function Declaration(声明):
In classname.h file, conventionally Simply lists all the data fields, constructor prototypes,