📄 chap11.lst
字号:
listing 1
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "This is output.\n"; // this is a single line comment
/* you can still use C style comments */
// input a number using >>
cout << "Enter a number: ";
cin >> i;
// now, output a number using <<
cout << i << " squared is " << i*i << "\n";
return 0;
}
listing 2
using namespace std;
listing 3
int main()
listing 4
int main(void)
listing 5
cout << "This is output.\n"; // this is a single line comment
listing 6
cout << "This is output.\n";
listing 7
cin >> i;
listing 8
cout << i << "squared is " << i*i << "\n";
listing 9
return 0;
listing 10
#include <iostream>
using namespace std;
int main()
{
float f;
char str[80];
double d;
cout << "Enter two floating point numbers: ";
cin >> f >> d;
cout << "Enter a string: ";
cin >> str;
cout << f << " " << d << " " << str;
return 0;
}
listing 11
cout << "A\tB\tC";
listing 12
/* Incorrect in C. OK in C++. */
int f()
{
int i;
i = 10;
int j; /* won't compile as a C program */
j = i*2;
return j;
}
listing 13
#include <iostream>
using namespace std;
int main()
{
float f;
double d;
cout << "Enter two floating point numbers: ";
cin >> f >> d;
cout << "Enter a string: ";
char str[80]; // str declared here, just before 1st use
cin >> str;
cout << f << " " << d << " " << str;
return 0;
}
listing 14
func(int i)
{
return i*i;
}
listing 15
int func(int i)
{
return i*i;
}
listing 16
/*
An old-style C++ program.
*/
#include <iostream.h>
int main()
{
return 0;
}
listing 17
/*
A modern-style C++ program that uses
the new-style headers and a namespace.
*/
#include <iostream>
using namespace std;
int main()
{
return 0;
}
listing 18
#include <stdio.h>
listing 19
#include <iostream.h>
listing 20
using namespace std;
listing 21
#include <iostream>
using namespace std;
listing 22
#include <iostream.h>
listing 23
#define SIZE 100
// This creates the class stack.
class stack {
int stck[SIZE];
int tos;
public:
void init();
void push(int i);
int pop();
};
listing 24
stack mystack;
listing 25
void stack::push(int i)
{
if(tos==SIZE) {
cout << "Stack is full.\n";
return;
}
stck[tos] = i;
tos++;
}
listing 26
stack stack1, stack2;
stack1.init();
listing 27
#include <iostream>
using namespace std;
#define SIZE 100
// This creates the class stack.
class stack {
int stck[SIZE];
int tos;
public:
void init();
void push(int i);
int pop();
};
void stack::init()
{
tos = 0;
}
void stack::push(int i)
{
if(tos==SIZE) {
cout << "Stack is full.\n";
return;
}
stck[tos] = i;
tos++;
}
int stack::pop()
{
if(tos==0) {
cout << "Stack underflow.\n";
return 0;
}
tos--;
return stck[tos];
}
int main()
{
stack stack1, stack2; // create two stack objects
stack1.init();
stack2.init();
stack1.push(1);
stack2.push(2);
stack1.push(3);
stack2.push(4);
cout << stack1.pop() << " ";
cout << stack1.pop() << " ";
cout << stack2.pop() << " ";
cout << stack2.pop() << "\n";
return 0;
}
listing 28
stack1.tos = 0; // Error, tos is private.
listing 29
#include <iostream>
using namespace std;
// abs is overloaded three ways
int abs(int i);
double abs(double d);
long abs(long l);
int main()
{
cout << abs(-10) << "\n";
cout << abs(-11.0) << "\n";
cout << abs(-9L) << "\n";
return 0;
}
int abs(int i)
{
cout << "Using integer abs()\n";
return i<0 ? -i : i;
}
double abs(double d)
{
cout << "Using double abs()\n";
return d<0.0 ? -d : d;
}
long abs(long l)
{
cout << "Using long abs()\n";
return l<0 ? -l : l;
}
listing 30
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
void stradd(char *s1, char *s2);
void stradd(char *s1, int i);
int main()
{
char str[80];
strcpy(str, "Hello ");
stradd(str, "there");
cout << str << "\n";
stradd(str, 100);
cout << str << "\n";
return 0;
}
// concatenate two strings
void stradd(char *s1, char *s2)
{
strcat(s1, s2);
}
// concatenate a string with a "stringized" integer
void stradd(char *s1, int i)
{
char temp[80];
sprintf(temp, "%d", i);
strcat(s1, temp);
}
listing 31
class building {
int rooms;
int floors;
int area;
public:
void set_rooms(int num);
int get_rooms();
void set_floors(int num);
int get_floors();
void set_area(int num);
int get_area();
};
listing 32
// house is derived from building
class house : public building {
int bedrooms;
int baths;
public:
void set_bedrooms(int num);
int get_bedrooms();
void set_baths(int num);
int get_baths();
};
listing 33
#include <iostream>
using namespace std;
class building {
int rooms;
int floors;
int area;
public:
void set_rooms(int num);
int get_rooms();
void set_floors(int num);
int get_floors();
void set_area(int num);
int get_area();
};
// house is derived from building
class house : public building {
int bedrooms;
int baths;
public:
void set_bedrooms(int num);
int get_bedrooms();
void set_baths(int num);
int get_baths();
};
// school is also derived from building
class school : public building {
int classrooms;
int offices;
public:
void set_classrooms(int num);
int get_classrooms();
void set_offices(int num);
int get_offices();
};
void building::set_rooms(int num)
{
rooms = num;
}
void building::set_floors(int num)
{
floors = num;
}
void building::set_area(int num)
{
area = num;
}
int building::get_rooms()
{
return rooms;
}
int building::get_floors()
{
return floors;
}
int building::get_area()
{
return area;
}
void house::set_bedrooms(int num)
{
bedrooms = num;
}
void house::set_baths(int num)
{
baths = num;
}
int house::get_bedrooms()
{
return bedrooms;
}
int house::get_baths()
{
return baths;
}
void school::set_classrooms(int num)
{
classrooms = num;
}
void school::set_offices(int num)
{
offices = num;
}
int school::get_classrooms()
{
return classrooms;
}
int school::get_offices()
{
return offices;
}
int main()
{
house h;
school s;
h.set_rooms(12);
h.set_floors(3);
h.set_area(4500);
h.set_bedrooms(5);
h.set_baths(3);
cout << "house has " << h.get_bedrooms();
cout << " bedrooms\n";
s.set_rooms(200);
s.set_classrooms(180);
s.set_offices(5);
s.set_area(25000);
cout << "school has " << s.get_classrooms();
cout << " classrooms\n";
cout << "Its area is " << s.get_area();
return 0;
}
listing 34
// This creates the class stack.
class stack {
int stck[SIZE];
int tos;
public:
stack(); // constructor
void push(int i);
int pop();
};
listing 35
// stack抯 constructor function
stack::stack()
{
tos = 0;
cout << "Stack Initialized\n";
}
listing 36
// This creates the class stack.
class stack {
int stck[SIZE];
int tos;
public:
stack(); // constructor
~stack(); // destructor
void push(int i);
int pop();
};
// stack抯 constructor function
stack::stack()
{
tos = 0;
cout << "Stack Initialized\n";
}
// stack抯 destructor function
stack::~stack()
{
cout << "Stack Destroyed\n";
}
listing 37
// Using a constructor and destructor.
#include <iostream>
using namespace std;
#define SIZE 100
// This creates the class stack.
class stack {
int stck[SIZE];
int tos;
public:
stack(); // constructor
~stack(); // destructor
void push(int i);
int pop();
};
// stack抯 constructor function
stack::stack()
{
tos = 0;
cout << "Stack Initialized\n";
}
// stack抯 destructor function
stack::~stack()
{
cout << "Stack Destroyed\n";
}
void stack::push(int i)
{
if(tos==SIZE) {
cout << "Stack is full.\n";
return;
}
stck[tos] = i;
tos++;
}
int stack::pop()
{
if(tos==0) {
cout << "Stack underflow.\n";
return 0;
}
tos--;
return stck[tos];
}
int main()
{
stack a, b; // create two stack objects
a.push(1);
b.push(2);
a.push(3);
b.push(4);
cout << a.pop() << " ";
cout << a.pop() << " ";
cout << b.pop() << " ";
cout << b.pop() << "\n";
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -