📄 chap20.lst
字号:
listing 1
#include <iostream>
using namespace std;
int main()
{
int i;
char str[80];
cout << "I like C++ Builder.\n"; // this is a single-line comment
/* you can still use C-style comments, too */
// input a number using >>
cout << "Enter a number: ";
cin >> i;
// now, output a number using <<
cout << "Your number is " << i << "\n";
// read a string
cout << "Enter a string: ";
cin >> str;
// print it
cout << str;
return 0;
}
listing 2
#include <stdio.h>
listing 4
// This creates the class queue.
class queue {
int q[100];
int sloc, rloc;
public:
void init();
void qput(int i);
int qget();
};
listing 5
#include <iostream>
using namespace std;
// This creates the class queue.
class queue {
int q[100];
int sloc, rloc;
public:
void init();
void qput(int i);
int qget();
};
void queue::init()
{
rloc = sloc = 0;
}
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, b; // create two queue objects
// now, access the queues through their member functions
a.init();
b.init();
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 6
#include <iostream>
using namespace std;
// sqr_it is overloaded three ways
int sqr_it(int i);
double sqr_it(double d);
long sqr_it(long l);
int main()
{
cout << sqr_it(10) << "\n";
cout << sqr_it(11.0) << "\n";
cout << sqr_it(9L) << "\n";
return 0;
}
// Define sqr_it for ints.
int sqr_it(int i)
{
cout << "Inside the sqr_it() function that uses ";
cout << "an integer argument.\n";
return i*i;
}
// Overload sqr_it for doubles.
double sqr_it(double d)
{
cout << "Inside the sqr_it() function that uses ";
cout << "a double argument.\n";
return d*d;
}
// Overload sqr_it again, this time for longs.
long sqr_it(long l)
{
cout << "Inside the sqr_it() function that uses ";
cout << "a long argument.\n";
return l*l;
}
listing 7
#include <iostream>
using namespace std;
void prompt(char *str, int *i);
void prompt(char *str, double *d);
void prompt(char *str, long *l);
int main()
{
int i;
double d;
long l;
prompt("Enter an integer: ", &i);
prompt("Enter a double: ", &d);
prompt("Enter a long: ", &l);
cout << i << " " << d << " " << l;
return 0;
}
// Prompt for an int.
void prompt(char *str, int *i)
{
cout << str;
cin >> *i;
}
// Prompt for a double.
void prompt(char *str, double *d)
{
cout << str;
cin >> *d;
}
// Prompt for a long.
void prompt(char *str, long *l)
{
cout << str;
cin >> *l;
}
listing 8
class road_vehicle {
int wheels;
int passengers;
public:
void set_wheels(int num);
int get_wheels();
void set_pass(int num);
int get_pass();
};
listing 9
class truck : public road_vehicle {
int cargo;
public:
void set_cargo(int size);
int get_cargo();
void show();
};
listing 10
#include <iostream>
using namespace std;
class road_vehicle {
int wheels;
int passengers;
public:
void set_wheels(int num);
int get_wheels();
void set_pass(int num);
int get_pass();
};
// Extend road_vehicle for trucks.
class truck : public road_vehicle {
int cargo;
public:
void set_cargo(int size);
int get_cargo();
void show();
};
enum type {car, van, wagon};
// Extend road_vehicle for cars.
class automobile : public road_vehicle {
enum type car_type;
public:
void set_type(enum type t);
enum type get_type();
void show();
};
void road_vehicle::set_wheels(int num)
{
wheels = num;
}
int road_vehicle::get_wheels()
{
return wheels;
}
void road_vehicle::set_pass(int num)
{
passengers = num;
}
int road_vehicle::get_pass()
{
return passengers;
}
void truck::set_cargo(int num)
{
cargo = num;
}
int truck::get_cargo()
{
return cargo;
}
void truck::show()
{
cout << "Wheels: " << get_wheels() << "\n";
cout << "Passengers: " << get_pass() << "\n";
cout << "Cargo capacity in cubic feet: " << cargo << "\n";
}
void automobile::set_type(enum type t)
{
car_type = t;
}
enum type automobile::get_type()
{
return car_type;
}
void automobile::show()
{
cout << "Wheels: " << get_wheels() << "\n";
cout << "Passengers: " << get_pass() << "\n";
cout << "Type: ";
switch(get_type()) {
case van: cout << "Van\n";
break;
case car: cout << "Car\n";
break;
case wagon: cout << "Wagon\n";
}
}
int main()
{
truck t1, t2;
automobile c;
t1.set_wheels(18);
t1.set_pass(2);
t1.set_cargo(3200);
t2.set_wheels(6);
t2.set_pass(3);
t2.set_cargo(1200);
t1.show();
t2.show();
c.set_wheels(4);
c.set_pass(6);
c.set_type(van);
c.show();
return 0;
}
listing 11
// This creates the class queue.
class queue {
int q[100];
int sloc, rloc;
public:
queue(); // constructor
~queue(); // destructor
void qput(int i);
int qget();
};
// This is the constructor function.
queue::queue()
{
sloc = rloc = 0;
cout << "Queue initialized.\n";
}
// This is the destructor function.
queue::~queue()
{
cout << "Queue destroyed.\n";
}
listing 12
#include <iostream>
using namespace std;
// This creates the class queue.
class queue {
int q[100];
int sloc, rloc;
public:
queue(); // constructor
~queue(); // destructor
void qput(int i);
int qget();
};
// This is the constructor function.
queue::queue()
{
sloc = rloc = 0;
cout << "Queue initialized.\n";
}
// This is the destructor function.
queue::~queue()
{
cout << "Queue 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, b; // 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -