Professional Documents
Culture Documents
listing 2
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 3
// Using base pointers on derived class objects.
#include <iostream>
#include <cstring> // for older compilers, use <string.h>
using namespace std;
class B_class {
char author[80];
public:
void put_author(char *s) { strcpy(author, s); }
void show_author() { cout << author << "\n"; }
} ;
int main()
{
B_class *p;
B_class B_ob;
D_class *dp;
D_class D_ob;
return 0;
}
listing 4
((D_class *)p)->show_title();
listing 5
// A short example that uses virtual functions.
#include <iostream>
using namespace std;
class base {
public:
virtual void who() { // specify a virtual
cout << "Base\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 6
first_obj.who();
listing 7
// 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 8
#include <iostream>
using namespace std;
class base {
public:
virtual void who() {
cout << "Base\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 base's who() because
second_d does not redefine it */
return 0;
}
listing 9
#include <iostream>
using namespace std;
class base {
public:
virtual void who() {
cout << "Base\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 first_d's who() because
second_d does not redefine it */
return 0;
}
listing 10
#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";
}
} ;
int main()
{
figure *p; // create a pointer to base type
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 11
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 12
#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";
}
} ;
int main()
{
figure *p; // create a pointer to base type
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 13
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 14
/*
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
} ;
int main()
{
figure *p; // create a pointer to base type
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;
}