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 Class Implementation A constructor is a special member function provided to allow initialization of variables.

3 Constructors constructor initializes private data when a class object is created Example TimeType(int, int, int); Student(double, char, char, char, char);

4 Default Constructors default constructor parameterless constructor, it initializes the variables to some default value, such as zero Example TimeType(); Student();

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

6 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 * * * *

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

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

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

10 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)

11 Review - In Specification File
h 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); * * *

12 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 * *

13 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); * * *

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

15 Inheritance - simple Circle base class: derived class: Sphere Cylinder
*

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

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

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

19 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)

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

21 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); * *

22 Initialize Data Members of a Class
double checkAmt; checkAmt = 54.78; Student iago; iago.ID = 12345; * *

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

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

25 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

26 Common Errors missing :: and class name in header
forgetting to include the appropriate header files

27 Happiness, like most of the other important processes of life, cannot be planned.


Download ppt "Constructors Member Functions Inheritance"

Similar presentations


Ads by Google