#include <iostream.h>
class figure
class figure
{
protected:
double x, y;
public:
void setdim(double i, double j)
protected:
double x, y;
public:
void setdim(double i, double j)
{
x = i;
y = j;
}
virtual void showarea( )
x = i;
y = j;
}
virtual void showarea( )
{
cout << "No area computation defined ";
cout << "for this class.\n";
}
} ;
class triangle : public figure
cout << "No area computation defined ";
cout << "for this class.\n";
}
} ;
class triangle : public figure
{
public:
void showarea( )
public:
void showarea( )
{
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};
class rectangle : public figure
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};
class rectangle : public figure
{
public:
void showarea( )
public:
void showarea( )
{
cout << "Rectangle with dimensions ";
cout << x << "x" << y;
cout << " has an area of ";
cout << x * y << ".\n";
}
};
cout << "Rectangle 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
rectangle s;
p = &t;
p->setdim(10.0, 5.0);
p->showarea();
p = &s;
p->setdim(10.0, 5.0);
p->showarea();
return 0;
}
{
figure *p; // create a pointer to base type
triangle t; // create objects of derived types
rectangle s;
p = &t;
p->setdim(10.0, 5.0);
p->showarea();
p = &s;
p->setdim(10.0, 5.0);
p->showarea();
return 0;
}
Comments
Post a Comment