Sams Teach Yourself C in 21 Days

(singke) #1
Object-Oriented Programming Languages 637

BD1


LISTINGB1.2 cube.cpp. C++ OOP in action
1: // A C++ program with square and cube classes
2: #include <iostream.h>
3:
4: // Simple square class
5: class square {
6: public:
7: square();
8: square(int);
9: int length;
10: long area();
11: int draw();
12: };
13:
14: //simple cube class inheriting from the square class
15: class cube: public square {
16: public:
17: cube( int );
18: long area();
19: };
20:
21: //constructor for square
22: square::square()
23: {
24: length = 4;
25: }
26:
27: //overloaded constructor for square
28: square::square( int init_length )
29: {
30: length = init_length;
31: }
32:
33: //square class’ area function
34: long square::area( void )
35: {
36: return((long) length * length);
37: }

FIGUREB1.3
A cube inherits parts
of a square.

(x, y)

length

square
Area of square = length * length

(x, y)

cube
Area of cube = Area of square * length

length

36 448201x-Bonus1 8/13/02 11:18 AM Page 637

Free download pdf