cis 230 29-mar-06. quiz write a class declaration (what would go in our coord.h file…) for the...

23
CIS 230 29-Mar-06

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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.– 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.

Overview

• Cast

• Inheritance

• Protected

• Inheritance Overriding

• #include “ “

• File Management

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

Cast to a float Real Division

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;

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.

Inheritance

• Each class may have many direct descendants

quadrilateral

parallelogram trapezoid

rhombus rectangle

Rhombus “inherits” all the aspects of parallelogram

AND ALSO all the aspects of quadrilateral

Subclass form

class Name : access Parent_class

{

};

Usually Public

Circle & Cylinder

Circle

Cylinder Sphere

Circle class

class Circle{

private:float radius;

public:void store_radius(float);float calc_circum();float calc_area();float return_radius();

};

Cylinder class

class Cylinder : public Circle{

private:float length;

public:void store_length(float);float clac_volume();float return_length();

};

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

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;}

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

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

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

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

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().

Cylinder::calc_volume()

float Cylinder::calc_volume()

{

return (length * Circle::calc_area());

} Uses the parent method

#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

We already know…

• Circle.h contains:class Circle {

protected://variable declarations

public://method prototypes

}• Circle.cpp would contain:

– #include “circle.h”– method definitions

Descending with #include “ “

• In Cylinder.h:– #include “circle.h”

• In main.cpp– #include “cylinder.h”

File Management

circle.h

circle.cpp#include “circle.h”

cylinder.h#include “circle.h”

cylinder.cpp#include “cylinder.h”

main.cpp#include “cylinder.h”