类静态信息工程学院计算机技术教研室_第1页
类静态信息工程学院计算机技术教研室_第2页
类静态信息工程学院计算机技术教研室_第3页
类静态信息工程学院计算机技术教研室_第4页
类静态信息工程学院计算机技术教研室_第5页
已阅读5页,还剩227页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 类(Class)是面向对象程序设计(OOP)实现信息封装的基础。 类是用户定义类型,也称为类类型 每个类包含数据说明和一组操作数据或传递消息的函数。类的 实例称为对象 C+的类由C语言的结构类型演变而来。本章首先复习结构类型, 然后讨论C+的类和对象 概述3.1 结构3.2 类与对象3.3 构造函数和析构函数3.4 静态成员3.5 友员小结概述 结构由数目固定的成员构成 各成员可以具有不同的数据类型 一个结构变量在内存占有一片连续的存储空间3.1 结构结构类型定义形式为:struct 标识符 类型 成员1 ; 类型 成员2 ; 类型 成员n ; ; 例:struct employee cha

2、r name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ;类型名3.1.1 定义结构例:struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ; 可以用不同方法定义一个结构变量(1) 声明类型之后声明变量employee worker1, worker2, *Emp ;3.1.1 定义结构例:struct employee char name 10 ; long code ;

3、float salary ; char address 50 ; char phone 20 ; ; 可以用不同方法定义一个结构变量(1) 声明类型之后声明变量worker1, worker2, *Emp ;(2) 声明类型的同时声明变量3.1.1 定义结构例:struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ; 可以用不同方法定义一个结构变量(1) 声明类型之后声明变量worker1, worker2, *Emp ;(2) 声明类型的同时声明变量(3) 直接声

4、明结构类型变量注意此时没有了结构类型标识符3.1.1 定义结构例:struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ;employee worker1, worker2, *Emp = &worker1 ; 说明(1) 结构变量占有一片连续内存 空间,具有结构类型的特征Wang Li9910834561200.5guang zhou87111111worker1Emp3.1.1 定义结构 说明(2) 一个结构类型的成员 可以是另一个已定义的结构类型struct

5、date int month ; int day ; int year ; ;struct employee char name 10 ; date birthday ; long code ; float salary ; char address 50 ; char phone 11 ; worker1, worker2 ;例如: 为职工结构添加出生日期信息 类型和变量声明为:3.1.1 定义结构 说明(3) 声明结构类型变量可以同时初始化struct employee char name 10 ; long code ; float salary ; char address 50 ;

6、char phone 11 ; worker = Wang Li , 991083456, 1200.5, guang zhou , 87111111 ;3.1.1 定义结构(1)访问结构变量的成员结构变量 . 成员# include struct weather/ 声明结构类型 double temp; double wind; ;void main ( ) weather today ;/ 声明结构类型变量 today . temp = 10.5 ;/ 对结构变量成员赋值 today . wind = 3.1 ; cout “Temp = ” today . temp endl ; / 按

7、成员输出 cout “Wind = ” today . wind 成员 (*结构指针 ) . 成员# include # include struct person char name20 ; unsigned long id; float salary; ;void main ( ) person pr1 ; person * pp ;/ 定义结构指针 pp = & pr1 ;/ 取结构变量地址 strcpy ( pp - name , “David Marat” ) ;/ 对结构成员赋值 pp - id = 987654321 ; pp - salary = 335.0 ; cout na

8、me t id t salary 成员 (*结构指针 ) . 成员# include # include struct person char name20 ; unsigned long id; float salary; ;void main ( ) person pr1 ; person * pp ;/ 定义结构指针 pp = & pr1 ;/ 取结构变量地址 strcpy ( pp - name , “David Marat” ) ;/ 对结构成员赋值 pp - id = 987654321 ; pp - salary = 335.0 ; cout name t id t salary

9、 endl ;3.1.2 访问结构(*pp).name (*pp). id (*pp). salary# include struct weather double temp; double wind; yesterday ;void main ( ) weather today ; yesterday . temp = 10.5 ; yesterday . wind = 3.1 ; today = yesterday ;/ 结构变量整体赋值 cout “Temp = ” today . temp endl ; cout “Wind = ” today . wind endl ;(3)类型相同

10、的结构变量可以整体赋值3.1.2 访问结构(3)类型相同的结构变量可以整体赋值例如:struct weather1 double temp; double wind; yesterday ;struct weather2 double temp; double wind; today ;“类型相同的变量”是指用同一类型标识符说明的变量yesterday 和 today尽管成员相同,但不是同类型变量不可以整体赋值3.1.2 访问结构 面向对象编程的程序基本单位是类 类是数据和操作数据的函数的封装 类的对象使用自己的方法完成对数据的操作 类可以隐藏数据和操作细节,对象通过类接口与外部 通信 3.2

11、 类和对象/ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .封装数组和数组类class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 /

12、重载运算符 +函数 Array operaor + (const Array & other) ; void main() Array a(10) , b(10) ;/ 声明对象 . a . Sort() ; b . Sort() ;/ 调用排序方法 a = a + b ;/ 数组相加 ./ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b ,

13、 10) ; .封装数组和数组类class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array & other) ; void main() Array a(10) , b(10) ;/ 声明对象 . a . Sort() ; b . Sort() ;/ 调用排序方法 a = a + b ;/ 数组相加 ./ 排序函数原型void Sor

14、t (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (con

15、st Array & other) ;封装数组和数组类类是数据和操作数据的函数的封装class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array & other) ; void main() Array a(10) , b(10) ;/ 声明对象 . a . Sort() ; b . Sort() ;/ 调用排序方法 a = a + b

16、 ;/ 数组相加 ./ 排序函数原型void Sort (int , int ) ; / 数组相加函数原型void Add ( int , int , int ) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .a . Sort() ; b . Sort() ; / 调用排序方法a = a + b ; / 数组相加封装数组和数组类对象使用自己的方法对数据操作几点如何调整通信构造class 钟 private : 钟的构造; public : 读取时间值 ; 调整

17、时间值 ;对象通过类接口与外部通信通信/例一个类的例子#includeclass 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;void main() Tdate a ; a.Set

18、 (10, 16, 2003) ; a.Print() ;3.2.1 定义类和对象/ 例一个类的例子#includeclass 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;void

19、 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 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;3.2.1 定义类和

20、对象/ 例一个类的例子#includeclass 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;void main() Tdate a ; a.Set (10, 16, 2003)

21、; 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 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;关键字定义一个类标识符类名 class ,struct,union 都可以定义一个类: class

22、缺省说明时,其成员被认为是私有的 struct若不特别指出,其所有成员都是公有的 union其所有成员都是公有的,且不能更改3.2.1 定义类和对象/ 例一个类的例子#includeclass 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:

23、int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;aTdate 类型的一个对象(实例)3.2.1 定义类和对象/ 例一个类的例子#includeclass 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()

24、 cout year . month . day endl ; private: int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;类由成员构成: 成员数据描述对象的属性成员函数描述对象的方法3.2.1 定义类和对象/ 例一个类的例子#includeclass Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%

25、4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;int month; int day; int year;数据成员3.2.1 定义类和对象/ 例一个类的例子#includeclass Tdate public: void Set(int m, int d, int y ) month =

26、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;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; void Set(int m, int d, int y ) month = m ; day = d ; year = y ;

27、int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; 类中定义成员函数内联函数处理3.2.1 定义类和对象/ 例一个类的例子#includeclass 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 )|(

28、year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; void Set(int m, int d, int y ) ; int IsLeapYear() ; void Print() ;在类外定义成员函数 void Tdate : Set(int m, int d, int y ) month = m ; day = d ; year = y

29、; int Tdate : IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Tdate : Print() cout year.month.dayendl ; 3.2.1 定义类和对象/ 例一个类的例子#includeclass 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 )|( y

30、ear%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;成员的性质由关键字public、protected、private 决定public 公有 公有段的成员是提供给外部的接口protected 保护 保护段成员在该类和它的派生类中可见private 私有 私有段成员仅在类中可见各段中既可以包含数据成员,也可以包含成员函数3.2.1 定义类和对象/

31、 例一个类的例子#includeclass 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;void main() Tdate a ; a.Set (10, 16, 2003) ; a

32、.Print() ;类说明的一般形式为: class 类名 public:公有段数据成员和成员函数 ; protected: 保护段数据成员和成员函数 ; private:私有数据成员和成员函数 ; ; 3.2.1 定义类和对象注:1. 允许已定义类名出现在类的说明中例: class X ; class Y X dataMember ; ;/ 声明一个类类型数据成员3.2.1 定义类和对象注:1. 允许已定义类名出现在类的说明中例: class X X dataMember ; ;/ 错误错误无穷递归结构3.2.1 定义类和对象注:1. 允许已定义类名出现在类的说明中2. 类可以无名,用于直接

33、声明对象例: class mydate ;/ 直接声明一个对象3.2.1 定义类和对象注:1. 允许已定义类名出现在类的说明中2. 类可以无名,用于直接声明对象例:#includeclass empty ;void main() empty e1 , e2 ; cout &e1 = &e1 endl ; cout &e2 = &e2 endl ; 3. 类是一个程序包。可以只有数据成员或只有成员函数,或者为空。 空类的对象大小不为零,空类对象具有地址一个空类3.2.1 定义类和对象注:1. 允许已定义类名出现在类的说明中2. 类可以无名,用于直接声明对象例:#includeclass empt

34、y ;void main() empty e1 , e2 ; cout &e1 = &e1 endl ; cout &e2 = &e2 endl ; 3. 类是一个程序包。可以只有数据成员或只有成员函数,或者为空。 空类的对象大小不为零,空类对象具有地址两个空类对象3.2.1 定义类和对象注:1. 允许已定义类名出现在类的说明中例:#includeclass empty ;void main() empty e1 , e2 ; cout &e1 = &e1 endl ; cout &e2 = &e2 ” 运算符访问对象成员 /访问对象的公有成员 #includeclass Tclass pub

35、lic: int x,y ; void print() cout x “,” ” 运算符访问对象成员 /访问对象的公有成员 #includeclass Tclass public: int x,y ; void print() cout x “,” ” 运算符访问对象成员 /访问对象的公有成员 #includeclass Tclass public: int x,y ; void print() cout x “,” y ; ; ;void main() Tclass test ; test.x = 100 ; test.y = 200 ; test.print() ;调用公有成员函数 3.2

36、.2 访问对象成员/ 用指针访问对象成员 #includeclass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void 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(&test) ;3.2.2 访问对象成员/ 用指针访问对象成员 #includeclass Tclass pub

37、lic : int x, y ; void print() cout x , y x + ptf-y ) ; void 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(&test) ;3.2.2 访问对象成员建立动态对象 / 用指针访问对象成员 #includeclass Tclass public : int x, y ; void print() cout x ,

38、 y x + ptf-y ) ; void 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(&test) ;3.2.2 访问对象成员用指针访问对象成员 / 用指针访问对象成员 #includeclass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void main() Tclass

39、 test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= add(&test) ;3.2.2 访问对象成员具有类指针参数的函数 / 用指针访问对象成员 #includeclass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void main() Tclass test, * pt = new(Tclass) ; pt-x =

40、 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= add(&test) ;3.2.2 访问对象成员向函数传递对象地址 #includeclass Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 1

41、5 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY

42、 ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void m

43、ain() 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.y3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b

44、; void printXY() cout x , y endl ; ; ;void 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向哪个对象的数据成员赋值? 3.2.3 this指针#include class Simple int

45、 x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y x = a ; this-y = b ; obj1 . setXY ( 10, 15, &obj1 ) ;成员函数隐含定义 this 指针接受调用对象的地址3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y x = a ; this-

46、y = b ; 3.2.3 this指针#include 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 ; 3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Sim

47、ple 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通过调用函数的对象this 指针获取对象地址3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x

48、 = a ; y = b ; void printXY() cout x , y endl ; ; ;void 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.y3.2.3 this指针#include class Simple

49、int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void 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

50、.yobj3.xobj3.y3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 )

51、 ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y在 obj1 上操作 10 , 15 20 , 25 30 , 353.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 .

52、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 , 353.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ;

53、;void 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 上操作3.2.3 this指针#include class Simple int x, y ; publi

54、c : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void 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.y2025obj2.xobj2.yobj3.xobj3

55、.y 10 , 15 20 , 25 30 , 35在 obj2 上操作3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; o

56、bj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.y2025obj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 353.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10

57、, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.y2025obj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 35在 obj2 上操作3.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void

58、printXY() cout x , y endl ; ; ;void 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.y2025obj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 35在 obj2 上操作3.2.3 this指针#inc

59、lude class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1

60、.xobj1.y2025obj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 353.2.3 this指针#include class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;void main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 )

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论