27
Classes - Part II Constructors Member Functions Inheritance

Classes - Part II

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

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

• constructorinitializes private data when a class

object is created

• ExampleTimeType(int, int, int);

• Student(double, char, char, char, char);

Default Constructors

• default constructorparameterless constructor, it

initializes the variables to some default value, such as zero

• ExampleTimeType();

• Student();

Default Constructor Function

*

SyntaxSyntaxClassnameClassname::::ClassnameClassname(( )){{

function bodyfunction body}}

samesame

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

* * * *

Default Constructor Function

•Example 1• TimeType::TimeType( )• {• hrs = 0;• mins = 0;• secs = 0;• }Example 2Example 2

StudentStudent::::Student( )Student( ){{

GPA = 0;GPA = 0;num_of_grades = 130;num_of_grades = 130;

}}* *

Default Constructor Function

* *

Example 3Example 3CircleCircle::::Circle( radius)Circle( radius){{

radius_of circle = radius;radius_of circle = radius;x_center = 5;x_center = 5;y_center = -3;y_center = -3;

}}

Example 4Example 4StudentStudent::::Student( )Student( ){ { }}

Destructors

• ~Classname( )

A default do-nothing destructor is provided by the compiler.

Only one destructor per function class

No arguments - no return values

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)

Review - In Specification File

• default constructorClassname();Track();

• constructorClassname(type para, type para , ...);Track(int disc, double hurdles);

• member functiontype Function(type para , type para , ...);void Event(double hurdles);

* * *

h file

Review - In Implementation File

• constructorClassname::Classname(type para, type para , ...)Track::Track(int disc, double hurdles)

• member functiontype Function(type para , type para , ...)void Event(double hurdles)

all have a body

* *

default constructordefault constructorClassname::Classname() { }Classname::Classname() { }Track::Track() { }Track::Track() { }

Review - In Client File

• default constructorIf NOT passed parameters - gets calledwhen 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);

* * *

Inheritance

creating new classes out of existing ones

reuse code without need for retesting and validation

speeds up programming process

simple vs. multiple

CircleCircle

base class:

derived class:

derived class:

SphereSphere CylinderCylinder

Inheritance - simple

*

baseclasses:

derived class:

Inheritance - multiple

CarCar

MinivanMinivan

TruckTruck

*

Constructor Function Call

• Syntax

Classname object(arguments);

• or

Classname object = Classname(arguments);

* *

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;Circle big_circ;

* * * *

Overload Constructors

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

Date me;Date you(7,25,49);Date moon(690628);

Class Implementation:Function Syntax

• Example double Circle::setVal(int xx, int yy, double rr)

• {xcenter = xx;ycenter = yy;radius = rr;

}

* *

• Syntaxobject.function(arguments);

Function Call

* *

ExampleExamplea.setVal( );a.setVal( );b.setVal(3, 2, 6.5);b.setVal(3, 2, 6.5);big_circ.setVal(1, 4, 8.3)big_circ.setVal(1, 4, 8.3)sm_circ.setVal( );sm_circ.setVal( );k.setVal(1, 0, 5.0);k.setVal(1, 0, 5.0);

Initialize Data Membersof a Class

• double checkAmt;

• checkAmt = 54.78;

* *

Student iago;Student iago;

iago.ID = 12345;iago.ID = 12345;

Arrays of Objects

• Date theDate[4];

• creates the objects:

• theDate[0]

• theDate[1]

• theDate[2]

• theDate[3]

Arrays of Objects

• Function calls:

• thedate[0].showdata();

• or

• for(ndx=0; ndx<4; ndx++)

• theDate[ndx].showdata();

Common ErrorsCommon Errors

missing ; at end of class declaration

including a return type with constructor’s prototype

missing a return type with functionprototypes

defining more than one default constructorper class

Common ErrorsCommon Errors

missing :: and class name in header

forgetting to include the appropriate header files