// multi_inhert1.cpp By: Aiman Hanna - (c)1993-2002 Aiman Hanna // This program expalins multiple inheritance. // // Key Points: 1) Watch for ambiguity. #include "multi_inhert1.h" Audio::Audio() { qlty = medium; length = 60; } char* Audio::GetQuality() { switch (qlty) { case low: return "low"; case medium: return "medium"; case high: return "high"; } return "unknown"; } Video::Video() { resolution = 800; length = 60; } Video_Cassette::Video_Cassette( recordingspeed rec, int res, int len, quality q ) { recsp = rec; resolution = res; Video::length = len; // "length = len;" would be ambiguous qlty = q; } void Video_Cassette::ShowInfo() { cout << " The video cassette information is as follow: " << endl; cout << " The recording speed is: " << GetRecordingSpeed() << endl << " the picture resolution is: " << resolution << endl << " the length is: " << Video::length << endl << " and the quality is: " << GetQuality() << endl << endl; } void Video_Cassette::SetRecordingSpeed( recordingspeed rsp = sp ) { recsp = rsp; } char* Video_Cassette::GetRecordingSpeed() { switch (recsp) { case sp: return "sp"; case lp: return "lp"; case ep: return "ep"; } return "unknown"; } Video_Clipper::Video_Clipper( int numclp, int res, quality q ) { numofclips = numclp; resolution = res; qlty = q; } void Video_Clipper::SetNumOfClips( int numclp ) { numofclips = numclp; } int Video_Clipper::GetNumOfClips( void ) { return numofclips; } void Video_Clipper::ShowInfo() { cout << " The video clipper information is as follow: " << endl; cout << " The number of video clips is: " << numofclips << endl << " the picture resolution is: " << resolution << endl << " and the quality is: " << GetQuality() << endl << endl; } Audio_CD::Audio_CD( int trk = 12, int len = 60, quality q = medium ) { numtracks = trk; Audio::length = len; // "length = len;" would NOT be ambiguous here qlty = q; } void Audio_CD::ShowInfo() { cout << " The audio CD information is as follow: " << endl; cout << " The number of tracks is: " << numtracks << endl << " the length is: " << Audio::length << endl << " and the quality is: " << GetQuality() << endl << endl; } void Audio_CD::SetNumTracks( int nt ) { numtracks = nt; } int Audio_CD::GetNumTracks( ) { return numtracks; } void main() { Video_Cassette v1(ep, 1024, 90, high), v2(lp, 600, 120, low); Audio_CD a1(20, 45, high), a2(10, 30, medium); Video_Clipper c1; v1.ShowInfo(); v2.ShowInfo(); a1.ShowInfo(); a2.ShowInfo(); c1.ShowInfo(); cout << " =============================================" << endl; // Notice that both Video_Cassette objects, and Video_Clipper objects // can be considered as Video objects, while Audio_CD objects can NOT. Video* ved_ptr[5]; // Array of 5 pointers to Video objects ved_ptr[0] = new Video_Cassette(sp, 800, 90, high); ved_ptr[1] = new Video_Clipper(30, 600, high); ved_ptr[2] = new Video_Cassette(ep, 1024, 120, low); ved_ptr[3] = new Video_Clipper(5, 400, low); ved_ptr[4] = new Video_Cassette(lp, 600, 30, medium); for ( int i = 0; i < 5; i++ ) { ved_ptr[i] -> ShowInfo(); } cout << " =============================================" << endl; // You can also do the following. Notice that all Video_Cassette objects, // Video_Clipper objects and Audio_CD objects can also be considered as // Audio objects. Audio* aud_ptr[4]; // Array of 4 pointers to Audio objects aud_ptr[0] = new Video_Cassette(sp, 800, 90, high); aud_ptr[1] = new Video_Clipper(30, 600, high); aud_ptr[2] = new Video_Cassette(ep, 1024, 120, low); aud_ptr[3] = new Audio_CD(20, 45, high); for ( i = 0; i < 4; i++ ) { aud_ptr[i] -> ShowInfo(); } } /* The output of the program is as follow: The video cassette information is as follow: The recording speed is: ep the picture resolution is: 1024 the length is: 90 and the quality is: high The video cassette information is as follow: The recording speed is: lp the picture resolution is: 600 the length is: 120 and the quality is: low The audio CD information is as follow: The number of tracks is: 20 the length is: 45 and the quality is: high The audio CD information is as follow: The number of tracks is: 10 the length is: 30 and the quality is: medium The video clipper information is as follow: The number of video clips is: 20 the picture resolution is: 400 and the quality is: medium ============================================= The video cassette information is as follow: The recording speed is: sp the picture resolution is: 800 the length is: 90 and the quality is: high The video clipper information is as follow: The number of video clips is: 30 the picture resolution is: 600 and the quality is: high The video cassette information is as follow: The recording speed is: ep the picture resolution is: 1024 the length is: 120 and the quality is: low The video clipper information is as follow: The number of video clips is: 5 the picture resolution is: 400 and the quality is: low The video cassette information is as follow: The recording speed is: lp the picture resolution is: 600 the length is: 30 and the quality is: medium ============================================= The video cassette information is as follow: The recording speed is: sp the picture resolution is: 800 the length is: 90 and the quality is: high The video clipper information is as follow: The number of video clips is: 30 the picture resolution is: 600 and the quality is: high The video cassette information is as follow: The recording speed is: ep the picture resolution is: 1024 the length is: 120 and the quality is: low The audio CD information is as follow: The number of tracks is: 20 the length is: 45 and the quality is: high */