1 / 23

CIS 230

CIS 230. 29-Mar-06. Quiz. Write a class declaration (what would go in our Coord.h file…) for the class Coord that contains: T private floating-point data members named xval and yval . S Constructor function that does not receive any arguments.

regina
Download Presentation

CIS 230

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 230 29-Mar-06

  2. Quiz • Write a class declaration (what would go in our Coord.h file…) for the class Coord that contains: • T private floating-point data members named xval and yval. • S Constructor function that does not receive any arguments. • A display function named printCoord() that does not accept or return any arguments. • A friend function named ConvPolar() that accepts two floating point numbers and returns a Coord.

  3. Overview • Cast • Inheritance • Protected • Inheritance Overriding • #include “ “ • File Management

  4. Cast • Temporarily converts one data type to another • Example: int first, second; float answer; … answer = (first + second) / 2; answer = (first + second) / 2.0; answer = ( (float) first + (float) second ) / 2; Integer division Real division Real Division Cast to a float

  5. Cast • Two different ways to cast: • (float) first • float (first) • Example • answer = first / second; • Integer division • Can’t just add a decimal point • answer = (float) first / (float) second;

  6. Inheritance • A class may be defined in terms of a previously defined class • This new class has all the components of its parent class: • Data • Methods • The new class may then add features of its own.

  7. quadrilateral parallelogram trapezoid rectangle rhombus Inheritance • Each class may have many direct descendants Rhombus “inherits” all the aspects of parallelogram AND ALSO all the aspects of quadrilateral

  8. Subclass form class Name : access Parent_class { … … }; Usually Public

  9. Circle & Cylinder Circle Cylinder Sphere

  10. Circle class class Circle { private: float radius; public: void store_radius(float); float calc_circum(); float calc_area(); float return_radius(); };

  11. Cylinder class class Cylinder : public Circle { private: float length; public: void store_length(float); float clac_volume(); float return_length(); };

  12. Cylinder member functions void Cylinder::store_length(float lnth) { length = lnth; } float Cylinder::calc_volume(void) { return (length * calc_area()); } Invokes on of its “parent’s” methods

  13. Main() int main() { Cylinder cyl; float answer; cyl.store_radius(3); cyl.store_length(10); answer = cyl.calc_volume(); cout << "The volume of the cylinder is " << answer << '\n'; return 0; }

  14. Problem • Cylinder HAS a radius (inherited) • Cylinder cannot directly touch it float Cylinder::calc_volume() { return (3.14 * radius * radius * length); } This is cylinder’s radius, but it was declared private in Circle

  15. Protected • Protected – A new access form • Pivate to the outside world • Allows descendent classes access • Use in the original class class Circle { protected: float radius; public: … }; It’s important to PLAN your class hierarchy

  16. Inheritance Overriding • To override a parent method: • Write a new method with the same name • Example: • Assume we want Cylinder’s area to be its entire surface area

  17. Cylinder class class Cylinder : public Circle { private: float length; public: void store_length(float); float clac_volume(); float return_length(); float calc_area(); } Same name as in Circle

  18. Cylinder::calc_area() float cylinder::calc_area() { float end, side; end = 3.14 * radius * radius; side = 2 * 3.14 * radius * length; return (2 * end + side); } This creates a problem in cylinder’s original calc_volume().

  19. Cylinder::calc_volume() float Cylinder::calc_volume() { return (length * Circle::calc_area()); } Uses the parent method

  20. #include “ “ • Classes (or other functions) may be used by other programs • Important to have a good class hierarchy • Important to have private/protected/public set correctly • #include “filename.ext” • Searches in the same directory as the program being compiled • Source code that will be compiled with the program • Should be thoroughly tested

  21. We already know… • Circle.h contains: class Circle { protected: //variable declarations public: //method prototypes } • Circle.cpp would contain: • #include “circle.h” • method definitions

  22. Descending with #include “ “ • In Cylinder.h: • #include “circle.h” • In main.cpp • #include “cylinder.h”

  23. File Management circle.h cylinder.h #include “circle.h” main.cpp #include “cylinder.h” circle.cpp #include “circle.h” cylinder.cpp #include “cylinder.h”

More Related