• No se han encontrado resultados

Propedéutico de Programación

N/A
N/A
Protected

Academic year: 2024

Share "Propedéutico de Programación"

Copied!
82
0
0

Texto completo

(1)

Propedéutico de Programación

Coordinación de Ciencias Computacionales 2/13

Material preparado por:

Dra. Pilar Gómez Gil

(2)

Chapter 10

Simple Data Types: Built-In and User-Defined

Dale/Weems

(3)

C++ Simple Data Types

simple types

integral floating

char short int long bool enum float double long double

unsigned

(4)

Some Integral Types

Type Size in Bytes Minimum Value Maximum Value

char 1 -128 127

short 2 -32,768 32,767

int 2 -32,768 32,767

long 4 -2,147,483,648 2,147,483,647

NOTE: Values given for one machine; actual sizes are machine-dependent

(5)

Operator sizeof

sizeof A C++ unary operator that yields the size on your

machine, in bytes, of its single operand. The operand can be a variable name, or it can be the name of a data type enclosed in parentheses.

int age;

cout << “Size in bytes of variable age is “ << sizeof age << end;

cout << “Size in bytes of type float is “

<< sizeof (float) << endl;

(6)

More about Floating Point Types

• Floating point constants in C++ like 94.6 without a suffix are of type double by default

• To obtain another floating point type constant a suffix must be used

– The suffix F or f denotes float type, as in 94.6F

– The suffix L or l denotes long double, as in 94.6L

(7)

Combined Assignment Operators

int age;

cin >> age;

A statement to add 3 to age

age = age + 3;

OR

age += 3;

(8)

A statement to double profits

float profits;

cin >> profits;

profits = profits * 2.0;

OR

profits *= 2.0;

(9)

Which form to use?

• When the increment (or decrement) operator is used in a “stand alone” statement to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form

dogs -- ; -- dogs;

USE EITHER

(10)

int alpha;

int num;

num = 13;

alpha = ++num * 3;

13

num

14

num alpha

42 14

PREFIX FORM

“First increment, then use ”

(11)

num

num alpha

alpha

num

POSTFIX FORM

“Use, then increment”

int alpha;

int num;

num = 13;

alpha = num++ * 3;

13

13 39

14

(12)

Type Cast Operator

The C++ cast operator, which comes in two forms, is used to explicitly request a type conversion

int intVar;

float floatVar = 104.8;

intVar = int(floatVar); // Functional notation, OR intVar = (int)floatVar; // Prefix notation uses ()

104.8 104

(13)

Ternary (three-operand) Operator ? :

SYNTAX

Expression1 ? Expression2 : Expression3

MEANING

If Expression1 is true (nonzero), then the value of the entire expression is Expression2. Otherwise, the value of the entire expression is Expression 3.

For example . . .

(14)

Using Conditional Operator

float Smaller (float x, float y) // Finds the smaller of two float values

// Precondition: x assigned && y assigned

// Postcondition:Function value == x, if x < y

// == y, otherwise

{

float min;

min = (x < y) ? x : y;

return min;

}

(15)

typedef statement

• typedef creates an additional name for an already existing data type

• Before bool type became part of ISO-ANSI C++, a Boolean type was simulated this way

typedef int Boolean;

const Boolean true = 1;

const Boolean false = 0;

. . .

Boolean dataOK;

. . .

dataOK = true;

(16)

Enumeration Types

• C++ allows creation of a new simple type by listing (enumerating) all the ordered values in the domain of the type

EXAMPLE

enum MonthType { JAN, FEB, MAR, APR, MAY, JUN,

JUL, AUG, SEP, OCT, NOV, DEC };

(17)

enum Type Declaration

• The enum declaration creates a new programmer-defined type and lists all the possible values of that type--any valid C++ identifiers can be used as values

• The listed values are ordered as listed; that is, JAN < FEB < MAR < APR , and so on

• You must still declare variables of this type

enum MonthType { JAN, FEB, MAR, APR, MAY, JUN,

JUL, AUG, SEP, OCT, NOV, DEC};

(18)

More about enum Type

Enumeration type can be used in a Switch statement for the switch expression and the case labels

Stream I/O (using the insertion << and extraction >> operators) is not defined for enumeration types; functions can be written for this purpose

Comparison of enum type values is defined using the 6 relational operators (< , <= , > , >= , == , !=)

An enum type can be the return type of a value-returning function in C++

(19)

Multifile C++ Programs

• C++ programs often consist of several different files with extensions such as .h and .cpp

• Related typedef statements, const values, enum type declarations, and similar items are often placed in user-written header files

• By using the #include preprocessor directive, the contents of

these header files are inserted into any program file that uses them

(20)

Inserting Header Files

#include <iostream> // iostream #include “school.h”

int main ()

{ enum SchoolType

{ PRE_SCHOOL,

.

ELEM_SCHOOL,

.

MIDDLE_SCHOOL,

.

HIGH_SCHOOL,

COLLEGE };

}

(21)

Implicit type coercion occurs . . .

whenever values of different data types are used in:

1. arithmetic and relational expressions 2. assignment operations

3. parameter passage

4. returning a function value with return (from a value-returning function)

Two rules apply . . .

(22)

Promotion (or widening) in C++

• Promotion is the conversion of a value from a “lower” type to a “higher”

type--specifically, for mixed type expressions:

Step 1. Each char, short, bool, or enumeration value is promoted to int; if both operands are now int, the result is an int expression

Step 2. If Step 1 leaves a mixed-type expression, the value of the “lower” operand is promoted to the a value of the “higher” type using the following precedence of types

int, unsigned int, long, unsigned long, float, double, long double

For an arithmetic expression, the result is an expression of the “higher” type

For a relational expression, the result is always bool (true or false )

(23)

Demotion (or narrowing) . . .

• Demotion is the conversion of a value from a “higher” type to a “lower” type, and may cause loss of information

FOR EXAMPLE, 98.6 98

temperature number

float temperature = 98.6;

int number;

number = temperature; // Demotion occurs

(24)

Chapter 11

Structured Types,

Data Abstraction and Classes

Dale/Weems

(25)

C++ Data Types

structured

array struct union class

address

pointer reference

simple

integral enum

char short int long bool

floating

float double long double

(26)

Structured Data Type

A structured data type is a type in which each value is a collection of component items

– The entire collection has a single name

– Each component can be accessed individually

– Used to bundle together related data of various types for convenient access under the same identifier

For example . . .

(27)

thisAnimal

5000

.id 2037581

.name “giant panda”

.genus “Ailuropoda”

.species “melanoluka”

.country “China”

.age 18

.weight 234.6

.health Good

(28)

anotherAnimal

6000 .id 5281003

.name “llama”

.genus “Lama”

.species “peruana”

.country “Peru”

.age 7

.weight 278.5

(29)

struct AnimalType

enum HealthType { Poor, Fair, Good, Excellent };

struct AnimalType // Declares a struct data type

{ // does not allocate memory

long id;

string name;

string genus;

string species; struct members

string country;

int age;

float weight;

HealthType health;

};

// Declare variables of AnimalType AnimalType thisAnimal;

AnimalType anotherAnimal;

(30)

struct type Declaration

SYNTAX

struct TypeName// Does not allocate memory {

MemberList };

MemberList SYNTAX

DataType MemberName;

DataType MemberName;

.

(31)

struct type Declaration

The struct declaration names a type and names the members of the struct

It does not allocate memory for any variables of that type!

You still need to declare your struct variables

(32)

Accessing struct Members

Dot (period) is the member selection operator

After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot

EXAMPLES

thisAnimal.weight

anotherAnimal.country

(33)

Aggregate Operation

An aggregation operation is an operation on a data structure as a whole, as opposed to an operation on an individual

component of the data structure

(34)

Passing a struct Type by Reference

void ChangeAge(/* inout */ AnimalType& thisAnimal) // Adds 1 to age

// Precondition: thisAnimal.age is assigned // Postcondition:thisAnimal.age ==

// thisAnimal.age@entry + 1

{

thisAnimal.age++;

}

(35)

AnimalType GetAnimalData ()

// Obtains all information about an animal from keyboard // Postcondition:

// Return value == AnimalType members entered at kbd {

AnimalType thisAnimal;

char response;

do {

// Have user enter members until they are correct

.

.

.

} while (response != ‘Y’);

return thisAnimal;

}

(36)

Hierarchical Structures

• The type of a struct member can be another struct type

• This is called nested or hierarchical structures

• Hierarchical structures are very useful when there is much detailed information in each record

For example . . .

(37)

struct DateType {

int month; // Assume 1 . . 12 int day; // Assume 1 . . 31

int year; // Assume 1900 . . 2050 };

struct StatisticsType {

float failRate;

DateType lastServiced; // DateType is a struct type int downDays;

};

struct MachineRec {

int idNumber;

string description;

StatisticsType history; // StatisticsType is a struct DateType purchaseDate;

float cost;

};

(38)

struct type variable machine

7000

.idNumber .description . history .purchaseDate .cost

.month .day .year

5719 “DRILLING…” 3 21 1995 8000.0 .failrate .lastServiced .downdays

.02 1 25 1999 4

.month .day.year

(39)

Unions in C++

DEFINITION

A union is a struct that holds only one of its members at a time during program execution.

EXAMPLE

union WeightType {

long wtInOunces;

int wtInPounds; Only one at a time

float wtInTons;

};

(40)

Using Unions

union WeightType // Declares a union type {

long wtInOunces;

int wtInPounds;

float wtInTons;

};

WeightType weight; // Declares a union variable

weight.wtInTons = 4.83;

// Weight in tons is no longer needed

// Reuse the memory space

(41)

Abstraction

• Abstraction is the separation of the essential qualities of an object from the details of how it works or is composed

– Focuses on what, not how

– Is necessary for managing large, complex software

projects

(42)

Control Abstraction

• Control abstraction separates the logical properties of an action from its

implementation

Search (list, item, length, where, found);

• The function call depends on the function’s specification (description), not its

implementation (algorithm)

(43)

Data Abstraction

• Data abstraction separates the logical properties of a data type from its

implementation

LOGICAL PROPERTIES IMPLEMENTATION

What are the possible values? How can this be done in C++?

What operations will be needed? How can data types be used?

(44)

Abstract Data Type (ADT)

• An abstract data type is a data type whose

properties (domain and operations) are specified (what) independently of any particular

implementation (how)

For example . . .

(45)

ADT Specification Example

TYPE

Time

DOMAIN

Each Time value is a time in hours, minutes, and seconds.

OPERATIONS

Set the time Print the time

Increment by one second Compare 2 times for equality

Determine if one time is “less than” another

(46)

ADT Implementation

• ADT implementation

– Choose a specific data representation for the abstract data using data types that already exist (built-in or programmer-defined)

– Write functions for each allowable operation

(47)

Class

• A class is a structured type in a programming

language that is used to represent an abstract data type

• A class member is a component of a class. Class

member may be either data or functions

(48)

C++ class Type

• Facilitates re-use of C++ code for an ADT

• Software that uses the class is called a client

• Variables of the class type are called class objects or class instances

• Client code uses class’s public member functions to

manipulate class objects

(49)

class Time Specification

// Specification file (Time.h)

class Time // Declares a class data type { // does not allocate memory

public : // Five public function members void Set (int hours , int mins , int secs);

void Increment ();

void Write () const;

bool Equal (Time otherTime) const;

bool LessThan (Time otherTime) const;

private : // Three private data members int hrs;

int mins;

int secs;

};

(50)

Public and private…

• Data or functions declared between the words public and private constitute the public interface: clients can access these class

members directly.

• Class members declared after the word

private are considered private information

and are inaccessible to clients.

(51)

Client Code Using Time

#include “time.h” // Includes specification of the class using namespace std;

int main () {

Time currentTime; // Declares two objects of Time Time endTime;

bool done = false;

currentTime.Set (5, 30, 0);

endTime.Set (18, 30, 0);

while (! done) { . . .

currentTime.Increment ();

if (currentTime.Equal (endTime)) done = true;

};

}

(52)

class type Declaration

The class declaration creates a data type and names the members of the class

It does not allocate memory for any variables of that type!

Client code still needs to declare class variables

(53)

Remember …

• Two kinds of class members: data members and function members

• Class members are private by default

• Data members are generally private

• Function members are generally declared public

• Private class members can be accessed only by the class

member functions (and friend functions), not by client

code

(54)

Aggregate class Operations

• Built-in operations valid on class objects are

Member selection using dot (.) operator ,

Assignment to another class variable using (=),

Pass to a function as argument (by value or

by reference),

Return as value of a function

• Other operations can be defined as class member functions

(55)

Separate Specification and Implementation in diferent files

// Specification file “time.h”

// Specifies the data and function members class Time

{

public:

. . .

private:

. . . };

// Implementation file “time.cpp”

// Implements the Time member functions

{

. . .

}

(56)

Implementation File for Time

// Implementation file “time.cpp”

// Implements the Time member functions.

#include “ time.h” // Also must appear in client code

#include <iostream>

. . .

bool Time::Equal(/* in */ Time otherTime) const // Postcondition: Return value == true,

// if this time equals otherTime, // otherwise == false

{

return ((hrs == otherTime.hrs) && (mins == otherTime.mins)

&& (secs == otherTime.secs));

(57)

Information Hiding

Information hiding - Class implementation details are hidden from the client’s view

Public functions of a class provide the interface between the client code and the class objects

client

code specification implementation

(58)

Selection and Resolution

• C++ programs typically use several class types

• Different classes can have member functions with the same identifier, like Write()

• Member selection operator is used to determine the object to whom member function Write() is applied

currentTime.Write(); // Class Time

numberZ.Write(); // Class ComplexNumber

• In the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its class

void Time::Write () const

{ . . . }

(59)

Use of const with Member Functions

• When a member function does not modify the private data members, use const in both the function prototype (in specification file) and the heading of the function definition (in

implementation file)

(60)

Class Constructors

• A class constructor is a member function whose purpose is to initialize the private data members of a class object

• The name of a constructor is always the name of the class, and there is no return type for the constructor

• A class may have several constructors with different parameter lists

• A constructor with no parameters is the default constructor

• A constructor is implicitly invoked when a class object is declared--if

there are parameters, their values are listed in parentheses in the

declaration

(61)

Specification of Time Class Constructors

class Time // Time.h {

public : // 7 function members

void Set(int hours, int minutes, int seconds);

void Increment();

void Write() const;

bool Equal(Time otherTime) const;

bool LessThan(Time otherTime) const;

// Parameterized constructor

Time (int initHrs, int initMins, int initSecs);

// Default constructor Time();

private : // 3 data members int hrs;

int mins;

int secs;

(62)

Implementation of Time Default Constructor

Time::Time ()

// Default Constructor // Postcondition:

// hrs == 0 && mins == 0 && secs == 0 {

hrs = 0;

mins = 0;

secs = 0;

}

(63)

Parameterized Constructor

Time::Time( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs) // Constructor

// Precondition:

// 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59

// Postcondition:

// hrs == initHrs && mins == initMins // && secs == initSecs

{

hrs = initHrs;

mins = initMins;

secs = initSecs;

}

(64)

Chapter 18

Recursion

Dale/Weems

(65)

Recursive Function Call

• A recursive call is a function call in which the called function is the same as the one making the call

• In other words, recursion occurs when a function calls itself!

• But we need to avoid making an infinite

sequence of function calls (infinite recursion)

(66)

Finding a Recursive Solution

• A recursive solution to a problem must be written carefully

• The idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved

• This easily solved situation is called the base case

• Each recursive algorithm must have at least one base case,

as well as a general (recursive) case

(67)

General format for

Many Recursive Functions

if (some easily-solved condition) // Base case

solution statement

else // General case

recursive function call

(68)

Writing a Recursive Function to Find n Factorial

DISCUSSION

The function call Factorial(4) should have value 24, because that is 4

* 3 * 2 * 1

For a situation in which the answer is known, the value of 0! is 1 So our base case could be along the lines of

if (number == 0)

return 1;

(69)

Writing a Recursive Function to Find Factorial(n)

Now for the general case . . .

The value of Factorial(n) can be written as

n * the product of the numbers from (n - 1) to 1, that is,

n * (n - 1) * . . . * 1

or, n * Factorial(n - 1)

And notice that the recursive call Factorial(n - 1) gets us “closer” to the base case

of Factorial(0)

(70)

Recursive Solution

int Factorial ( int number)

// Pre: number is assigned and number >= 0 {

if (number == 0) // Base case return 1;

else // General case return

number + Factorial (number - 1);

}

(71)

Why use recursion?

•These examples could all have been written more easily using iteration

•However , for certain problems the recursive solution is the most natural solution

•This often occurs when structured variables are used

Remember The iterative solution uses a loop, and the

recursive solution uses a selection statement

(72)

Recursion with Linked Lists

•For certain problems the recursive solution is the most natural solution

•This often occurs when pointer variables are

used

(73)

struct NodeType

typedef char ComponentType;

struct NodeType {

ComponentType component;

NodeType* link;

}

NodeType* head;

(74)

Recursion or Iteration?

EFFICIENCY

CLARITY

(75)

When a function is called...

• A transfer of control occurs from the calling block to the code of the function

• It is necessary that there be a return to the correct place in the calling block after the function code is executed

• This correct place is called the return address

• When any function is called, the run-time stack is used--activation

record for the function call is placed on the stack

(76)

Stack Activation Record

• The activation record (stack frame) contains the return address for this function call, the parameters, local variables, and space for the function’s return value (if non-void)

• The activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function code

• At this time the function’s return value, if non-void, is brought back

to the calling block return address for use there

(77)

// Another recursive function

int Func (/* in */ int a, /* in */ int b) // Pre: Assigned(a) && Assigned(b)

// Post: Return value == ??

{

int result;

if (b == 0) // Base case result = 0;

else if (b > 0) // First general case result = a + Func (a, b - 1));

// Say location 50

else // Second general case

result = Func (- a, - b);

// Say location 70 return result;

(78)

FCTVAL ? result ? b 2

Run-Time Stack Activation Records

x = Func(5, 2); // original call at instruction 100

original call

at instruction 100

pushes on this record

(79)

FCTVAL ? result ? b 1 a 5 Return Address 50 FCTVAL ?

result 5+Func(5,1) = ? b 2

a 5 record for Func(5,2) call in Func(5,2) code at instruction 50

pushes on this record for Func(5,1)

Run-Time Stack Activation Records

x = Func(5, 2); // original call at instruction 100

(80)

Function BinarySearch()

 BinarySearch that takes sorted array a , and two subscripts, low and high , and a key as arguments

 It returns true if key is found in the elements a[low...high], otherwise, it returns false

 BinarySearch can also be written using

iteration or recursion, but it is an inherently

recursive algorithm

(81)

x = BinarySearch(a, 0, 14, 25);

low high key subscripts

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 array

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28

16 18 20 22 24 26 28

24 26 28 24

NOTE: denotes element examined

(82)

// Recursive definition

bool BinarySearch (/* in */ const int a[ ], /* in */ int low,

/* in */ int high, /* in */ int key)

// Pre: a[low .. high] in ascending order && Assigned (key) // Post: IF (key in a[low . . high]), return value is true // otherwise return value is false

{ int mid;

if (low > high)

return false;

else {

mid = (low + high) / 2;

if (a [ mid ] == key) return true;

else if (key < a[mid]) // Look in lower half return BinarySearch(a, low, mid-1, key);

else // Look in upper half

return BinarySearch(a, mid+1, high, key);

}

Referencias

Documento similar

The Peñalba Formation is divided in the Seceda Member of Martínez-Catalán (1985) and Carucedo Member of Rabu (1977), whose boundaries are redefined.. The Seceda Member begins at

Se debe hacer cada vez que una clase contenga atributos punteros. En este caso es necesario pues el compilador implementa “copia baja” en la asignación y constructor copia por

Ahí surge el paradigma de la programación orientada a objetos, el lenguaje c se cambia al lenguaje c++ y el lenguaje pascal evoluciona a object pascal, ya no se utiliza un sistema

El compilador de Java convierte el archivo de código fuente (.java) a bytecode (.class)..

If you miss a class, follow this schedule to prepare for the next class session: it is YOUR responsibility to be prepared for each and every class session.. Always communicate

nor even talk to each other. If the player having his/her turn finds anyone moving then s/he calls his/her name. Then it becomes the turn of the player whose name is uttered.

The object is at the edge of a dark cloud, and it is asso- ciated with IRAS 17181–4405, whose colors are characteristic of an embedded young star. •

The coupled QD is a single photon emitter, whose energy detuning from the cavity simultaneously controls the linear polarization angle of the emitted light and the photon