1 / 27

Classes - Part II

Classes - Part II. Constructors Member Functions Inheritance. Classes. Structured Programming 256 Chapter 9. Class Implementation. A constructor is a special member function provided to allow initialization of variables. Constructors.

Download Presentation

Classes - Part II

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. Classes - Part II Constructors Member Functions Inheritance

  2. Classes Structured Programming 256 Chapter 9

  3. Class Implementation • A constructor is a special member function provided to allow initialization of variables.

  4. Constructors • constructor initializes private data when a class object is created • ExampleTimeType(int, int, int); • Student(double, char, char, char, char);

  5. Default Constructors • default constructor parameterless constructor, it initializes the variables to some default value, such as zero • ExampleTimeType(); • Student();

  6. same Default Constructor Function Syntax Classname::Classname() { function body } *

  7. Constructor Functions • do NOT have a return type (not even void) • must have the same name as the class itself • invoked when you declare an instance of a class with a list of initial values • there should be a default constructor * * * *

  8. Default Constructor Function • Example 1 • TimeType::TimeType( ) • { • hrs = 0; • mins = 0; • secs = 0; • } Example 2 Student::Student( ) { GPA = 0; num_of_grades = 130; } * *

  9. Default Constructor Function Example 3 Circle::Circle( radius) { radius_of circle = radius; x_center = 5; y_center = -3; } Example 4 Student::Student( ) { } * *

  10. Destructors • ~Classname( ) • A default do-nothing destructor is provided by the compiler. • Only one destructor per function class • No arguments - no return values

  11. Some Terminology • OOP C++ • object class object or class instance • instance variable private data member • method public member function • message passing function call (to a public member function)

  12. h file Review - In Specification File • default constructorClassname();Track(); • constructorClassname(typepara, typepara , ...); Track(int disc, double hurdles); • member functiontype Function(typepara , typepara , ...);void Event(double hurdles); * * *

  13. Review - In Implementation File default constructorClassname::Classname() { } Track::Track() { } • constructorClassname::Classname(typepara, typepara , ...) Track::Track(int disc, double hurdles) • member functiontype Function(typepara , typepara , ...)void Event(double hurdles) all have a body * *

  14. Review - In Client File • default constructor If NOT passed parameters - gets called when declaring an object of type Class. • constructor If passed parameters - gets called when declaring an object of type Class. • member functionobject.Function(para , para , ...);b_jenner.Event(double hurdles); * * *

  15. Inheritance • creating new classes out of existing ones • reuse code without need for retesting and validation • speeds up programming process • simple vs. multiple

  16. Inheritance - simple Circle base class: derived class: derived class: Sphere Cylinder *

  17. Inheritance - multiple base classes: derived class: Car Truck Minivan *

  18. Constructor Function Call • SyntaxClassnameobject(arguments); • orClassnameobject = Classname(arguments); * *

  19. } Constructor Function Call • Example Circle big_circ(0, 2. 3.6); Circle big_circ = Circle(2, 2, 3.6); • Circle big_circ( ); Circle big_circ; * * * *

  20. Overload Constructors Date me; Date you(7,25,49); Date moon(690628); • You must provide constructors for each. • PrototypesHeaders • Date( ); Date::Date() • Date(int, int, int); Date::Date(int, int, int) • Date(long) ; Date::Date(long)

  21. Class Implementation:Function Syntax • Exampledouble Circle::setVal(int xx, int yy, double rr) • { xcenter = xx; ycenter = yy; radius = rr; } * *

  22. Function Call • Syntax object.function(arguments); Examplea.setVal( ); b.setVal(3, 2, 6.5); big_circ.setVal(1, 4, 8.3) sm_circ.setVal( ); k.setVal(1, 0, 5.0); * *

  23. Initialize Data Membersof a Class • double checkAmt; • checkAmt = 54.78; Student iago; iago.ID = 12345; * *

  24. Arrays of Objects • Date theDate[4]; • creates the objects: • theDate[0] • theDate[1] • theDate[2] • theDate[3]

  25. Arrays of Objects • Function calls: • thedate[0].showdata(); • or • for(ndx=0; ndx<4; ndx++) • theDate[ndx].showdata();

  26. Common Errors • missing ; at end of class declaration • including a return type with constructor’s prototype • missing a return type with function prototypes • defining more than one default constructor per class

  27. Common Errors • missing :: and class name in header • forgetting to include the appropriate header files

More Related