Тема: Доступ до protected змінних базового класу
Вітаю Вас форумчани!
Я займаюсь по книзі Герберта Шилдта і виконую вправи по динамічній ідентифікації типів.
Маю дві подібні програмки
№1 компілюється нормально:
/* Dynamic types identification demonstration */
#include <iostream>
#include <cstdlib>
#include <typeinfo>
using namespace std;
class figure {
protected:
double x, y;
public:
figure(double i, double j) {
x = i;
y = j;
}
virtual double area() = 0;
};
class triangle : public figure {
public:
triangle(double i, double j) : figure(i, j) {}
double area() {
return x * 0.5 * y;
}
};
class rectangle : public figure {
public:
rectangle(double i, double j) : figure(i, j) {}
double area() {
return x * y;
}
};
class circle : public figure {
public:
circle(double i, double j=0) : figure(i, j) {}
double area() {
return 3.14 * x * x;
}
};
/* Fabric of objects of class figure */
figure *factory() {
switch(rand() % 3) {
case 0: return new circle(10.0);
case 1: return new triangle(10.1, 5.3);
case 2: return new rectangle(4.3, 5.7);
}
return 0;
}
int main() {
figure *p; /* pointer to the base class */
int i;
int t = 0, r = 0, c = 0;
/* generating and counting objects */
for (i = 0; i < 10; i++) {
p = factory(); /* generating object */
cout << "Object has type " << typeid(*p).name();
cout << ". ";
/* keeping this object in mind */
if(typeid(*p) == typeid(triangle)) t++;
if(typeid(*p) == typeid(rectangle)) r++;
if(typeid(*p) == typeid(circle)) c++;
/* Output square of the figure */
cout << "The square of the figure is" << p->area() << endl;
}
cout << endl;
cout << "There was generated next objects:\n";
cout << " triangles: " << t << endl;
cout << " rectangles: " << t << endl;
cout << " circles: " << c << endl;
return 0;
}
№2 Ця ж програма, але із динамічним визначенням типу:
/* Template version of figure hierarchy */
#include <iostream>
#include <cstdlib>
#include <typeinfo>
using namespace std;
template <class T> class figure {
protected:
T x, y;
public:
figure(T i, T j) {
x = i;
y = j;
}
virtual T area() = 0;
};
template <class T> class triangle : public figure<T> {
public:
triangle(T i, T j) : figure<T>(i, j) {}
T area() {
return x * 0.5 * y;
}
};
template <class T> class rectangle : public figure<T> {
public:
rectangle(T i, T j) : figure<T>(i, j) {}
T area() {
return x * y;
}
};
template <class T> class circle : public figure<T> {
public:
circle(T i, T j=0) : figure<T>(i, j) {}
T area() {
return 3.14 * x * x;
}
};
/* Fabric of objects of class figure */
figure<double> *generator() {
switch(rand() % 3) {
case 0: return new circle<double>(10.0);
case 1: return new triangle<double>(10.1, 5.3);
case 2: return new rectangle<double>(4.3, 5.7);
}
return 0;
}
int main() {
figure<double> *p; /* pointer to the base class */
int i;
int t = 0, r = 0, c = 0;
/* generating and counting objects */
for (i = 0; i < 10; i++) {
p = generator(); /* generating object */
cout << "Object has type " << typeid(*p).name();
cout << ". ";
/* keeping this object in mind */
if(typeid(*p) == typeid(triangle<double>)) t++;
if(typeid(*p) == typeid(rectangle<double>)) r++;
if(typeid(*p) == typeid(circle<double>)) c++;
/* Output square of the figure */
cout << "The square of the figure is" << p->area() << endl;
}
cout << endl;
cout << "There was generated next objects:\n";
cout << " triangles: " << t << endl;
cout << " rectangles: " << t << endl;
cout << " circles: " << c << endl;
return 0;
}
Компілятор не компілює другу програму і видає помилки:
main.cpp:22:16: error: ‘x’ was not declared in this scope
return x * 0.5 * y;
^
main.cpp:22:26: error: ‘y’ was not declared in this scope
return x * 0.5 * y;
Чому в першій програмі ми можемо отримати доступ до x і y, а в другій ні?