Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Notes on Class Inheritance in C++ - Foundation of Computer Science Lab | COT 3002L, Lab Reports of Computer Science

Material Type: Lab; Class: Foundation of Computer Sci Lab; Subject: Computing Theory; University: Florida Atlantic University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 07/23/2009

koofers-user-gja
koofers-user-gja 🇺🇸

10 documents

1 / 16

Toggle sidebar

Related documents


Partial preview of the text

Download Notes on Class Inheritance in C++ - Foundation of Computer Science Lab | COT 3002L and more Lab Reports Computer Science in PDF only on Docsity! Session 13 Student Name Other Identification Class Inheritance in C++ In this laboratory session you will: 1. Learn why inheritance is important and how to define derived classes. 2. Investigate how inheritance influences encapsulation. 3. Experiment with constructors of derived classes. 4. Learn about polymorphism and virtual functions. Your instructor will tell you which of the proposed experiments you are to perform. In preparation for this laboratory session, you should read Chapter Seven of Computer Science: An Overview . 192 Session 13 Fundamentals of Inheritance Object-oriented programming languages such as C++ provide a mechanism, called inheritance, to describe new classes in terms of existing ones (see Chapter Seven of Computer Science: An Overview ). The original class is called the base class, and a class that is built using a base class is called a derived class. To motivate the need for inheritance, reconsider the tv_show class which was introduced in laboratory Session 11. This class was defined as follows: class tv_show { private: char title[30]; int length; // 30 or 60 minutes char kind; // c - comedy, d - drama, i - infomercial public: tv_show(char tl[],char k) // constructor {strcpy(title, tl); kind = k; if (kind == 'd') length = 60; else if (kind == 'c') || (kind == 'i') length = 30; } void print(void) {cout << "Show title: " << title << endl; cout << "Length: " << length << " minutes" << endl; cout << "Type: " << kind << endl; } }; Recall that the kind data member was used to initialize the length data member in the constructor, depending on whether the show is a comedy, drama, or infomercial. In reality, however, there are more distinctions among such programs than their lengths. For example, a comedy has a laugh track, whereas an infomercial has a toll free telephone number. Such distinctions could lead us to three distinct classes. Rather than one tv_show class, we could define comedy , drama , and infomercial classes. Each of these classes would have only the data members appropriate for it. As an example, the comedy class might be defined as class comedy { private: char title[30]; int length; // 30 or 60 minutes char laugh_track; // y or n public: // constructor comedy(char tl[]) {strcpy(title, tl); length = 30; laugh_track = 'y'; } Session 13 195 class derivedclassname : public baseclassname { private: data member definitions public: member function definitions }; The base class, tv_show , will contain the data members that are common to all types of shows. The definition looks like this class tv_show { protected: char title[30]; int length; // 30 or 60 minutes }; To define the comedy class that inherits from tv_show , we define comedy as a derived class, class comedy : public tv_show { private: char laugh_track; . . . }; Note the use of the access type protected in the tv_show class . The protected access type is used in the defintion of a base class. Protected means that the members that follow the keyword can be accessed by member functions in classes that are derived from this class. Thus, any member functions we may declare in our comedy class can access the protected data members defined in the tv_show class To demonstrate, let us add a print member function (to display the current values of the data members) and a constructor to our comedy class. class comedy : public tv_show { private: char laugh_track; public: // constructor comedy(char tl[]) {strcpy(title, tl); length = 30; laugh_track = 'y'; } void print(void) {cout << "Show title: " << title << endl; cout << "Length: " << length << " minutes" << endl; cout << "Laughtrack: " << laugh_track << endl; } }; 196 Session 13 We can establish objects having the structure of comedy through traditional variable declarations such as comedy mycomedy("A Boy and His Dog"); which declares mycomedy to be a variable of type comedy . Experiment 13.2 Step 1 . Try to execute the following program (CP13E02) and record the results. #include <iostream.h> #include <string.h> class tv_show { protected: char title[30]; int length; // 30 or 60 minutes }; class infomercial : public tv_show { private: char phone_number[15]; public: infomercial(char s[], char p[]) // constructor {strcpy(title, s); length = 30; strcpy(phone_number,p); } void print(void) {cout << "Show title: " << title << endl; cout << "Length: " << length << " minutes" << endl; cout << "Phone Number: " << phone_number << endl; } }; void main(void) { infomercial one("Vote for Me","1-800-555-4455"); tv_show two; one.print(); } ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ Session 13 197 Step 2 . Add the statement two.length = 30; to the function main in step 1. What happens when you try to compile and execute the modified program? What does that tell you about access to protected data members from outside of the derived class? ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ Step 3 . Modify protected: in the program in step 1 to be private: What happens when you try to compile and execute the modified program? What does that tell you about access to private data members in the base class by the derived class? ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ Constructors and Inheritance Note that in our previous examples, the constructors in the derived classes infomercial and comedy have been given the task of initializing the value of the title data member in the base class tv_show . It would be better to define a constructor in the base class that initializes the title data member and define the derived class constructors to use the base 200 Session 13 to theinfomercial class constructor. Record what happens. What does this tell you about the order in which the constructors of a base class and its derived class are executed? ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ Step 3. Modify the program in step 1 to change the line infomercial(char tl[], char p[]) : tv_show(tl) to be infomercial(char tl[], char p[]) so that the infomercial class constructor no longer invokes the tv_show class constructor. Record and explain what happens. ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ ________________________________________________________________________ Polymorphism and Virtual Functions We have defined a variety of objects that represent televsion shows. In particular, our tv_show class can have the form comedy, drama, or infomercial. Each object can respond to a message to print itself. However, the routine used to perform the print operation depends on the kind of television show involved. Printing a comedy is different from printing an infomercial. Thus, each class interprets the request to print itself in its own way. This customized interpretation of a message is called polymorphism. Note that in contrast to overloading where the interpretation of a function is determined by the parameters, polymorphism refers to the interpretation of a function based on the object Session 13 201 asked to execute it. Suppose we define objects with the following statements: infomercial show1("Vote for Me","1-800-555-4455")); comedy show2("A Boy and His Dog"); tv_show show3("Simple Show"); tv_show *show4; Note that show4 is declared to be a pointer to an instance of the base class tv_show. This means that it can be used to point to such an object, or to any object from a class derived from tv_show as well. This flexibility is not granted for pointers to the traditional types such as int and float. When a pointer that is declared to bea pointer to something of type float, it cannot be used to point to something of type int. However, a pointer that is declared to be a pointer to an object whose type is a base class can also be used to point to objects whose types are derived classes. In our example, this means that the statement show4 = &show1; could be used to cause show4 to point to the object show1 (of type infomercial) and show4 = &show2; could be used to cause show4 to point to the object show2 (of type comedy). The subtleties arising from this flexibility surface when we consider the problem of sending the print message to the object pointed to by show4. If show4 is pointing to an infomercial object, then the print member function defined for the infomercial class should be executed. But, if show4 is pointing to a comedy object, then the print member function defined for the comedy class should be executed. In other words, the print member function must be polymorphic. To request such polymorphic behavior for a member function, the declaration of the member function in the base class must be preceded by the keyword virtual. Without such a declaration, the member function will not be given polymorphic abilities, as you will see in Step 3 of Experiment 13.4. The following declaration of the base class tv_show, in which the member function print is declared to be virtual, provides the polymorphic behavior we desire in our example. The derived classes comedy and infomercial would be defined as before. In particular, the print member functions in these derived classes would be unchanged. Only the declaration of the print member function in the base class is declared to be virtual. class tv_show { protected: char title[30]; int length; // 30 or 60 minutes public: // constructor tv_show(char s[]) {strcpy(title, s);} virtual void print(void) {cout << "Show title: " << title << endl;} }; 202 Session 13 Experiment 13.4 Step 1. Execute the following program (CP13E04) and record the results. #include <iostream.h> #include <string.h> class tv_show { protected: char title[30]; int length; // 30 or 60 minutes public: // base class constructor tv_show(char s[]) {strcpy(title, s);} virtual void print(void) {cout << "Show title: " << title << endl;} }; class comedy : public tv_show { private: char laugh_track; public: // derived class constructor comedy(char tl[]) : tv_show(tl) { length = 30; laugh_track = 'y'; } void print(void) {cout << "Comedy show title: " << title << endl; cout << "Length: " << length << " minutes" << endl; cout << "Laughtrack: " << laugh_track << endl; } }; class infomercial : public tv_show { private: char phone_number[15]; public: // derived class constructor infomercial(char tl[], char p[]) : tv_show(tl) {length = 30; strcpy(phone_number,p);} void print(void) {cout << "Infomercial title: " << title << endl; cout << "Length: " << length << " minutes" << endl; cout << "Phone Number: " << phone_number << endl; } }; Session 13 205 Post-Laboratory Problems 13.1. Complete the drama derived class, including a constructor and a print member function. 13.2. What are some advantages and disadvantages of using inheritance? 13.3. There are some video games you play on your home machine and others you prefer to play at the arcade. Each video game has a name, difficulty, manufacturer, and rating. Games you play in the arcade have a cost per play. Games you play at home come either on CD or cartridge. Using inheritance, design the base and derived classes to store information about all of your favorite video games. 13.4. The situation where class A is the base class, class B is derived from class A and class C is derived from class B is called multi-level inheritance. Use multi-level inheritance to write a program that describes employees of a company. Some employees are managers and some are engineers, some are both, some neither. All employees have names and social security numbers. Engineers and managers have computers and e-mail addresses. Only managers have offices with doors that close. 13.5 What does your experience with Experiment 13.4 tell you about the difference between function overloading in C++ functions and polymorphism? 206 Session 13
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved