In this lecture we learned about: Separation of interface and implementation, objects are declared as primitive type, the destructor operator, copy constructor, default constructor.
AMBIENT/
Chủ đề:
Nội dung Text: Lecture note Data visualization - Chapter 13
- Lecture 13
- Recap
Separation of Interface and Implementation
Objects are Declared as Primitive Type
The Destructor Operator
Copy Constructor
Default Constructor
- Selective Questions from
Exercise
- Q: What is information hiding? What is encapsulation?
How does C++ support these concepts?
Answer:
Information hiding makes implementation details,
including components of an object, inaccessible.
Encapsulation is the grouping of data and the
operations that apply to them to form an aggregate
while hiding the implementation of the aggregate.
Encapsulation and information hiding are achieved
in C++ through the use of the class.
- Q: Explain the public and private sections of the class.
Answer:
Members in the public section of a class are visible
to nonclass routines and can be accessed via the.
member operator. Private members are not visible
outside of the class.
- Q: Describe the roles of the constructor and destructor.
Answer:
The constructor is called when an object is created,
either by declaration, a call to new, or as a member of
an object which itself is being constructed.
The destructor is called when the object exits scope,
either because it is a local variable in a terminating
function, it is subject to a delete, or it is a member of an
object whose destructor is called.
- Q: What is the difference between a copy constructor and
a copy assignment operator?
Answer:
The copy constructor creates and initializes a new
object. It is used to implement call by value.
The copy assignment operator copies into an already
existing object.
- Q: If a class provides no constructor and no destructor,
what is the result?
Answer:
The default constructor is a memberbymember
application of a default constructor. The default
destructor is a memberbymember application of a
destructor.
- Chapter 4
Inheritance
- Introduction
In this chapter we will study
how the general principles of inheritance and the
objectoriented concept of polymorphism relate to code
reuse,
how inheritance is implemented in C++,
how a collection of classes can be derived from a
single abstract class,
how runtime binding decisions, rather than compile
time linking decisions, can be made for these classes.
- Introduction to Inheritance
On one level inheritance is the fundamental object
oriented principle governing the reuse of code among
related classes
Inheritance models the ISA relationship
In an ISA relationship, the derived class is a base class
For example: a Circle ISA Shape and a Car ISA Vehicle.
However, an Ellipse ISNOTA Circle
Inheritance relationships form hierarchies
For instance: we can extend Car to other classes, as a
Foreign car ISA Car and a Domestic car ISA Car and so
- Continued….
Another type of relationship is a HASA relationship
In a HASA relationship, the derived class has a base
class.
This type of relationship does not possess the properties
that would be natural in an inheritance hierarchy
For Example: A car HASA steering wheel
Generally, HASA relationships should not be modeled
by inheritance. Instead, they should be modeled with the
technique of composition, in which the components are
simply made private data fields
- Inheritance in C++
The C++ language itself makes some use of inheritance in
implementing its class libraries.
Two examples are
Exceptions
Files
- Exceptions
C++ defines in the
class exception
There are several kinds of
exceptions, including bad_alloc
and bad_cast
In figure
Classes in exception hierarchy
has been shown
Each is a separate class, but for
all of them, the what method can
be used to return a string that
- I/O Operation
In figure
streams hierarchy uses
inheritance
In actual, the streams
hierarchy is more complex
than shown
- ISA Relationship
The inheritance models an ISA relationship
A button ISA component
A bad_cast ISA exception
An if stream ISA istream
Because of ISA relationships, the fundamental property
of inheritance guarantees that any method that can be
performed by istreamcan also be performed by
ifstreamand that an ifstream object can always be
referenced by an istream reference
The reverse is not true
- Continued….
what is a method available in the exception class
If we need to catch various exceptions we can use a catch
handler and write:
{ catch( const exception & e ) I cout
- Polymorphism in Inheritance
The call to what illustrates an important objectoriented
principle known as polymorphism
It is the ability of a reference variable to reference objects
of several different types
When operations are applied to the variable, the operation
that is appropriate to the actual referenced object is
automatically selected
The same is true for pointer variables
In the case of an exception reference, a run time decision
is made
- Classes in Inheritance
Inheritance is the process of deriving a class from a base
class without disturbing the implementation of the base
class
The base class is the foundation for inheritance
A derived class is a completely new class that inherits all
the properties of the base class, with all the public
methods available to the base class becoming public
methods with identical implementation of the derived
class
The derived class can then add data members and
additional methods and change the meaning of the
- Continued….
A derived class is type compatible with its base class
A reference variable of the base class type may reference
an object of the derived class, but not vice versa
Sibling classes (i.e., classes derived from a common class)
are not type compatible
The exception hierarchy highlights the typical design
approach of factoring commonalities into base classes and
then specializing in the derived classes
In this hierarchy, we say that the derived class is a
subclass of the base class and the base class is a superclass