• No results found

The story so far ...

N/A
N/A
Protected

Academic year: 2022

Share "The story so far ..."

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

CS101 Computer Programming and Utilization

Milind Sohoni

June 4, 2006

(2)

1 So far

2 What is a class

3 A simple class

4 The structure ofclass

(3)

The story so far ...

We have seen various control flows.

We have seen multi-dimensional arrays and thechardata type.

We saw the use of functions and calling methods.

This week...

Introduction to Classes

(4)

Class

The basic objective of classes were

to generalize type declarations such as int,floatto user-defined types such aspoly, matrix.

to separate the user from the implementer. Thus, e.g., I may writepolynomials class and various operations on it such asevaluation,

differentiationetc. Any other user may use my polynomial definition. She must however use it only through the operations that I allow.

(5)

Class

The basic objective of classes were

to generalize type declarations such as int,floatto user-defined types such aspoly, matrix.

to separate the user from the implementer. Thus, e.g., I may writepolynomials class and various operations on it such asevaluation,

differentiationetc. Any other user may use my polynomial definition. She must however use it only through the operations that I allow.

TheClassframework is a special feature of C++.

The class definition contains of two main chunks:

I Theprivatedefinition (or member variables),

I Thepublicoperations.

Theprivatecconcerns details about the representation, Thepublicdefines operations which are exposed to the outside.

(6)

The poly.cpp class

#include <iostream.h>

#include <math.h>

class poly {

private:

float coefs[10];

int degree;

public:

void ReadIn(void);

int deg(void);

float eval(float);

// poly diff(void);

};

void poly::ReadIn(void) {

cin>> degree;

if (degree >9)

cout << "degree bounded by 9";

for (int i=0;i<=degree;i=i+1) cin >> coefs[i];

}

int poly::deg(void) {

return degree;

}

float poly::eval(float x) {

float r;

r=0;

for (int i=0;i<=degree;i=i+1) r=r+pow(x,i)*coefs[i];

return r;

; }

(7)

The poly.cpp class

class poly {

public:

void ReadIn(void);

int deg(void);

float eval(float);

// poly diff(void);

};

int main() {

poly p;

p.ReadIn();

cout << p.eval(1.0);

}

[sohoni@nsl-13]$ ./a.out 3

1 2 3 4

10

Thusp.ReadIn()causes the construction of the polynomial

p= 1 + 2x+ 3x2+ 4x3 The next statement

p.eval(1.0)evaluates it at x= 1.

(8)

The class structure

The basic structure of a program using classes is as follows:

# include ...

class classname {

private ...;

public ...;

};

void classname::function {

body }

The class definition has two parts and must be done before the main program begins.

The Declaration, which declare the class name, the private ormembervariables and themethods, which are the public ways of accessing objects.

The Methods, which are the definition of the functions which operate on the local variables. These are the only methods by which an outer program may access the member variables.

(9)

Uses

The basic structure of a program using classes is as follows:

# include ...

class classname {

private ...;

public ...;

};

void classname::function {

body }

The main uses of classes are:

As opposed tostruct, aclass allows not only data but procedures which manipulate this data.

This allows a separation: the person who writes the class may be different from the one who uses it.

Classes allow us to

remember data: coefs read only once, but polynomial evaluated repeatedly.

(10)

Root finding again

The Newton-Raphson technique:

Use the function value and its derivative to compute the next candidate.

x(i) x(i+1)

f(x)

(11)

Root finding again

The Newton-Raphson technique:

Use the function value and its derivative to compute the next candidate.

x(i) x(i+1)

f(x)

xi+1 =xi−(f(xi)/f0(xi)).

(12)

Root finding again

The Newton-Raphson technique:

Use the function value and its derivative to compute the next candidate.

x(i) x(i+1)

f(x)

xi+1 =xi−(f(xi)/f0(xi)).

We will upgrade outpoly class to include

differentiation.

(13)

newraph.cpp

class poly {

private:

float coefs[10];

int degree;

public:

void ReadIn(void);

int deg(void);

float eval(float);

poly diff(void);

};

This allows yet anotherpublic function calledp.diff()on a polynomial p.

(14)

newraph.cpp

class poly {

private:

float coefs[10];

int degree;

public:

void ReadIn(void);

int deg(void);

float eval(float);

poly diff(void);

};

This allows yet anotherpublic function calledp.diff()on a polynomial p.

And here is the definition of p.diff.

poly poly::diff(void) {

poly q;

q.degree=degree-1;

for (int i=0;i<=q.degree;

i=i+1) q.coefs[i]=

(i+1)*coefs[i+1];

return q;

}

(15)

newraph.cpp

int main() {

poly p,q;

p.ReadIn();

q=p.diff();

cin >> x >> tol;

fval=p.eval(x);

while ((fabs(fval)>tol)

&& (count<1000)) {

der=q.eval(x);

x=x-fval/der;

count=count+1;

fval=p.eval(x);

};

cout << ...

Whats happening?

The polynomialpis read in.

The initial guess valuex and tol is next read.

Notice howq, the derivative ofpis craeted.

(16)

newraph.cpp

int main() {

poly p,q;

p.ReadIn();

q=p.diff();

cin >> x >> tol;

fval=p.eval(x);

while ((fabs(fval)>tol)

&& (count<1000)) {

der=q.eval(x);

x=x-fval/der;

count=count+1;

fval=p.eval(x);

};

cout << ...

}

Whats happening?

The polynomialpis read in.

The initial guess valuex and tol is next read.

Notice howq, the derivative ofpis craeted.

Next the loop is set-up and the iterations begin.

Notice howqis evaluated finally, outputs are generated.

(17)

Whats the big deal?

2 -1 0 1

-1.5 0.0000000001

Thusp=x2−1, the initial gues is−1.5 and the tolerance is 1010!

(18)

Whats the big deal?

2 -1 0 1

-1.5 0.0000000001

Thusp=x2−1, the initial gues is−1.5 and the tolerance is 1010!

[sohoni@nsl-13]$ ./a.out <input final root -1

iterations 4 Just 4 iterations.

(19)

Whats the big deal?

2 -1 0 1

-1.5 0.0000000001

Thusp=x2−1, the initial gues is−1.5 and the tolerance is 1010!

[sohoni@nsl-13]$ ./a.out <input final root -1

iterations 4 Just 4 iterations.

3

0 -1 0 1

-1.5 0.0000000001

Thusp=x3−x, the initial gues is−1.5 and the tolerance is 1010!

(20)

Whats the big deal?

2 -1 0 1

-1.5 0.0000000001

Thusp=x2−1, the initial gues is−1.5 and the tolerance is 1010!

[sohoni@nsl-13]$ ./a.out <input final root -1

iterations 4 Just 4 iterations.

3

0 -1 0 1

-1.5 0.0000000001

Thusp=x3−x, the initial gues is−1.5 and the tolerance is 1010!

[sohoni@nsl-13]$ ./a.out <input final root -1

iterations 5 5 iterations.

(21)

Assignment

Write a function on poly to evaluate thek-th derivative at the pointx. Use this function to writep.multiplyby(q)which multiplies polynomialpbyq (and stores it inp).

Now writep.DivideBy(q)and implement polynomial long division.

References

Related documents

However, with India constituting about 11% of global deaths in road accidents, it can be discerned that efforts towards ensuring road safety could be improved on all

• policy planning, land reform, natural resources management, climate change, agricultural production, value chains development, employment creation and food

Some of the key ideas envisaged in the report include the development of a seamless river transport system, a 4000-km-long ring road connecting all the north eastern

Corporations such as Coca Cola (through its Replenish Africa Initiative, RAIN, Reckitt Benckiser Group and Procter and Gamble have signalled their willingness to commit

6851 Loans for Village and Small Industries 6860 Loans for Consumer Industries 6875 Loans for Other Industries 7053 Loans for Civil Aviation 7055 Loans for Road Transport.. 7610

4711 Capital Outlay on Flood Control Projects 4801 Capital Outlay on Power Projects 4852 Capital Outlay on Industries. 4860 Capital Outlay on Consumer Industries 4875 Capital Outlay

4860 Capital Outlay on Consumer Industries 4875 Capital Outlay on Other Industries 5053 Capital Outlay on Civil Aviation 5054 Capital Outlay on Roads and Bridges 5055 Capital Outlay

INDEPENDENT MONITORING BOARD | RECOMMENDED ACTION.. Rationale: Repeatedly, in field surveys, from front-line polio workers, and in meeting after meeting, it has become clear that