Class 6 Friday, February 18 CSI2172C - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Class 6 Friday, February 18 CSI2172C

Description:

... the operators in C can be redefined (overloaded) to serve a customized usage. ... The following examples provide more illustration: External Aggregation: ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 28
Provided by: park183
Category:

less

Transcript and Presenter's Notes

Title: Class 6 Friday, February 18 CSI2172C


1
Class 6 (Friday, February 18)CSI2172C
2
Operator Overloading
  • Similar to functions, the operators in C can
    be redefined (overloaded) to serve a customized
    usage.
  • An example is the overloading of the assignment
    operator, covered in the previous lecture.
  • Overloading the priority of operators is not
    possible.
  • It is possible to overload all operators,
    except
  • . . ?

3
Operator Overloading
  • The following operators can all be redefined
  • () -gt
  • - -- ! new new delete
  • /
  • -
  • ltlt gtgt
  • lt lt gt gt
  • !
  • -
  • ,

4
Operator Overloading ()
class point private float xAxis,
yAxis public point() point(float,float)
void print() point operator
(point) float operator (point) point
pointoperator (point p) point
result resultat.xAxis xAxis
p.xAxis resultat.yAxis yAxis
p.yAxis return result float
pointoperator (point p) return (xAxis
p.xAxis yAxis p.yAxis)
5
Operator Overloading
  • The syntax is
  • ltresultgt operatorltoperation typegt(ltargumentsgt)
    ...
  • The operator is the only one that is predefined
    by the compiler for all classes ab
  • It is important to overload the assignment
    operator when a deep copy of complex variables is
    needed.

6
Operator Overloading ()
  • p1 p2 is equivalent to p1.operator(p2)
  • The operator represents a function that belongs
    to the object appearing at the left of the
    operator. The object at the right side is
    considered as a parameter to the function
    represented by .
  • point pointoperator(int x)
  • point result result.xAxis xAxis
    x result.yAxis yAxis return result
  • This call is legal p1 3
  • But the following one is not 3 p1

7
Operator Overloading ()
class string int length char buffer
//... public //... char
operator(int i) char stringoperator
(int i) assert ( i lt length )
return(bufferi) //assert() is a macro
that evaluates a condition. If it returns 0 the
program aborts.
8
Class Relationships
  • Aggregation
  • The has a relationship
  • Containment of objects of other class types
  • Example A class vehicle has an object of class
    type chassis, and another one of class type
    engine
  • Generalization
  • The is a relationship
  • Inheritance from a general class to a more
    specific one
  • Example A class car is an extension of class
    vehicle
  • Association
  • The interaction and communication among classes
  • Example A class vehicle communicates with the
    class road via public methods

9
Aggregation
  • Aggregation describes the containment
    relationship among classes.

Vehicle
Owns
Chassis
Engine
wheel
Owns or Contains
Starter
Battery
10
Aggregation ()
  • All objects, which are contained in a class A,
    must be initialized before entering the body of
    the A constructor. That is possible by listing
    the constructors of the contained objects in an
    initialization list next to the constructor
    header of the owner class.
  • NOTE Any constructor can have an initialization
    list that is executed before applying the
    instructions in its body. Here is an example of a
    constructor having an initialization list
  • Class Example
  • int a, b, c
  • Example(int k)
  • ExampleExample(int k) a(5), b(10)
  • c k

11
Example Aggregation
class Y public Y() Y(int) class
C protected int c Y y public
C() C(int) C operator(const C)
C()
12
YY() cout ltlt "YY()" ltlt endl YY(int)
cout ltlt "YY(int)" ltlt endl CC() y(2),
c(5) cout ltlt "CC()" ltlt endl CC(int
i) y(3), c(j) cout ltlt "CC(int)" ltlt endl
C Coperator(const C v) c v.c y
v.y cout ltlt "Coperator" ltlt endl return
this CC() cout ltlt "CC()" ltlt endl

13
Aggregation ()
  • If the constructor of a contained object Y is not
    specified in the constructors initialization
    list of the container class X, the default
    constructor of Y gets called before entering the
    constructor of X.
  • The programmer avoids the default constructor by
    listing the appropriate constructor in the
    initialization list to force the compiler to use
    it.
  • if a default constructor does not exist, and the
    the object is not initialized in the
    initialization list, the compiler will complain.
    To work around this limitation, pointers can be
    used to declare objects. In this case, the
    compiler will not require the initialization of
    contained objects before entering the
    constructors body.

14
Example Aggregation using pointers
class C protected int c Y y
public C() C(int) C
operator(const C) C() CC()
c(5) y new Y(2) CC(int i)c(j) y
new Y(3)
15
C Coperator(const C v) c v.c delete
y y new Y((v.y)) return this
CC() delete y y NULL
16
External Aggregation
  • An aggregated object (owned) might not be
    created by the aggregator object (owner). In some
    cases, an object might be owning other objects
    that are created outside its scope and passed by
    reference to it. We call this form of aggregation
    external.
  • External aggregation is achieved by declaring a
    pointer or reference to the contained class.
  • If the external aggregation is achieved by
    declaring a reference, the reference must be
    initialized in the constructors initialization
    list. Otherwise (using a pointer), the
    initialization can be specified in the body of
    the constructor.
  • The following examples provide more illustration

17
// Example External aggregation using
reference class Truck private Trailer
trl int serialNum int
colour public Truck(Trailer aTrl, int
aSerialNum, int aColour) trl(aTrl)
serialNumaSerialNum colour_aColour

// Example External aggregation using pointers
(1) class Truck private Trailer trl
int serialNum int colour public
Truck(Trailer aTrl, int aSerialNum, int
aColour) serialNumaSerialNum
colour_aColour trl aTrl
//Example External aggregation using pointers
(2) class Truck private Trailer trl
int serialNum int colour public
Truck(Trailer aTrl, int aSerialNum, int
aColour) serialNumaSerialNum
colour_aColour trl aTrl
18
Aggregations Access
  • If a member function has to return a contained
    object with read only limitation, the simplest
    method is to return a constant reference to that
    object.

/ Access for an aggregated object/ class Truck
private Trailer trl const Trailer
TruckGetTrailer(void) return trl
/ Access for an aggregated object (reference)
/ class Truck private Trailer trl
const Trailer TruckGetTrailer(void)
return trl
/ Access for an aggregated object (pointer)
/ class Truck private Trailer trl
const Trailer TruckGetTrailer(void)
return trl
19
Generalization
  • When multiple objects share some similarities, it
    is always wise to to have a general object, that
    encapsulates the similar characteristics, and
    acts as the base class for these objects. The
    classes that evolve from a base class are said to
    be inheriting its characteristics.
  • For example the class motorbike, truck, bus, and
    car have many commonalities and some differences.
    Some of the commonalities are engine, chassis,
    and wheels, etc It is advisable to encapsulate
    the common attributes in one class, called
    vehicle, from which the other classes evolve, or
    inherit.
  • Saying a car is a vehicle means that it inherits
    the attributes of the class vehicle.
  • The generalization process is needed in order to
    build an effective inheritance model.

20
Inheritance
  • A specific class evolves from or inherits the
    characteristics of a more general class, to which
    it adds its own characteristics. Inheritance can
    be hierarchical.
  • In practical terms
  • The more general class in an inheritance
    hierarchy is known as the base class or
    super-class with respect to the other classes
    that inherit from it.
  • The more specific class in a inheritance
    hierarchy is known as the derived class or
    sub-class with respect to the other class from
    which it inherits

21
Inheritance Hierarchy
Base Class
Vehicle
Inherits
Derived Class
Truck
Car
Bus
Toyota
Ford
Inherits
22
Inheritance ()
  • Talking about inheritance leads to polymorphism.
    Polymorphism in a derived class is the act of
    overloading a method defined in a base class. It
    is the hiding of a functions behaviour specified
    by the super-class with a new behaviour
    introduced in the sub-class.
  • For example the class plane and car are both
    vehicles and inherits the characteristics of the
    class vehicle. vehicle has a function called
    move() to change location from one point to
    another. Since a car drives and a plane flies,
    move() has to be overloaded in both classes.

vehicle move()
plane move() .. fly() ..
car move() .. drive() ..
23
Inheritance ()
  • Inheritance Syntax in C
  • DerivedClassName publicprivate BaseClass1
  • ,publicprivate BaseClassN1..n
  • class declaration
  • Examples
  • class truck public vehicle
  • class tv public vedio, public audio
  • class toyota private car

24
Inheritance Modifiers
Recap of Access Modifiers A private data member
is not accessible from outside its class, not
even from an inheriting sub-class. On the other
hand, a protected member is not accessible from
outside a class, but can be manipulated within
the scope of an inheriting sub-class. The
following table summarizes the access modifiers
used Access Visibility Visibility
Modifier (From Sub-Class) (From
the outside) private No
No protected Yes No public
Yes Yes
25
Inheritance Modifiers ()
The public and private access modifiers can be
used with inheritance to specify the
accessibility of the members of a base class
inside the scope of a derived class. For
example Class X private Y //defines a
derived class X privately inheriting
from base class Y. The following table describes
the members accessibility of a public or private
base class within a derived class
26
Derived Class Construction and Destruction
  • The constructor of a derived class automatically
    calls the constructor of its base class before
    initializing its own attributes. Similarly, the
    destructor of a base class gets automatically
    executed by the destructor of a derived class.
  • The call for a base class constructor can be
    specified in the constructors initialization
    list of the derived class alternatively, the
    compiler calls the default constructor instead of
    specifying one in the initialization list. If no
    default constructor exists, the compiler will
    complain.

27
Abstract Class
  • A class having at least one pure virtual
    function is said to be an abstract class. An
    abstract class cannot be instantiated. Its sole
    purpose is to serve as a base class for more
    specific sub-classes.
  • A pure virtual function is a function that has no
    implementation. It is defined by replacing the
    functions body with the symbols 0.
  • Class X
  • virtual foo() 0
  • // foo() is pure virtual, and X is
    abstract
  • A class having no pure function is a concrete
    class that can be instantiated.
  • Back to our vehicle example, it makes sense to
    make the class vehicle abstract since a vehicle
    does not exist by itself. It is rather an
    abstract description of concrete classes such as
    car, truck, and plane.
Write a Comment
User Comments (0)
About PowerShow.com