Resources
Study this
C++ public derivation
class Wodget: public Widget { added stuff };
reflects the UML meaning well.
They make Widgets and Wodgets. A Wodget is a special kind of Widget. So our UML design is
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