помогите решить задачку на с++
с++(на Microsoft Visual Studio 2010)
Код:
#include <iostream>
//------------------------------------------------
class PR {
protected:
float *Side;
public:
virtual float P(void) = 0;
PR(int N){Side = new float [N];}
~PR(void){
delete [] Side;
std::cout << "Dest!" << std::endl;
}
};
//------------------------------------------------
class Triangle : PR {
public:
Triangle(float a, float b, float c) : PR(3) {
Side[0] = a, Side[1] = b, Side[2] = c;
}
float P(void){
float p = Side[0] + Side[1] + Side[2];
std::cout << "Perimeter of Triangle = " << p << std::endl;
return p;
}
};
//------------------------------------------------
class Rectangle : PR {
public:
Rectangle(float a, float b) : PR(2){
Side[0] = a, Side[1] = b;
}
float P(void){
float p = 2.0*(Side[0] + Side[1]);
std::cout << "Perimeter of Rectangle = " << p << std::endl;
return p;
}
};
//------------------------------------------------
int main() {
PR *o;
Triangle *tr = new Triangle(2,4,3);
Rectangle *re = new Rectangle(2,4);
o = (PR*)tr;
o->P();
delete tr;
o = (PR*)re;
o->P();
delete re;
std::cin.get();
return 0;
}
//------------------------------------------------
class PR {
protected:
float *Side;
public:
virtual float P(void) = 0;
PR(int N){Side = new float [N];}
~PR(void){
delete [] Side;
std::cout << "Dest!" << std::endl;
}
};
//------------------------------------------------
class Triangle : PR {
public:
Triangle(float a, float b, float c) : PR(3) {
Side[0] = a, Side[1] = b, Side[2] = c;
}
float P(void){
float p = Side[0] + Side[1] + Side[2];
std::cout << "Perimeter of Triangle = " << p << std::endl;
return p;
}
};
//------------------------------------------------
class Rectangle : PR {
public:
Rectangle(float a, float b) : PR(2){
Side[0] = a, Side[1] = b;
}
float P(void){
float p = 2.0*(Side[0] + Side[1]);
std::cout << "Perimeter of Rectangle = " << p << std::endl;
return p;
}
};
//------------------------------------------------
int main() {
PR *o;
Triangle *tr = new Triangle(2,4,3);
Rectangle *re = new Rectangle(2,4);
o = (PR*)tr;
o->P();
delete tr;
o = (PR*)re;
o->P();
delete re;
std::cin.get();
return 0;
}