📄 chap23.lst
字号:
listing 1
class X {
int i;
int j;
public:
void get_ij();
void put_ij();
} ;
class Y : public X {
int k;
public:
int get_k();
void make_k();
} ;
listing 2
class X {
protected:
int i;
int j;
public:
void get_ij();
void put_ij();
} ;
class Y : public X {
int k;
public:
int get_k();
void make_k();
} ;
listing 3
class my_class {
protected:
int i;
int j;
public:
void f1();
void f2();
protected:
int a;
public:
int b;
} ;
listing 4
#include <iostream>
using namespace std;
class X {
protected:
int i;
int j;
public:
void get_ij() {
cout << "Enter two numbers: ";
cin >> i >> j;
}
void put_ij() { cout << i << " " << j << "\n"; }
} ;
// In Y, i and j of X become protected members.
class Y : public X {
int k;
public:
int get_k() { return k; }
void make_k() { k = i*j; }
} ;
/* Z has access to i and j of X, but not to
k of Y, since it is private. */
class Z : public Y {
public:
void f();
} ;
// i and j are accessible here
void Z::f()
{
i = 2; // ok
j = 3; // ok
}
int main()
{
Y var;
Z var2;
var.get_ij();
var.put_ij();
var.make_k();
cout << var.get_k();
cout << "\n";
var2.f();
var2.put_ij();
return 0;
}
listing 5
#include <iostream>
using namespace std;
class X {
protected:
int i;
int j;
public:
void get_ij() {
cout << "Enter two numbers: ";
cin >> i >> j;
}
void put_ij() { cout << i << " " << j << "\n"; }
} ;
// Now, i and j are converted to private members of Y.
class Y : private X {
int k;
public:
int get_k() { return k; }
void make_k() { k = i*j; }
} ;
/* Because i and j are private in Y, they
cannot be inherited by Z. */
class Z : public Y {
public:
void f();
} ;
// This function no longer works.
void Z::f()
{
// i = 2; i and j are no longer accessible
// j = 3;
}
int main()
{
Y var;
Z var2;
// var.get_ij(); no longer accessible
// var.put_ij(); no longer accessible
var.make_k();
cout << var.get_k();
cout << "\n";
var2.f();
// var2.put_ij(); no longer accessible
return 0;
}
listing 6
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "\nBase created\n"; }
};
class D_class1 : public Base {
public:
D_class1() { cout << "D_class1 created\n"; }
};
int main()
{
D_class1 d1;
// do nothing but execute constructors
return 0;
}
listing 7
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "\nBase created\n"; }
~Base() { cout << "Base destroyed\n\n"; }
};
class D_class1 : public Base {
public:
D_class1() { cout << "D_class1 created\n"; }
~D_class1() { cout << "D_class1 destroyed\n"; }
};
int main()
{
D_class1 d1;
cout << "\n";
return 0;
}
listing 8
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "\nBase created\n"; }
~Base() { cout << "Base destroyed\n\n"; }
};
class D_class1 : public Base {
public:
D_class1() { cout << "D_class1 created\n"; }
~D_class1() { cout << "D_class1 destroyed\n"; }
};
class D_class2 : public D_class1 {
public:
D_class2() { cout << "D_class2 created\n"; }
~D_class2() { cout << "D_class2 destroyed\n"; }
};
int main()
{
D_class1 d1;
D_class2 d2;
cout << "\n";
return 0;
}
listing 9
#include <iostream>
using namespace std;
class X {
protected:
int a;
public:
void make_a(int i) { a = i; }
};
class Y {
protected:
int b;
public:
void make_b(int i) { b = i; }
} ;
// Z inherits both X and Y
class Z : public X, public Y {
public:
int make_ab() { return a*b; }
} ;
int main()
{
Z i;
i.make_a(10);
i.make_b(12);
cout << i.make_ab();
return 0;
}
listing 10
#include <iostream>
using namespace std;
class X {
protected:
int a;
public:
X() {
a = 10;
cout << "Initializing X\n";
}
};
class Y {
protected:
int b;
public:
Y() {
cout << "Initializing Y\n";
b = 20;
}
} ;
// Z inherits both X and Y
class Z : public X, public Y {
public:
Z() { cout << "Initializing Z\n"; }
int make_ab() { return a*b; }
} ;
int main()
{
Z i;
cout << i.make_ab();
return 0;
}
listing 11
#include <iostream>
using namespace std;
class X {
protected:
int a;
public:
X(int i) { a = i; }
};
class Y {
protected:
int b;
public:
Y(int i) { b = i; }
} ;
// Z inherits both X and Y
class Z : public X, public Y {
public:
/* Initialize X and Y via Z's constructor.
Notice that Z does not actually use x or y
itself, but it could, if it so chooses. */
Z(int x, int y) : X(x), Y(y)
{
cout << "Initializing\n";
}
int make_ab() { return a*b; }
} ;
int main()
{
Z i(10, 20);
cout << i.make_ab();
return 0;
}
listing 12
B_class *p; // pointer to object of type B_class
B_class B_ob; // object of type B_class
D_class D_ob; // object of type D_class
listing 13
p = &B_ob; // p points to object of type B_class
p = &D_ob; /* p points to object of type D_class,
which is an object derived from B_class. */
listing 14
// Using pointers on derived class objects.
#include <iostream>
#include <cstring>
using namespace std;
class B_class {
char name[80];
public:
void put_name(char *s) { strcpy(name, s); }
void show_name() { cout << name << " "; }
} ;
class D_class : public B_class {
char phone_num[80];
public:
void put_phone(char *num) {
strcpy(phone_num, num);
}
void show_phone() { cout << phone_num << "\n"; }
};
int main()
{
B_class *p;
B_class B_ob;
D_class *dp;
D_class D_ob;
p = &B_ob; // address of base
// Access B_class via pointer.
p->put_name("Thomas Edison");
// Access D_class via base pointer.
p = &D_ob;
p->put_name("Albert Einstein");
// Show that each name went into proper object.
B_ob.show_name();
D_ob.show_name();
cout << "\n";
/* Since put_phone and show_phone are not part of the
base class, they are not accessible via the base
pointer p and must be accessed either directly,
or, as shown here, through a pointer to the
derived type.
*/
dp = &D_ob;
dp->put_phone("555 555-1234");
p->show_name(); // either p or dp can be used in this line
dp->show_phone();
return 0;
}
listing 15
((D_class *)p)->show_phone();
listing 16
// A short example that uses virtual functions.
#include <iostream>
using namespace std;
class Base {
public:
virtual void who() { // specify a virtual function
cout << "Base\n";
}
};
class first_d : public Base {
public:
void who() { // define who() relative to first_d
cout << "First derivation\n";
}
};
class second_d : public Base {
public:
void who() { // define who() relative to second_d
cout << "Second derivation\n";
}
};
int main()
{
Base base_obj;
Base *p;
first_d first_obj;
second_d second_obj;
p = &base_obj;
p->who(); // access Base's who
p = &first_obj;
p->who(); // access first_d's who
p = &second_obj;
p->who(); // access second_d's who
return 0;
}
listing 17
/* Here, a base class reference is used to access
a virtual function. */
#include <iostream>
using namespace std;
class Base {
public:
virtual void who() { // specify a virtual function
cout << "Base\n";
}
};
class first_d : public Base {
public:
void who() { // define who() relative to first_d
cout << "First derivation\n";
}
};
class second_d : public Base {
public:
void who() { // define who() relative to second_d
cout << "Second derivation\n";
}
};
// Use a base class reference parameter.
void show_who(Base &r) {
r.who();
}
int main()
{
Base base_obj;
first_d first_obj;
second_d second_obj;
show_who(base_obj); // access Base's who
show_who(first_obj); // access first_d's who
show_who(second_obj); // access second_d's who
return 0;
}
listing 18
// Derive from first_d, not Base
class second_d : public first_d {
public:
void who() { // define who() relative to second_d
cout << "Second derivation\n";
}
};
listing 19
#include <iostream>
using namespace std;
class Base {
public:
virtual void who() {
cout << "Base\n";
}
};
class first_d : public Base {
public:
void who() {
cout << "First derivation\n";
}
};
class second_d : public Base {
// who() not defined
};
int main()
{
Base base_obj;
Base *p;
first_d first_obj;
second_d second_obj;
p = &base_obj;
p->who(); // access Base's who()
p = &first_obj;
p->who(); // access first_d's who()
p = &second_obj;
p->who(); /* access Base's who() because
second_d does not redefine it */
return 0;
}
listing 20
#include <iostream>
using namespace std;
class figure {
protected:
double x, y;
public:
void set_dim(double i, double j) {
x = i;
y = j;
}
virtual void show_area() {
cout << "No area computation defined ";
cout << "for this class.\n";
}
} ;
class triangle : public figure {
public:
void show_area() {
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};
class square : public figure {
public:
void show_area() {
cout << "Square with dimensions ";
cout << x << "x" << y;
cout << " has an area of ";
cout << x * y << ".\n";
}
};
int main()
{
figure *p; /* create a pointer to base type */
triangle t; /* create objects of derived types */
square s;
p = &t;
p->set_dim(10.0, 5.0);
p->show_area();
p = &s;
p->set_dim(10.0, 5.0);
p->show_area();
return 0;
}
listing 21
class circle : public figure {
public:
void show_area() {
cout << "Circle with radius ";
cout << x;
cout << " has an area of ";
cout << 3.14 * x * x;
}
} ;
listing 22
#include <iostream>
using namespace std;
class figure {
protected:
double x, y;
public:
void set_dim(double i, double j=0) {
x = i;
y = j;
}
virtual void show_area() {
cout << "No area computation defined ";
cout << "for this class.\n";
}
} ;
class triangle : public figure {
public:
void show_area() {
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};
class square : public figure {
public:
void show_area() {
cout << "Square with dimensions ";
cout << x << "x" << y;
cout << " has an area of ";
cout << x * y << ".\n";
}
};
class circle : public figure {
public:
void show_area() {
cout << "Circle with radius ";
cout << x;
cout << " has an area of ";
cout << 3.14 * x * x;
}
} ;
int main()
{
figure *p; /* create a pointer to base type */
triangle t; /* create objects of derived types */
square s;
circle c;
p = &t;
p->set_dim(10.0, 5.0);
p->show_area();
p = &s;
p->set_dim(10.0, 5.0);
p->show_area();
p = &c;
p->set_dim(9.0);
p->show_area();
return 0;
}
listing 23
class figure {
double x, y;
public:
void set_dim(double i, double j=0) {
x = i;
y = j;
}
virtual void show_area() = 0; // pure
} ;
listing 24
/*
This program will not compile because the class
circle does not override show_area().
*/
#include <iostream>
using namespace std;
class figure {
protected:
double x, y;
public:
void set_dim(double i, double j) {
x = i;
y = j;
}
virtual void show_area() = 0; // pure
} ;
class triangle : public figure {
public:
void show_area() {
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};
class square : public figure {
public:
void show_area() {
cout << "Square with dimensions ";
cout << x << "x" << y;
cout << " has an area of ";
cout << x * y << ".\n";
}
};
class circle : public figure {
// no definition of show_area() will cause an error
};
int main()
{
figure *p; // create a pointer to base type
circle c; // attempt to create an object of type circle -- ERROR
triangle t; // create objects of derived types */
square s;
p = &t;
p->set_dim(10.0, 5.0);
p->show_area();
p = &s;
p->set_dim(10.0, 5.0);
p->show_area();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -