float width float length float area set Data

  • Slides: 75
Download presentation

成员变量 float width; float length; float area; 属性 成员函数 set. Data( ) { ……}

成员变量 float width; float length; float area; 属性 成员函数 set. Data( ) { ……} calc. Area( ) { ……} get. Width( ) { ……} get. Length( ){ ……} get. Area( ) { ……} 方法

class Rectangle { float width; float length: float area; void float }; 默认情况下类的成员是私有 的,而结构体(struct)中的成

class Rectangle { float width; float length: float area; void float }; 默认情况下类的成员是私有 的,而结构体(struct)中的成 员是公有的。 set. Data(float, float); calc. Area( ); get. Width( ); get. Length( ); get. Area( );

class Rectangle { private: float width; float length: float area; public: void set. Data(float,

class Rectangle { private: float width; float length: float area; public: void set. Data(float, float); void calc. Area( ); float get. Width( ); float get. Length( ); float get. Area( ); };

class Rectangle { public: void set. Data(float, float); void calc. Area( ); float get.

class Rectangle { public: void set. Data(float, float); void calc. Area( ); float get. Width( ); float get. Length( ); float get. Area( ); private: float width; float length: float area; };

class Rectangle { private: float width; public: void set. Data(float, float); void calc. Area(

class Rectangle { private: float width; public: void set. Data(float, float); void calc. Area( ); float get. Width( ); float get. Length( ); float get. Area( ); private: float length: float area; };

惯例: class-name { private: declaration; //. . . more declarations may follow. . .

惯例: class-name { private: declaration; //. . . more declarations may follow. . . public: declaration; //. . . more declarations may follow. . . };

class Rectangle { private: float width; float length: float area; public: void set. Data(float,

class Rectangle { private: float width; float length: float area; public: void set. Data(float, float); void calc. Area( ); float get. Width( ); float get. Length( ); float get. Area( ); };

void { Rectangle: : set. Data(float w, float l ) width = w; length

void { Rectangle: : set. Data(float w, float l ) width = w; length = l; } float { Rectangle: : get. Width ( ) return width; }

3. 4 定义对象 • 定义对象称为类的实例化 (模具-铸件) • Example: Rectangle box; box. set. Data(10. 0,

3. 4 定义对象 • 定义对象称为类的实例化 (模具-铸件) • Example: Rectangle box; box. set. Data(10. 0, 12. 5); cout << Box. get. Width( ); Rectangle *box. Ptr; box. Ptr = &box; box. Ptr->set. Data(15, 12); 15

// Program 3 -1 class Rectangle { private: float width; float length; float area;

// Program 3 -1 class Rectangle { private: float width; float length; float area; public: void set. Data(float, float); void calc. Area( ); float get. Width( ); float get. Length( ); float get. Area( ); };

// 参数值传递给私有成员变量 void Rectangle: : set. Data( float w, float l ) { width

// 参数值传递给私有成员变量 void Rectangle: : set. Data( float w, float l ) { width = w; length = l; } // 计算面积 void Rectangle: : calc. Area( ) { area = width * length; }

// 返回私有成员变量width float Rectangle: : get. Width( ) { return width; } // 返回私有成员变量

// 返回私有成员变量width float Rectangle: : get. Width( ) { return width; } // 返回私有成员变量 length float Rectangle: : get. Length( ) { return length; } // 返回私有成员变量 area float Rectangle: : get. Area( ) { return area; }

void main( ) { Rectangle box; float wide, box. Long; cout << " 请输入长和宽

void main( ) { Rectangle box; float wide, box. Long; cout << " 请输入长和宽 ? "; cin >> wide>> box. Long; box. set. Data(wide, box. Long); box. calc. Area( ); cout << "矩形的数据: n"; cout << "宽: "<< box. get. Width( ) << endl; cout << "长: "<< box. get. Length( )<<endl; cout << "面积: "<< box. get. Area( )<<endl; } 3 -1. cpp

// Contents of Rectang. h #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { float width,

// Contents of Rectang. h #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { float width, length , area; public: void set. Data(float, float); void calc. Area( ); float get. Width( ); float get. Length( ); float get. Area( ); }; #endif 第一个文件

// Contents of rectang. cpp #include "Rectang. h" // 把参数传递给私有成员 void Rectangle: : set.

// Contents of rectang. cpp #include "Rectang. h" // 把参数传递给私有成员 void Rectangle: : set. Data(float w, float l) { width = w; length = l; } // 计算面积 void Rectangle: : calc. Area( ) { area = width * length; } 为什么需要 这行?

// 返回私有成员:width float Rectangle: : get. Width( ) { return width; } // 返回私有成员:

// 返回私有成员:width float Rectangle: : get. Width( ) { return width; } // 返回私有成员: length float Rectangle: : get. Length( ) { return length; } // 返回私有成员:area float Rectangle: : get. Area( ) { return area; }

//主程序 #include "Rectang. h" //不能省略这一行 void main( ) { Rectangle box; float wide, box.

//主程序 #include "Rectang. h" //不能省略这一行 void main( ) { Rectangle box; float wide, box. Long; cout << " 输入长和宽 ? "; cin >> wide>> box. Long; box. set. Data(wide, box. Long); box. calc. Area( ); cout << "长: "<< box. get. Length( )<<endl; cout << "宽: "<< box. get. Width( ) << endl; cout << "面积: "<< box. get. Area( )<<endl; } Rectang. h Rectang. cpp 3 -2. cpp

void { Rectangle: : set. Data(float w, float l) width = w; length =

void { Rectangle: : set. Data(float w, float l) width = w; length = l; calc. Area( ); } … 27

#ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle{ private: float width , length, area ; public:

#ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle{ private: float width , length, area ; public: void set. Data( float , float ) ; void calculate. Area( ) { area = width * length ; } // 内联函数 float get. Width( ) ; float get. Length( ) ; float get. Area( ) ; }; inline void Rectangle: : set. Data( float w, float l ) // 内联函数 { width = w ; length = l ; } #endif

class Invoice. Item // 例3 -6 { char *desc; int units; public: Invoice. Item(

class Invoice. Item // 例3 -6 { char *desc; int units; public: Invoice. Item( ) { desc = new char [51]; } void set. Info(char *dscr, int un) { strcpy(desc, dscr); units = un; } char *get. Desc( ) { return desc; } int get. Units( ) { return units; } };

void main( ) { Invoice. Item stock; stock. set. Info( "鼠标", 20); cout <<

void main( ) { Invoice. Item stock; stock. set. Info( "鼠标", 20); cout << "库存物品: " << stock. get. Desc( ) << endl; cout << "库存量: " << stock. get. Units( ) << endl; } 3 -6. cpp 指向对象的指针: Invoice. Item *ptr ; ptr = new Invoice. Item ; // 此时调用构造函数

class Invoice. Item { char *desc; int units; public: Invoice. Item( ) { desc

class Invoice. Item { char *desc; int units; public: Invoice. Item( ) { desc = new char[51]; cout<< "构造函数 n"; } ~Invoice. Item( ) { delete [ ]desc; cout<< "析构函数 n"; }

void set. Info(char *dscr, int un) { strcpy(desc, dscr); units = un; } char

void set. Info(char *dscr, int un) { strcpy(desc, dscr); units = un; } char *get. Desc( ) { return desc; } int get. Units( ) { return units; } }; void main( ) { Invoice. Item stock; stock. set. Info( "鼠标", 20); cout << stock. get. Desc( ) << endl; cout << stock. get. Units( ) << endl; } 3 -7. cpp

// Contents of sale 2. h class Sale { float tax. Rate , total;

// Contents of sale 2. h class Sale { float tax. Rate , total; public: Sale(float rate ) { tax. Rate = rate; } void calc. Sale( float cost) { total = cost +(cost * tax. Rate); float get. Total( ) { }; } return total; }

//Contents of main program void main( ) { Sale cashier( 0. 06 f )

//Contents of main program void main( ) { Sale cashier( 0. 06 f ) ; float amount ; // 6% 税率 cout << "请输入销售额: " ; cin >> amount ; cashier. calculate. Sale( amount ) ; cout << "销售总额是 RMB" ; cout << cashier. get. Total( ) << endl ; }

例 3 -9 构造函数可以有缺省参数: class Sale { float tax. Rate , total; public: Sale(

例 3 -9 构造函数可以有缺省参数: class Sale { float tax. Rate , total; public: Sale( float rate = 0. 05 f ) { tax. Rate = rate ; } void calc. Sale( float cost) { total = cost +(cost * tax. Rate); } float get. Total( ) { return total; } };

//Contents of main program void main( ) { Sale cashier 1 ; // 缺省形参值

//Contents of main program void main( ) { Sale cashier 1 ; // 缺省形参值 Sale cashier 2(0. 06 f ) ; // 指定形参值 float amount ; cout << "请输入销售额: " ; cin >> amount ; cashier 1. calculate. Sale( amount ) ; cashier 2. calculate. Sale( amount ) ; cout << cashier 1. get. Total( ) << endl ; cout << cashier 2. get. Total( ) << endl ; }

#include <iostream> #include <cstring> using namespace std; class Char. Range { char *err. Msg

#include <iostream> #include <cstring> using namespace std; class Char. Range { char *err. Msg ; // 出错信息 char input ; // 用户输入值 char lower ; // 有效字符的低界 char upper ; // 有效字符的高界 public: Char. Range( char , const char * ) ; char get. Char( ) ; };

Char. Range: : Char. Range( char low , char high , const char *str

Char. Range: : Char. Range( char low , char high , const char *str ) { lower = toupper( low ) ; upper = toupper( high ) ; err. Msg = new char [ strlen( str) + 1] ; strcpy( err. Msg , str ) ; }

char Char. Range: : get. Char( ) { cin. get(input) ; cin. ignore( )

char Char. Range: : get. Char( ) { cin. get(input) ; cin. ignore( ) ; input = toupper( input ) ; while( input < lower || input > upper ) { cout << err. Msg ; cin. get(input) ; cin. ignore( ) ; input = toupper( input ) ; } return input ; }

int main( ) { const char *Msg="仅接受 J~N字符"; Char. Range input( 'J' , 'N'

int main( ) { const char *Msg="仅接受 J~N字符"; Char. Range input( 'J' , 'N' , Msg ) ; while( input. get. Char( ) != 'N') ; return 0; } 3 -10. cpp

class Invoice. Item void main( ) { { Invoice. Item iteml( "ABC"); char *desc;

class Invoice. Item void main( ) { { Invoice. Item iteml( "ABC"); char *desc; Invoice. Item item 2; int units; } public: Invoice. Item( int size = 51) { desc = new char[size]; } Invoice. Item(char *d) { desc = new char[strlen(d)+1]; strcpy(desc, d); } ~Invoice. Item( ) { delete [ ]desc; } 3 -11. cpp // 其它方法略 };

class Invoice. Item { illegal char *desc; int units; public: Invoice. Item( ) {

class Invoice. Item { illegal char *desc; int units; public: Invoice. Item( ) { desc = new char[80]; } Invoice. Item(int size = 51) { desc = new char[size]; } ~Invoice. Item( ) { delete[ ] desc; } // 其他函数略 };

class Invoice. Item { char *desc ; int storage ; public: Invoice. Item( int

class Invoice. Item { char *desc ; int storage ; public: Invoice. Item( int size = 51) { desc = new char [ size] ; } Invoice. Item( char *d ) { /* 函数代码略*/ } Invoice. Item( char *d , int u) { /* 函数代码略*/ } ~Invoice. Item( ) // 析构函数 { delete[ ] desc ; } // 其他函数略 };

思考: 三个对象 的初始化 void main( ) { Invoice. Item Inventory[3] = { Invoice. Item(

思考: 三个对象 的初始化 void main( ) { Invoice. Item Inventory[3] = { Invoice. Item( "ABC", 10), Invoice. Item( "DEF") }; for( int i = 0 ; i < 3 ; i++ ) cout << Inventory[i]. get. Desc ( ) <<endl; } 3 -12. cpp

class Int. Array { int list[20]; bool is. Valid(int); public: Int. Array( ); bool

class Int. Array { int list[20]; bool is. Valid(int); public: Int. Array( ); bool set(int, int); bool get(int, int& ); }; // 构造函数。对list 中的每个元素初始化 Int. Array: : Int. Array( ) { for(int i = 0; i < 20; i++) list[i] = 0; } 3 -14. cpp

// 检验参数 element 是否为有效的下标 bool Int. Array: : is. Valid(int element) { bool status

// 检验参数 element 是否为有效的下标 bool Int. Array: : is. Valid(int element) { bool status = true; if(element < 0 || element > 19) { cout << "ERROR: "<< element; cout << "is an invalid subscript. n"; status = false; } return status; }

// set向指定的数组位置存储一个值。 bool Int. Array: : set(int element, int value) { bool status =

// set向指定的数组位置存储一个值。 bool Int. Array: : set(int element, int value) { bool status = false; if(is. Valid(element)) { list[element] = value; status = true; } return status; }

// get获得数组中指定位置的值. bool Int. Array: : get(int element, int &value) { bool status =

// get获得数组中指定位置的值. bool Int. Array: : get(int element, int &value) { bool status = false; if(is. Valid(element)) { value = list [element]; status = true; } return status; }

void main( ) { Int. Array numbers; int val , x ; // 将

void main( ) { Int. Array numbers; int val , x ; // 将 1 存储在数组中,同时显示 20个'*' for(x = 0 ; x < 20 ; x++ ) if( numbers. set(x, 1 ) ) cout << "* " ; cout << endl;

for(x = 0; x < 20; x++) if(numbers. get(x, val)) cout << val <<

for(x = 0; x < 20; x++) if(numbers. get(x, val)) cout << val << " " ; cout << endl; // Attempt to store a value outside the bounds. if(numbers. set(50, 3)) cout << "Element 50 successfully set. n"; } 63

class Int. Array { int list [20]; bool is. Valid(int); public: Int. Array( );

class Int. Array { int list [20]; bool is. Valid(int); public: Int. Array( ); bool set(int, int); bool get(int, int&); int linear. Search(int); int binary. Search(int); void bubble. Sort( ); void selection. Sort( ); };

// Int. Array 类的构造函数 Int. Array: : Int. Array( ) { for(int i =

// Int. Array 类的构造函数 Int. Array: : Int. Array( ) { for(int i = 0; i < 20; i++) list [i] = 0; } // is. Valid 函数检验参数是否为有效的下标 bool Int. Array: : is. Valid(int element) { if(element < 0 || element > 19) { cout << "ERROR: "<< element; cout << "is an invalid subscript. n"; return false; } else return true; }

// set 函数,向指定的数组位置存储一个值 bool Int. Array: : set(int element, int value) { if(is. Valid(element))

// set 函数,向指定的数组位置存储一个值 bool Int. Array: : set(int element, int value) { if(is. Valid(element)) { list[element] = value; return true; } else return false; } // get member function. bool Int. Array: : get(int element, int &value) { if (is. Valid(element)) { value = list[element]; return true; } else return false; }

// linear. Search线形查找函数. int Int. Array: : linear. Search(int value) { int status =

// linear. Search线形查找函数. int Int. Array: : linear. Search(int value) { int status = -1; } for (int count = 0; count < 20; count++) if (list [count] == value) { status = count; break; } return status;

// binary. Search member function. int Int. Array: : binary. Search(int value) { int

// binary. Search member function. int Int. Array: : binary. Search(int value) { int first = 0, last = 19, middle ; selection. Sort( ) ; // 首先对数组排序 while( first <= last ) { middle = (first + last) / 2 ; if( list[middle] == value ) return middle ; else if( list[middle] > value ) last = middle - 1 ; else first = middle + 1 ; } return -1 ; // 代表未找到指定的元素 }

// bubble. Sort member function. void Int. Array: : bubble. Sort( ) { int

// bubble. Sort member function. void Int. Array: : bubble. Sort( ) { int temp ; } for( int line = 0 ; line < 19 ; line++ ) for( int col = 0 ; col < 19 - line ; col++ ) if( list [ col ] > list [ col + 1 ] ) { temp = list [ col ] ; list [ col ] = list [ col + 1] ; list [ col + 1 ] = temp ; }

// selection. Sort member function. void Int. Array: : selection. Sort( ) { int

// selection. Sort member function. void Int. Array: : selection. Sort( ) { int start. Scan, min. Index, temp ; for( start. Scan = 0 ; start. Scan < 19 ; start. Scan++ ) { min. Index = start. Scan ; for( int i = start. Scan + 1 ; i < 20 ; i++ ) if( list [ i ] < list [ min. Index ] ) min. Index = i ; temp=list[min. Index] ; list[min. Index] = list[start. Scan] ; list[start. Scan] = temp ; } }

int main ( ) { Int. Array numbers; int val, x , search. Result;

int main ( ) { Int. Array numbers; int val, x , search. Result; for ( x = 0; x < 20; x++) if (! numbers. set(x, rand( ))) cout << "存储数据出错! n" ;

cout << "n下面是随机产生的 20 个数: n" ; for( x = 0 ; x <

cout << "n下面是随机产生的 20 个数: n" ; for( x = 0 ; x < 20 ; x++ ) { if(numbers. get(x, val ) ) cout <<setw( 10 )<<val ; if((x+1) % 5 == 0 ) cout << endl ; } cout << endl ; cout << "按 Enter 键继续. . . "<<endl ; cin. get( ) ;

numbers. selection. Sort( ) ; cout << "下面是排序后的 20 个数: n" ; // 显示排序后的

numbers. selection. Sort( ) ; cout << "下面是排序后的 20 个数: n" ; // 显示排序后的 20 个数 for( x = 0 ; x < 20 ; x++ ) { if(numbers. get(x, val ) ) cout <<setw( 10 )<<val ; if((x+1) % 5 == 0 ) cout << endl ; } cout << endl ;

cout << "输入一个数,然后进行查找: " ; cin >> val ; cout << "正在查找,请稍侯. . .

cout << "输入一个数,然后进行查找: " ; cin >> val ; cout << "正在查找,请稍侯. . . n" ; search. Result = numbers. binary. Search( val ) ; if( search. Result == -1 ) cout << "没找到!n" ; else { cout << "在排序后,它的下标位置是:" ; cout << search. Result << endl ; } return 0; } 3 -15. cpp