⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap21.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 1
// This creates the class queue. 
class queue { 
  int q[100]; 
  int sloc, rloc; 
  int who; // holds the queue's ID number 
public: 
  queue(int id);  // parameterized constructor 
  ~queue(); // destructor 
  void qput(int i); 
  int qget(); 
};

listing 2
// This is the constructor function. 
queue::queue(int id) 
{ 
  sloc = rloc = 0; 
  who = id; 
  cout << "Queue " << who << " initialized\n"; 
}

listing 3
#include <iostream> 
using namespace std; 
 
// This creates the class queue. 
class queue { 
  int q[100]; 
  int sloc, rloc; 
  int who; // holds the queue's ID number 
public: 
  queue(int id);  // parameterized constructor 
  ~queue(); // destructor 
  void qput(int i); 
  int qget(); 
}; 
 
// This is the constructor function. 
queue::queue(int id) 
{ 
  sloc = rloc = 0; 
  who = id; 
  cout << "Queue " << who << " initialized.\n"; 
} 
 
// This is the destructor function. 
queue::~queue() 
{ 
  cout << "Queue " << who << " destroyed.\n"; 
} 
 
void queue::qput(int i) 
{ 
  if(sloc==99) { 
    cout << "Queue is full.\n"; 
    return; 
  } 
  sloc++; 
  q[sloc] = i; 
} 
 
int queue::qget() 
{ 
  if(rloc == sloc) { 
    cout << "Queue underflow.\n"; 
    return 0; 
  } 
  rloc++; 
  return q[rloc]; 
} 
 
int main() 
{ 
  queue a(1), b(2);  // create two queue objects 
 
  a.qput(10); 
  b.qput(19); 
 
  a.qput(20); 
  b.qput(1); 
 
  cout << a.qget() << " "; 
  cout << a.qget() << " "; 
  cout << b.qget() << " "; 
  cout << b.qget() << "\n"; 
 
  return 0; 
}

listing 4
#include <iostream> 
using namespace std; 
 
class widget { 
  int i; 
  int j; 
public: 
  widget(int a, int b); 
  void put_widget(); 
} ; 
 
widget::widget(int a, int b) 
{ 
  i = a; 
  j = b; 
} 
 
void widget::put_widget() 
{ 
  cout << i << " " << j << "\n"; 
} 
 
int main() 
{ 
  widget x(10, 20), y(0, 0); 
 
  x.put_widget(); 
  y.put_widget(); 
 
  return 0; 
}

listing 5
#include <iostream> 
using namespace std; 
 
class X { 
  int a; 
public: 
  X(int j) { a = j; } 
  int geta() { return a; } 
}; 
 
int main() 
{ 
  X ob = 99; // passes 99 to j 
 
  cout << ob.geta(); // outputs 99 
 
  return 0; 
}

listing 6
X ob = X(99);

listing 7
class cl { 
  // ... 
public: 
  friend void frd(); 
  // ... 
};

listing 8
class line; 
 
class box { 
  int color; // color of box 
  int upx, upy; // upper left corner 
  int lowx, lowy; // lower right corner 
public: 
  friend int same_color(line l, box b); 
  void set_color(int c); 
  void define_box(int x1, int y1, int x2, int y2); 
  void show_box(); 
} ; 
 
class line { 
  int color; // color of line 
  int startx, starty; // coordinates 
  int len; // length 
public: 
  friend int same_color(line l, box b); 
  void set_color(int c); 
  void define_line(int x, int y, int l); 
  void show_line(); 
} ;

listing 9
// Return true if line and box have same color. 
int same_color(line l, box b) 
{ 
  if(l.color==b.color) return 1; 
  return 0; 
}

listing 10
#include <iostream> 
#include <conio.h> 
using namespace std; 
 
class line; 
 
class box { 
  int color; // color of box 
  int upx, upy; // upper left corner 
  int lowx, lowy; // lower right corner 
 public: 
  friend int same_color(line l, box b); 
  void set_color(int c); 
  void define_box(int x1, int y1, int x2, int y2); 
  void show_box(); 
} ; 
 
class line { 
  int color; // color of line 
  int startx, starty; // coordinates 
  int len; // length 
public: 
  friend int same_color(line l, box b); 
  void set_color(int c); 
  void define_line(int x, int y, int l); 
  void show_line(); 
} ; 
 
// Return true if line and box have same color. 
int same_color(line l, box b) 
{ 
  if(l.color==b.color) return 1; 
  return 0; 
} 
 
void box::set_color(int c) 
{ 
  color = c; 
} 
 
void line::set_color(int c) 
{ 
  color = c; 
} 
 
void box::define_box(int x1, int y1, int x2, int y2) 
{ 
  upx = x1; 
  upy = y1; 
  lowx = x2; 
  lowy = y2; 
} 
 
void box::show_box() 
{ 
  int i; 
 
  textcolor(color); 
 
  gotoxy(upx, upy); 
  for(i=upx; i<=lowx; i++) cprintf("-"); 
 
  gotoxy(upx, lowy-1); 
  for(i=upx; i<=lowx; i++) cprintf("-"); 
 
  gotoxy(upx, upy); 
  for(i=upy; i<=lowy; i++) { 
    cprintf("|"); 
    gotoxy(upx, i); 
  } 
 
  gotoxy(lowx, upy); 
  for(i=upy; i<=lowy; i++) { 
    cprintf("|"); 
    gotoxy(lowx, i); 
  } 
} 
 
void line::define_line(int x, int y, int l) 
{ 
  startx = x; 
  starty = y; 
  len = l; 
} 
 
void line::show_line() 
{ 
  int i; 
 
  textcolor(color); 
 
  gotoxy(startx, starty); 
 
  for(i=0; i<len; i++) cprintf("-"); 
} 
 
int main() 
{ 
  box b; 
  line l; 
 
  b.define_box(10, 10, 15, 15); 
  b.set_color(3); 
  b.show_box(); 
 
  l.define_line(2, 2, 10); 
  l.set_color(2); 
  l.show_line(); 
 
  if(!same_color(l, b)) cout << "Not the same.\n"; 
  cout << "\nPress a key."; 
  getch(); 
 
  // now, make line and box the same color 
  l.define_line(2, 2, 10); 
  l.set_color(3); 
  l.show_line(); 
 
  if(same_color(l, b)) cout << "Are the same color.\n"; 
 
  return 0; 
}

listing 11
//Output a string at specified X,Y location. 
void xyout(char *str, int x = 0, int y = 0) 
{ 
  if(!x) x = wherex(); 
  if(!y) y = wherey(); 
  gotoxy(x, y); 
  cout << str; 
}

listing 12
#include <iostream> 
#include <conio.h> 
using namespace std; 
 
void xyout(char *str, int x=0, int y=0) 
{ 
  if(!x) x = wherex(); 
  if(!y) y = wherey(); 
  gotoxy(x, y); 
  cout << str; 
} 
 
int main() 
{ 
  xyout("hello", 10, 10); 
  xyout(" there"); 
  xyout("I like C++", 40);  // this is still on line 10 
 
  xyout("This is on line 11.\n", 1, 11); 
  xyout("This follows on line 12.\n"); 
  xyout("This follows on line 13."); 
 
  return 0; 
}

listing 13
#include <iostream> 
#include <conio.h> 
using namespace std; 
 
void xyout(char *str, int x = 0, int y = 0); 
 
int main() 
{ 
  xyout("hello", 10, 10); 
  xyout(" there"); 
  xyout("I like C++", 40);  // this is still on line 10 
 
  xyout("This is on line 11.\n", 1, 11); 
  xyout("This follows on line 12.\n"); 
  xyout("This follows on line 13."); 
 
  return 0; 
} 
 
/* Since x and y's defaults have already been specified 
   in xyout()'s prototype, they cannot 
   be repeated here. 
*/ 
void xyout(char *str, int x, int y) 
{ 
  if(!x) x = wherex(); 
  if(!y) y = wherey(); 
  gotoxy(x, y); 
  cout << str; 
}

listing 14
/* This is the constructor function that uses 
   a default value. */ 
queue::queue(int id=0) 
{ 
  sloc = rloc = 0; 
  who = id; 
  cout << "Queue " << who << " initialized.\n"; 
}

listing 15
#include <iostream> 
using namespace std; 
 
struct cl { 
  int get_i(); // these are public 
  void put_i(int j); // by default 
private: 
  int i; 
} ; 
 
int cl::get_i() 
{ 
  return i; 
} 
 
void cl::put_i(int j) 
{ 
  i = j; 
} 
 
int main() 
{ 
  cl s; 
 
  s.put_i(10); 
  cout << s.get_i(); 
 
  return 0; 
}

listing 16
#include <iostream> 
using namespace std; 
 
class cl { 
  int i; // private by default 
public: 
  int get_i(); 
  void put_i(int j); 
} ; 
 
int cl::get_i() 
{ 
  return i; 
} 
 
void cl::put_i(int j) 
{ 
  i = j; 
} 
 
int main() 
{ 
  cl s; 
 
  s.put_i(10); 
  cout << s.get_i(); 
 
  return 0; 
}

listing 17
#include <iostream> 
using namespace std; 
 
union u_type { 
  u_type(short int a);  // public by default 
  void showchars(); 
  short int i; 
  char ch[2]; 
}; 
 
// constructor 
u_type::u_type(short int a) 
{ 
  i = a; 
} 
 
// Show the characters that comprise an int. 
void u_type::showchars() 
{ 
  cout << ch[0] << " "; 
  cout << ch[1] << "\n"; 
} 
 
int main() 
{ 
  u_type u(1000); 
 
  u.showchars(); 
 
  return 0; 
}

listing 18
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  // This declares an anonymous union. 
  union {  // no tag name 
    int i; 
    char ch[4]; 
  } ;  // no variables specified 
 
  /* Now reference i and ch without referencing 
     a union name or dot or arrow operators. 
  */ 
  i = 88; 
  cout << i << " " << ch[0]; 
 
  return 0; 
}

listing 20
#include <iostream> 
using namespace std; 
 
class cl { 
  int i;  
public: 
  // automatic inline functions 
  int get_i() { return i; } 
  void put_i(int j) { i = j; } 
} ; 
 
int main() 
{ 
  cl s; 
 
  s.put_i(10); 
  cout << s.get_i(); 
 
  return 0; 
}

listing 21
class cl { 
  int i;  
public: 
  // automatic inline functions 
  int get_i() 
  { 
    return i; 
  } 
 
  void put_i(int j) 
  { 
     i = j; 
  } 
} ;

listing 22
#include <iostream> 
using namespace std; 
 
class myclass { 
  int i; 
public: 
  myclass(int n); 
  ~myclass(); 
  void set_i(int n) { i=n; } 
  int get_i() { return i; } 
}; 
 
myclass::myclass(int n) 
{ 
  i = n; 
  cout << "Constructing " << i << "\n"; 
} 
 
myclass::~myclass() 
{ 
  cout << "Destroying " << i << "\n"; 
} 
 
void f(myclass ob); 
 
int main() 
{ 
  myclass o(1); 
 
  f(o); 
  cout << "This is i in main: "; 
  cout << o.get_i() << "\n"; 
 
  return 0; 
} 
 
void f(myclass ob) 
{ 
  ob.set_i(2); 
 
  cout << "This is local i: " << ob.get_i(); 
  cout << "\n"; 
}

listing 23
#include <iostream> 
using namespace std; 
 
class myclass { 
  int i; 
public: 
  void set_i(int n) { i=n; } 
  int get_i() { return i; } 
}; 
 
myclass f();  // return object of type myclass 
 
int main() 
{ 
  myclass o; 
 
  o = f(); 
  cout << o.get_i() << "\n"; 
 
  return 0; 
} 
 
myclass f() 
{ 
  myclass x; 
 
  x.set_i(1); 
  return x; 
}

listing 24
#include <iostream> 
using namespace std; 
 
class myclass { 
  int i; 
public: 
  void set_i(int n) { i=n; } 
  int get_i() { return i; } 
}; 
 
int main() 
{ 
  myclass ob1, ob2; 
 
  ob1.set_i(99); 
  ob2 = ob1; // assign data from ob1 to ob2 
 
  cout << "this is ob2's i: " << ob2.get_i(); 
 
  return 0; 
}

listing 25
// An example of arrays of objects. 
 
#include <iostream> 
using namespace std; 
 
enum resolution { r640x480, r800x600, r1024x768 }; 
enum coloroption { c16, c256, cHighColor, cTrueColor }; 
 
class display { 
  coloroption coption; // color option 
  resolution res; // resoltuion 
public: 
  void set_coloropt(coloroption opt) { coption = opt; } 
  coloroption get_coloropt() { return coption; } 
  void set_res(resolution r) { res = r; } 
  resolution get_res() { return res; } 
} ; 
 
char options[4][20] = { 
  "16 Colors", 
  "256 Colors", 
  "High Color (16 bit)", 
  "True Color (32 bit)" 
} ; 
 
char resvals[3][20] = { 
  "640 x 480", 
  "800 x 600", 
  "1024 x 768" 
} ; 
 
int main() 
{ 
  display monitors[3]; 
  register int i; 
 
  monitors[0].set_coloropt(c16); 
  monitors[0].set_res(r640x480); 
 
  monitors[1].set_coloropt(cTrueColor); 
  monitors[1].set_res(r640x480); 
 
  monitors[2].set_coloropt(c256); 
  monitors[2].set_res(r1024x768); 
 
  for(i=0; i<3; i++) { 
    cout << options[monitors[i].get_coloropt()] << " "; 
    cout << "with resolution of " << resvals[monitors[i].get_res()]; 
    cout << "\n"; 
  } 
 
  return 0; 
} 


listing 26
#include <iostream> 
using namespace std; 
 
class cl { 
  int i; 
public: 
  cl(int j) { i=j; }  // constructor 
  int get_i() { return i; } 
}; 
 
int main() 
{ 
  cl ob[3] = {1, 2, 3};  // initializers 
  int i; 
 
  for(i=0; i<3; i++) 
    cout << ob[i].get_i() << "\n"; 
 
  return 0; 
}

listing 27
#include <iostream> 
using namespace std; 
 
class cl { 
  int h; 
  int i; 
public: 
  cl(int j, int k) { h=j; i=k; } // constructor 
  int get_i() { return i; } 
  int get_h() { return h; } 
}; 
 
int main() 
{ 
  cl ob[3] = { 
    cl(1, 2), 
    cl(3, 4), 
    cl(5, 6) 
  };  // initializers 
 
  int i; 
 
  for(i=0; i<3; i++) { 
    cout << ob[i].get_h(); 
    cout << ", "; 
    cout << ob[i].get_i() << "\n"; 
  } 
 
  return 0; 
}

listing 28
class cl { 
  int i; 
public: 
  cl() { i=0; }  // called for non-initialized arrays 
  cl(int j) { i=j; }  // called for initialized arrays 
  int get_i() { return i; } 
};

listing 29
cl a1[3] = { 3, 5, 6 }; // initialized 
cl a2[34]; // uninitialized

listing 30
// A simple example using an object pointer. 
 
#include <iostream> 
using namespace std; 
 
class P_example { 
  int num; 
public: 
  void set_num(int val) { num = val; } 
  void show_num(); 
}; 
 
void P_example::show_num() 
{ 
  cout << num << "\n"; 
} 
 
int main() 
{ 
  P_example ob, *p; // declare an object and pointer to it 
 
  ob.set_num(1); // access ob directly 
  ob.show_num(); 
  p = &ob; // assign p the address of ob 
  p->show_num();  // access ob using pointer 
 
  return 0; 
}

listing 31
// Incrementing an object pointer 
#include <iostream> 
using namespace std; 
 
class P_example { 
  int num; 
public: 
  void set_num(int val) { num = val; } 
  void show_num(); 
}; 
 
void P_example::show_num() 
{ 
  cout << num << "\n"; 
} 
 
int main() 
{ 
  P_example ob[2], *p; 
 
  ob[0].set_num(10);  // access objects directly 
  ob[1].set_num(20); 
 
  p = &ob[0];  // obtain pointer to first element 
  p->show_num(); // show value of ob[0] using pointer 
 
  p++;  // advance to next object 
  p->show_num(); // show value of ob[1] using pointer 
 
  p--;  // retreat to previous object 
  p->show_num(); // again show value of ob[0] 
 
  return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -