cs31 lecture 17

printableconst printable p printableconst do nothing

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ome syntax x Class A inherits from class B struct A : B { }; x Class A inherits from classes B and C struct A : B, C { }; x Call a method foo()
 from the parent class B from class A B::foo()
 foo()
 if only one parent defines foo()
 (no ambiguity)
 127 The hierarchy of objects x We note A : B if A inherits from B x A function taking as argument an object of type B can accept an function object of type B or any of its subclass (e.g., A)
 object x The converse is not true example: example: void foo(B b)
 { … } void void bar(A a)
 { … } A myobj; myobj; B myobj2; myobj2; foo(myobj)
; // this works foo(myobj)
; bar(myobj2)
; // this does not work! bar(myobj2)
; 128 Copy constructor x Special kind of constructor (it is a constructor!)
 x Is called to create a new copy of an object from another Is object object x Compilers always add a default one if missing x Must have a precise syntax ClassName([const] ClassName& obj)
 { … } x If the copy constructor does not modify the object to be If copied, systematically put the const qualifier copied, x It is often used in case of using the operator = 129 Defining a Class x Figure out the class name (say, ClassName)
, its attributes Figure and methods and x Always define the basic constructor ClassName()
 { … } x Always define the basic destructor ~ClassName()
 { …} x Always define the basic copy const...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online