miscellaneous jpc and jwd © 2002 mcgraw-hill, inc
TRANSCRIPT
![Page 1: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/1.jpg)
Miscellaneous
JPC and JWD © 2002 McGraw-Hill, Inc.
![Page 2: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/2.jpg)
Miscellaneous Topics
Inheritance more coming up ..
Enumerated Types (enums) enum Color {RED, BLUE, ..}; Color c = RED;
pred? expr1 : expr2 (a==2)? 5 : (D*4)
extern declaration extern int numEntries; // in every file where
numEntries is used int numEntries; // declare this in just one file int f(); // equivalent to extern declaration for a function
![Page 3: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/3.jpg)
Misc. Topics (Cont.)
static static global variables
static int foobar; // Available only to this file static local variables
like a global variable, but accessible only to the function where it is declared
e.g. int f () { static int numCalls = 0; numCalls ++ }
Coding style See CS101 home page
![Page 4: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/4.jpg)
Ability to define new classes of objects using existing classes as a basis
Inheritance
Ability to define new classes of objects using existing classes as a basis
The new class inherits the attributes and behaviors of the parent classes
Ability to define new classes of objects using existing classes as a basis
The new class inherits the attributes and behaviors of the parent classes
New class is aspecialized versionof the parent class Bicycle
MountainBikes
RacingBikes
TandemBikes
is-a relationships
![Page 5: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/5.jpg)
A natural way to reuse code Programming by extension rather than reinvention Object-oriented paradigm is well-suited for this style
ofprogramming
Inheritance
Bicycle
MountainBikes
RacingBikes
TandemBikes
is-a relationships
A natural way to reuse code Programming by extension rather than reinvention Object-oriented paradigm is well-suited for this style
ofprogramming
Terminology Base class (superclass) Derived class (subclass)
![Page 6: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/6.jpg)
class RectangleShape {public:
RectangleShape(SimpleWindow &W, float XCoord, float YCoord, const color &Color, float Width, float Height);void Draw();color GetColor() const;void GetSize(float &Width, float &Height) const;void GetPosition(float &x, float &y) const;float GetWidth() const;float GetHeight() const;SimpleWindow& GetWindow() const;void SetColor(const color &Color);void SetPosition(float x, float y);void SetSize(float Width, float Height);
private:SimpleWindow &Window;float XCenter;float YCenter;color Color;float Width;float Height;
};
Before Inheritance
![Page 7: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/7.jpg)
Before Inheritanceclass CircleShape {
public:CircleShape(SimpleWindow &W, float x, float y, const color &Color, float Diameter);void Draw();color GetColor() const;float GetSize() const;void GetPosition(float &x, float &y) const;SimpleWindow& GetWindow() const;void SetColor(const color &Color);void SetPosition(float x, float y);void SetSize(float Diameter);
private:SimpleWindow &Window;float XCenter;float YCenter;color Color;float Diameter;
};
![Page 8: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/8.jpg)
ShapesHierarchy
C: ShapeDM: Color
MF: GetColor(), SetColor()
C: RectangleShapeDM: Width, Height
MF: Draw(), GetWidth(),GetHeight(), SetSize()
C: TriangleShapeDM: SideLength
MF: Draw(),GetSideLength(),
SetSize()
C: WindowObjectDM: Location, Window
MF: GetPosition(), GetWindow(), SetPosition()
C: Label
C: EllipseShapeDM: Width, Height
MF: Draw(),GetWidth(),
GetHeight(), SetSize()
![Page 9: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/9.jpg)
Class WindowObject
class WindowObject {
public:
WindowObject(SimpleWindow &w,
const Position &p);
Position GetPosition() const;
SimpleWindow& GetWindow() const;
void SetPosition(const Position &p);
private:
SimpleWindow &Window;
Position Location;
};
![Page 10: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/10.jpg)
WindowObject Constructor
WindowObject::WindowObject(SimpleWindow &w,
const Position &p) : Window(w), Location(p) {
// No body needed
}
Members are initializedin class definition order
![Page 11: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/11.jpg)
Defining a Derived Class
class DerivedClass : public BaseClass { public:
// public section ... private:
// private section ...};
Derived class name
Access specifier(usually public)
Class name ofbase class
![Page 12: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/12.jpg)
Declaring a Derived Class
class Shape : public WindowObject {
public:
Shape(SimpleWindow &w,
const Position &p,
const color &c = Red);
color GetColor() const;
void SetColor(const color &c);
private:
color Color;
};
Read this as Shape is a kind of WindowObject
Shape inherits WindowObjectmembers Window, Location,GetPosition(), GetWindow(),and SetPosition()
![Page 13: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/13.jpg)
Implementing A Derived Class Constructor
Derivedclassname
Derivedclass
constructorparameter
list
Baseclassname
Base classconstructor
parameter list(sublist of PList)
Derived cass datamember initialization list
(sublist of PList)
DClass::DClass(PList) : BClass(BList), DMbrList {// Body of derived class constructor...
};
![Page 14: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/14.jpg)
Implementing a Derived Class
Shape::Shape(SimpleWindow &w, const Position &p, const color &c) : WindowObject(w, p), Color(c) { // No body needed}
color Shape::GetColor() const {return Color;
}
void Shape::SetColor(const color &c) {assert(c >= 0 && c < MaxColors);Color = c;
}
![Page 15: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/15.jpg)
Inheritance and Member Access
class BaseClass {public: int MyPublicData;protected: int MyProtectedData;private: int MyPrivateData;
};
class DerivedClass : public BaseClass {public: void DerivedClassFunction();// ...
};
void DerivedClass::DerivedClassFunction() {MyPublicData = 1; // access allowedMyProtectedData = 2; // access allowedMyPrivateData = 3; // illegal
}
![Page 16: Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e235503460f94b11845/html5/thumbnails/16.jpg)
Inheritance Related Buzzwords
Don’t worry about these for now, but read about them in your holidays if you are interested
virtual function abstract base class late binding