home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
class4.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
1KB
|
42 lines
// Program demonstrates virtual functions
#include <iostream.h>
class cSquare
{
protected:
double length;
public:
cSquare(double len) { length = len; }
double getLength() { return length; }
virtual double getWidth() { return length; }
double getArea() { return getLength() * getWidth(); }
};
class cRectangle : public cSquare
{
protected:
double width;
public:
cRectangle(double len, double wide) :
cSquare(len), width(wide) {}
virtual double getWidth() { return width; }
};
main()
{
cSquare square(10);
cRectangle rectangle(10, 12);
cout << "Square has length = " << square.getLength() << "\n"
<< " and area = " << square.getArea() << "\n";
cout << "Rectangle has length = "
<< rectangle.getLength() << "\n"
<< " and width = "
<< rectangle.getWidth() << "\n"
<< " and area = "
<< rectangle.getArea() << "\n";
return 0;
}