[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / 08
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] <08> [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Apr 29 12:19:21 PDT 2009

Contents


    CSci202 Computer Science II, Session 08 Modeling


      (Previous): C++ Classes [ 07.html ]

      Project 2 is due in today

      Preparation 12 Object-Oriented Programming: Inheritance 640

    1. 12.1 Introduction
    2. 12.2 Base Classes and Derived Classes 642
    3. 12.3 protected Members 645
    4. 12.4 Relationship between Base Classes and Derived Classes 645
      • 12.4.1 Creating and Using a CommissionEmployee Class 646
      • 12.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance 651
      • 12.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy 657
      • 12.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data 662
      • 12.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data 669
    5. 12.5 Constructors and Destructors in Derived Classes 677
    6. 12.6 public, protected and private Inheritance 685 My advice: don't. Always inherit "public". It is better to rethink the hierarchy in some way, in my humble opinion than create objects that are both "a special kind of" and also "don't behave the same as"!
    7. 12.7 Software Engineering with Inheritance 685
    8. 12.8 Wrap-Up 687

      Resources

      Study this
    9. inheritance::= See http://cse.csusb.edu/dick/cs202/inheritance.html

      UML Generalization and C++ Derivation

      Notation shown on board.
    10. Wodget ------|> Widget
    11. A Widget generalizes a Wodget.

      C++ public derivation

       		class Wodget: public Widget { added stuff };
      reflects the UML meaning well.

      Input

        Inheritance -- Don't reinvent the wheel.

      1. inheritance::= See http://cse.csusb.edu/dick/cs202/inheritance.html

        What is inherited?

        Exercise/Demo showing what functions and attributes can be inherited... [ 07ex.cpp ]

        Constructors and Inheritance

        Names do not change when inherited so constructors can not be inherited. There is a syntax to explicitly call the right Base constructors.

        Destructors and inheritance

        See below....

        Protected Members

        Only if you have to.... [ 07ex2.cpp ]

        Quick Exercise

          There is a company that makes Widgets. It is called "Universal Widgets".

          They make Widgets and Wodgets. A Wodget is a special kind of Widget. So our UML design is

           [UML diagram of exercise]

        1. On a blank sheet of paper write your name.
        2. Write the code for a class Widget that has a private integer identifier that is set when a Widget is constructed and has a "getId()" function that returns the id when called.
        3. Check your code.
        4. Swap it with some one near you. Check the code you just got. Negotiate any discrepancies.
        5. Now take the other person's work, add your name to it, and write a new class called Wodget that is just like a Widget accept that it has a (private) number of knobs, and has an operation "void add()" to add a knob to the Wodget, and a function "int getKnobs()". DO NOT CHANGE Widget in any way. DO NOT copy any code in Widget.
        6. Hand back the paper with both classes for the first person to review and correct.
        Here is the code for the Widget classes [ widgets.h ] and my unit tests [ testWidgets.cpp ]

      Interactive exercise to prepare for quizzes

      25 T/F questions on C++ classes [ 07tf.html ]

      Assigned Work Due -- Study and submit question

      Chapter 12 pages ppp-ppp -- Object Oriented Programming: Inheritance

      can you discuss the ways in which inheritance promotes software reuse,save time during program development and helps prevent errors.

      Reuse -- even if the base class is compiled into a ".o" file and you never see the source code, you can still reuse it by deriving a new class with special features.

      Saving time programming -- you don't have to retype old classes to create similar ones. You derive new ones.

      Preventing errors -- inheritance doen't subvert the walls that keep data private.

      [ inheritance.html ]

      Chapter 12 pages 640 -- Protected Members

      Why do we use private over protected on a regular basis?

      Safety and security. Your programs will have fewer bugs (at the cost of more time thinking and compiling).

      Chapter 12 pages 642... -- Base and Derived classes

      When a derived class is created what happens to the private member functions of the base class?

      They are still there - and still private. Even the derived class can not call them.

      Chapter 12 pages 642-643 -- Derived Class/ Base Class

      How would you explain a base class such as, lets say, a mall.

      Any class can be a base class. There is nothing special about them. All C++ classes can have other classes derived from them.

      How would I define a Mall in C++? I would have to talk to my client and stakeholders to find out what they think a Mall is. This would tell me about the data associated with a Mall. Then I would need to know a lot about what the software is going to do. This will give me ideas for what functions a Mall might have in the software.

      Chapter 12 pages 642-645 -- Base Classes and Derived Classes

      What is the difference between Base classes and Derived classes?

      Not much -- accept that the derived class extends or implements the base class.

      Notice -- there are no special "Base" classes. But in any piece of software with classes we can extend it by deriving new classes, like them, but better, by "deriving" them.

      Base----Derived is a relationship between two classes. The Derived class should have objects that are a special kind of object in the Base class. Just like a Student is a special kind of Person.

      Chapter 12 pages 642 -- Base Classes and Derived Classes

      Is there any problem if we declare an object of a class two times to used it in another class?

      No. But this is composition not derivation. When we declare an object in a class we are using composition. When we derive we are using a more powerful method of making a class from the old one.

      With multiple inheritance -- deriving one class from two or more other classes an ambiguity can happen where you don't know how many copies of class are inherited. C++ has notation for this. It is in my notes [ inheritance.html#Multiple Inheritance ] for some details.

      The bottom line Most people use the rule that you don't inherit from more than one base class, unless the base class has no data members -- it is an "interface" or "abstract" class.

      The won't be any questions on quizzes/labs/finals/etc in CSci202 on multiple inheritance.

      Chapter 12 pages 657 -- Inheritance

      What attributes are inherited when a class is derived from another, and what are the differences between inheritance classes and friend classes?

      All attributes are inherited.... but the private ones can not be accesses in the derived class.

      An derived class has all the properties -- data and functions of the base class. For example, a Dog is a special kind of Mammal.

      A friend class has access to private data and functions, but is not a special kind of the class it is friendly with.... rather like a pet dog may be friendly to its owner, but is not a special kind of human.

      Chapter 12 pages 645-647 -- base and derived classes

      how does base and derived classes work.

      The base class is unaffected by the having a derived class. You declare objects and use them in the usual way.

      The compiler uses both the derived class and the base class to construct object in the derived class. They have all the data of the base class plus all the data in the derived class. The member functions are all the base ones plus all the derived ones.

      Result -- objects that are a special kins of Base -- with extra data and functions.

      Chapter 12 pages 668 -- Using protected data

      In which case is it best to use private data members? And why?

      Make data private! This makes bugs rarer.

      Exception -- you are designing a class that has data and has lots of derived classes all needing access to the data. Make this data "protected". I only do this in small examples.

      The proper way to provide access to data is to provide functions that do it safely.

      You can make this functions protected and this gives a measure of control over access to the data that is usually good enough.

      Chapter 12 pages 678 -- Base and Derived Class

      Is there a way that a derived class can access private members of the base class without using public member functions? If other classes can be made friend, is there something similar for inherited classes.

      It would be odd.... but the base class can declare the derived class to be a friend and so share the privte date with the derived class. It can also use "protected".

      But if someone has made things private then you are stuck with the public accessors. The assumption is that the base class has some sensitive data that only it can be trusted to manipulate. This is the standard way to right a class.

      Chapter 12 pages 645 -- Protected

      Since you do not want us using friends, will "Protected" be of much use to us?

      They are two different things. "Protected" shares thing with derived classes -- the family of the base class. "Friend" shares things with other classes that are not derived... so we have "Friends and Family".

      Notice: both make the program easier to write but more likely to have bugs. It also makes the program harder to change.

      Chapter 12 pages 683-685 -- Constructor and Destructors in Derived Classes.

      In fig. 12.26, why doesn't the program output a BasePlusCommissionEmployee Constructor and Destructor for "Bob" like it did for "Lisa" and "Mark" ?

      Because "Bob" is not declared to be a BasePlusCommissionEmployee. He is a CommissionEmployee.... see line 23.

      Chapter 12 pages 645 -- protected members

      Can you elaborate a bit on protected members and why they are useful?

      Nobody but a class and its friends can access or use "private" members. Anybody -- even a classes enemies -- can access "public" members of a class. When a part of a class is "protected" then can be accessed and used by classes derived from it, and it self, (and its friends).

      Chapter 12 pages 664-667 -- private and protected

      i like to know what the differences is between private and protected and can we use both in the same code or we are only limited to picking one or the other

      A class can have public, private and protected members -- any number of each, in any order.... Each data item or function is either public, private, or protected.

      Private memebrs -- only accessable insdie the class.

      Public members -- accessable by any class and anywhere.

      Protected member -- accessible in the class and inside any classes that are derived from the class.

      Chapter 12 pages 641-687 -- Inheritance permissions

      Can you go over the aspects of public, protected and private inheritance and how they function?

      See below.

      Chapter 12 pages 644 -- Base Classes and Derived Classes -- private and public inheritance

      Can you further explain the differences and similarities with public, private, and protected inheritance

      Warning -- the words private, protected, and public are used in a different way when deriving a class from a base class.

       		class Derived: public Base { extra stuff };
       		class Derived: protected Base { extra stuff };
      Protects any Base class public members -- hides them away from other classes.
       		class Derived: private Base { extra stuff };
      Privatizes any Base class public or protected members -- hides them away from all other classes.

      Public inheritance takes the properties (data and functions) of the base class and adds to them. The other two hide properties( data and functions) as well as adding new ones.

      Chapter 11 pages 685 -- Protected and Private Inheritance

      Why are protected and private inheritance not "is-a" relationships?

      The "is-a" or "is-a-special-kind-of" relation is only true is one class has all the data and all the functions of the other. It must be just like the other class.... only more so. It can not have fewer functions or data -- and hiding the base classes members break this definition.

      Chapter 12.6 pages 685 -- Protected and Private

      Why is protected and private inheritance rarely used?

      They are more complicated to understand. They don't provide a useful feature. People neither want or need them They provide features that are confusing.

      Good news -- they are not part of CSci202.

      T/F questions on C++ classes

      [ 07tf.html ]

      Exercises If Time in class

      Here is the code for some improved Widget and Wodget classes: [ widgets2.h ] and my unit tests [ testWidgets2.cpp ] , draw the UML diagram of these classes. What changes have I made and why did I do them?

      Quiz

      Lab

      [ lab04.html ]

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 08 Modeling) <<Contents | End>>
    (Next): Polymorphism [ 09.html ] -- put your analyst on danger money!

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".

End