版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016第第6 6章章 类与对象类与对象 6.1 6.1 类与对类与对象象 6.2 6.2 构造函数和析构函数构造函数和析构函数 6.3 6.3 类的其他成员类的其他成员 6.4 6.4 类的包含类的包含 小结小结华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016 类(类(ClassClass)是面向对象程序设计()是面向对象程序设计(OOPOOP)实现信息封装的基础。)实现信息封装的基础。 类是用户定义类型,也称为类类型类是
2、用户定义类型,也称为类类型 每个类包含数据说明和一组操作数据或传递消息的函数。类的每个类包含数据说明和一组操作数据或传递消息的函数。类的 实例称为对象实例称为对象第第6 6章章 类与对象类与对象 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016 面向对象编程的程序基本单位是类面向对象编程的程序基本单位是类 类是数据和操作数据的函数的封装类是数据和操作数据的函数的封装 类的对象使用自己的方法完成对数据的操作类的对象使用自己的方法完成对数据的操作 类可以隐藏数据和操作细节,对象通过类接口与外部通信类可以隐藏数据和操作细节,对象通过类接口与外部通信 6.1 6.1 类与对
3、象类与对象 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1 类与对象/ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .数组与数组类数组与数组类华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016class Array/定义数组类 int *ap ; int len
4、; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array & other) ; int main() Array a(10) , b(10) ;/ 声明对象 . a . Sort() ; b . Sort() ; / 调用排序方法 a = a + b ;/ 数组相加 ./ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int
5、, int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .数组与数组类数组与数组类6.1 类与对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array opera
6、or + (const Array & other) ; int main() Array a(10) , b(10) ;/ 声明对象 . a . Sort() ; b . Sort() ; / 调用排序方法 a = a + b ;/ 数组相加 ./ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .数组与数组类数组与数
7、组类class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array & other) ;类是数据和操作数据的函数的封装6.1 类与对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016class Array/定义数组类 int *ap ; int len ; public: Array( int size
8、 )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array & other) ; int main() Array a(10) , b(10) ;/ 声明对象 . a . Sort() ; b . Sort() ; / 调用排序方法 a = a + b ;/ 数组相加 ./ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; int main() int
9、a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .数组与数组类数组与数组类对象使用自己的方法对数据操作6.1 类与对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1 类与对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20162016class 钟 private : 钟的构造; public : 读取时间值读取时间值 ; 调整时间值调整时间值 ;对象通过类接口与外部通信6.1 类与对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 2
10、01620166.1.1 6.1.1 定义类和对象定义类和对象 / 例例6-1 一个类的例子一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int mo
11、nth; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例6-1 一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4
12、= 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0
13、 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例6-1 一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ;
14、year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; yea
15、r = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;关键字关键字定义一个类定义一个类标识符标识符类名类名 class ,struct,union 都可以定义一个类:都可以定义一个类: class缺省说明时,其成员被认为缺省说明时,其成员被认为是私有的是私有的 struct 若不特别指出,其所有成员若不特别指出,其所有成
16、员都是公有的都是公有的 union其所有成员都是公有的,且其所有成员都是公有的,且不能更改不能更改华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例例6-1 一个类的例子一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 !=
17、0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate ; a.Set (10, 16, 2003) ; a.Print() ;Tdate 类型的一个类型的一个对象(实例)对象(实例)华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例例6-1 一个类的例子一个类的例子#includeusing namespace std ;clas
18、s Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; 类由成员构成
19、: 数据成员数据成员描述对象的属性 成员函数成员函数描述对象的方法华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例例6-1 一个类的例子一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 =
20、 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;int month; int day; int year;数据成员数据成员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例例6-1 一个类的例子一个类的例子#includeusing namespace std ;class Tda
21、te public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; void Set(in
22、t m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; 类中定义成员函数类中定义成员函数内联函数处理内联函数处理华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例例6-1 一个类的例子一个类的例子#includeusing na
23、mespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a
24、.Print() ; void Set(int m, int d, int y ) ; int IsLeapYear() ; void Print() ;在类外定义在类外定义成员函数成员函数 void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year.month.dayendl ; 华南理工大学计算机学院华南理工大学计算机学院 周霭
25、如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例6-1 一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int mon
26、th; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;成员的性质由关键字成员的性质由关键字public、protected、private 决定决定公有公有 公有段的成员是提供给外部的接口公有段的成员是提供给外部的接口 保护保护 保护段成员在该类和它的派生类中可见保护段成员在该类和它的派生类中可见 私有私有 私有段成员仅在类中可见私有段成员仅在类中可见各段中既可以包含数据成员,也可以包含成员函数各段中既可以包含数据成员,也可以包含成员函数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如
27、 201620166.1.1 6.1.1 定义类和对象定义类和对象 / 例6-1 一个类的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month;
28、int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; class 类名类名 public:公有段数据成员和成员函数公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: class 类名类名 public:公有段数据成
29、员和成员函数公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中例:例: class link link * next ; ;/ 声明一个指向声明一个指向link类类型的指针类类型的指针华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: class 类名类名 public:公有段数据成员和成员
30、函数公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中例:例: class X ; class Y X dataMember ; ;/ 声明一个类类型数据成员声明一个类类型数据成员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: class 类名类名 public:公有段数据成员和成员函数
31、公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中例:例: class dataMember ; ;/ 错误错误错误错误无穷递归结构无穷递归结构华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: class 类名类名 public:公有段数据成员和成员函数公有段数据成员和成员函数 ; pro
32、tected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中2. 类可以无名,用于直接声明对象类可以无名,用于直接声明对象例:例: class mydate ;/ 直接直接声明一个对象声明一个对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: class 类名类名 public:公有段数据成员和成员函数公有段数据成员和成员函数 ;
33、protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中2. 类可以无名,用于直接声明对象类可以无名,用于直接声明对象/例例6-2#includeusing namespace std ;class empty ;int main() empty e1 ; cout &e1 = &e1 endl ; cout sizeof e1 = sizeof(e1) endl ; 3. 类是一个程序包。可以只有数据成员或只有成员函数,或
34、者为空。类是一个程序包。可以只有数据成员或只有成员函数,或者为空。 一个空类一个空类华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: class 类名类名 public:公有段数据成员和成员函数公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中2. 类可以无名,用于直接声明对象类可以无名,
35、用于直接声明对象3. 类是一个程序包。可以只有数据成员或只有成员函数,或者为空。类是一个程序包。可以只有数据成员或只有成员函数,或者为空。 /例例6-2#includeusing namespace std ;class empty ;int main() empty e1 ; cout &e1 = &e1 endl ; cout sizeof e1 = sizeof(e1) endl ; 空类对象空类对象华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.1 6.1.1 定义类和对象定义类和对象 类说明的一般形式为:类说明的一般形式为: cla
36、ss 类名类名 public:公有段数据成员和成员函数公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数保护段数据成员和成员函数 ; private:私有数据成员和成员函数私有数据成员和成员函数 ; ; 1. 允许已定义类名出现在类的说明中允许已定义类名出现在类的说明中2. 类可以无名,用于直接声明对象类可以无名,用于直接声明对象/例例6-2#includeusing namespace std ;class empty ;int main() empty e1 ; cout &e1 = &e1 endl ; cout sizeof e1 = size
37、of(e1) ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-3 访问对象的公有成员访问对象的公有成员 #includeusing namespace std ;class Tclass public: int x,y ; void print() cout x “,” ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-3 访问对象的公有成员访问对象的公有成员#includeusing namespace std ;class Tclass: int x,y ; void print()
38、cout x “,” ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-3 访问对象的公有成员访问对象的公有成员#includeusing namespace std ;class Tclass: int x,y ; void print() cout x “,” ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-4 用指针访问对象成员用指针访问对象成员 #includeusing namespace std ;class Tclass public : int x, y ; void pr
39、int() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-4 用指针访问对象成员用指针访问对象成员 #includeusing namespace std ;class Tclass public : int x,
40、y ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-4 用指针访问对象成员用指针访问对象成员 #includeusing namespace std ;class Tclass public : int x, y
41、 ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; = 100 ; = 200 ; ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-4 用指针访问对象成员用指针访问对象成员 #includeusing namespace std ;class Tclass public : int x, y ; void pr
42、int() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 运算符访问对象成员运算符访问对象成员 6.1.2 6.1.2 访问对象成员访问对象成员 /例例6-4 用指针访问对象成员用指针访问对象成员 #includeusing namespace std ;class Tclass public : int x,
43、y ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= add() ;向函数向函数传递对象地址传递对象地址 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace
44、 std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;华南理工大学计算
45、机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY (
46、 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY
47、() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #in
48、clude /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ;
49、obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y向哪个对象向哪个对象的数据成员赋值?的数据成员赋值? 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y x = a ; this-y =
50、b ; obj1 . setXY ( 10, 15, &obj1 ) ;成员函数隐含定义成员函数隐含定义 this 指针指针接受调用对象的地址接受调用对象的地址华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y x = a ; this-y
51、= b ; this 指针不能显式声明指针不能显式声明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y x = a ; this-y = b ; this 指针不能显式声明指针不能显式声明可以显式使用可以显式使用obj1 . setXY ( 10,
52、 15, &obj1 ) ;华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . p
53、rintXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y通过调用函数的对象通过调用函数的对象this 指针获取对象地址指针获取对象地址华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ;
54、 public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj
55、3.y华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj
56、2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b
57、 ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y在在 obj1 上操作上操作 10 , 15 20 , 25 30 , 35华南理工大学计算机
58、学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY (
59、20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 35华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x
60、 = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 35在在 obj2 上操作上操作华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 201620166.1.3 this6.1.3 this指针指针 #include /例例6-5using namespa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人教版(2019) 必修第一册 Unit 3 Sports and Fitness Reading and Thinking (教学设计)
- DB14-2801-2023 工业涂装工序大气污染物排放标准
- 2021年春人教版物理中考考点突破-《光现象》(附答案)
- 12《总也倒不了的老屋》第二课时课件三年级上册语文统编版
- 《南方稻田冬绿肥机械化生产技术规程》征求意见稿
- 《机器学习-Python实战(微课版)》课件 第十二章 神经网络
- 2024智慧城市数据共享标准规范
- 《百合花》课件+2024-2025学年统编版高中语文必修上册+
- 2个人共同合伙协议书范文范本
- 巷口镇梨枣产业化示范基地建设项目可行性研究报告
- 沪科版九年级物理课件-第二十章-能源、材料与社会
- 商场装修技术标
- 材料研究方法:第3章 电子、原子、单胞对x射线的散射知识点
- 人教统编版二年级语文上册《8 古诗二首》课堂教学课件PPT小学公开课
- 英语-缓解压力课件
- 管理会计-全面预算课件
- 酒店管理卫生质量检查标准
- 煤矿智能化建设课件
- 弱电系统测试调试方案
- 电子商务完整课件
- 五年级作文-开头技巧课件
评论
0/150
提交评论