Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors Member Functions Inheritance

Similar presentations


Presentation on theme: "Constructors Member Functions Inheritance"— Presentation transcript:

1 Constructors Member Functions Inheritance
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 Example TimeType(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 Example TimeType(); Student();

6 Default Constructor Function
same 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 Review - In Specification File
default constructor Classname(); Track(); constructor Classname(type para, type para , ...); Track(int disc, double hurdles); member function type Function(type para , type para , ...); void Event(double hurdles); h file * * *

13 Review - In Implementation File
constructor Classname::Classname(type para, type para , ...) Track::Track(int disc, double hurdles) member function type Function(type para , type para , ...) void Event(double hurdles) default constructor Classname::Classname() { } Track::Track() { } 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 function object.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: Sphere Cylinder
*

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

18 Constructor Function Call
Syntax Classname object(arguments); or Classname object = Classname(arguments); * *

19 Constructor Function Call
Example Circle big_circ(0, ); 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. Prototypes Headers Date( ); Date::Date() Date(int, int, int); Date::Date(int, int, int) Date(long) ; Date::Date(long)

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

22 Function Call Syntax object.function(arguments);
Example a.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 Members of a Class
Student iago; iago.ID = 12345; double checkAmt; checkAmt = 54.78; * *

24 Arrays of Objects Date theDate[4]; creates the objects: theDate[0]

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


Download ppt "Constructors Member Functions Inheritance"

Similar presentations


Ads by Google