programming languages and paradigms c++. c++ program structure c++ program: collection of files...

35
Programming Languages and Paradigms C++

Upload: berenice-obrien

Post on 06-Jan-2018

228 views

Category:

Documents


0 download

DESCRIPTION

Header Files (.h)  Contains class declarations  Prototypes for functions outside of classes  Others

TRANSCRIPT

Page 1: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Programming Languages and

ParadigmsC++

Page 2: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ program structure C++ Program: collection of files

Header files CPP source files

Files contain class, function, and global variable declarations/definitions

main() function serves as an entry point (just like in C)

Page 3: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Header Files (.h) Contains class declarations Prototypes for functions outside of classes Others

Page 4: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Source Files (.cpp) Function/method definitions Directives (e.g., #include, #define) Variables and initialization

(global/static variables)

Page 5: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Variables in C++ Regular variables

int x; x = 5; bankaccount b;

Pointers int *p; p = &x; bankaccount *q; q = new bankaccount(); … delete q;

References/Aliases int &r = x; // initialization required

Arrays int num[20]; int *a; a = new int[size]; bankaccount *accts;

accts = new bankaccount[10]; … delete[] accts;

Page 6: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Class declaration class c

{ private: members … public: members …};

Members (fields and methods) grouped into public, private or protected regions

Fields can be regular variables, arrays, pointers, or references

Method declarations are (often) prototypes Method defined separately from the class declaration

Page 7: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Example: class declarationclass car{ private: int dist; double gas; public: car(); void drive( int km ); void loadGas( double lit ); int getOdometerReading(); double getGasLeft();};

Page 8: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Example: class declarationclass car{ private: int dist; double gas; public: car(); void drive( int km ); void loadGas( double lit ); int getOdometerReading(); double getGasLeft();};

fields

constructormethods

Page 9: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Example: method definitionscar::car(){ dist = 0; gas = 40.0;}void car::drive( int km ){ dist += km; gas -= km/10.0;}void car::loadGas( double lit ){ gas += lit;}int car::getOdometerReading(){ return dist;}double car::getGasLeft(){ return gas;}

Page 10: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Example: using objectsint main(){ car c; c.drive( 20 ); c.loadGas( 2 ); cout << "c: km-" << c.getOdometerReading() << " liters-" << c.getGasLeft() << endl; return 0;}

Page 11: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Role and necessity of header files For a source file to compile correctly, the

appropriate declarations need to precede the use of a class or function Differentiate declaration from definition

Both the source file containing class definitions and the source file using the class must include the header file

Page 12: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Exercise: defining a C++ classAssemble the following C++ project:

banktester.cpp: using the bankaccount class and the power function

power.h: contains prototype for the power function

power.cpp: code for the power function bankaccount.h: header file for the

bankaccount class (needs to be revised) bankaccount.cpp: source file for the

bankaccount class (needs to be created) Compare your output with correct output

Page 13: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copiesbankaccount a(100);bankaccount b;bankaccount c = a;b = a;

Page 14: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copiesbankaccount a(100);

bankaccount b;

bankaccount c = a;

b = a;

100balance

a

Page 15: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copiesbankaccount a(100);

bankaccount b;

bankaccount c = a;

b = a;

100balance

a

0 balance

b

Page 16: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copiesbankaccount a(100);

bankaccount b;

bankaccount c = a;

b = a;

100balance

a

0 balance

b

100balance

c

Page 17: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copiesbankaccount a(100);

bankaccount b;

bankaccount c = a;

b = a;

100balance

a

100balance

b

100balance

c

Page 18: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Pointers as fields Suppose a class has fields that are

pointers Often because of dynamically allocated data

Introduces complexity when copying objects

In C++, the default is a shallow copy Field values are copied verbatim If fields are pointers, this is often not the

desired intention A deep copy requires additional coding

Page 19: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Example: integer vectorclass intvector{private:int size;int * data;

public:intvector();intvector(int s);

// other methods...

};

Page 20: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Example: integer vectorintvector::intvector(){size = 5;data = new int[5];

}intvector::intvector(int s){size = s;data = new int[s];

}

Page 21: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copies (pointers)intvector a;intvector b(10);intvector c = a;b = a;

Page 22: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copies (pointers)intvector a;

intvector b(10);

intvector c = a;

b = a;

data

a

5size

Page 23: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copies (pointers)intvector a;

intvector b(10);

intvector c = a;

b = a;

data

a

5size

data

b

10size

Page 24: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copies (pointers)intvector a;

intvector b(10);

intvector c = a;

b = a;

data

a

5size

data

b

10size

data

c

5size

Page 25: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

C++ objects and copies (pointers)intvector a;

intvector b(10);

intvector c = a;

b = a;

data

a

5size

data

b

5size

data

c

5size

Page 26: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

The intentionintvector a;

intvector b(10);

intvector c = a;

b = a;

data

a

5size

data

b

10size

data

c

5size

5

Page 27: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Variables going out of scopevoid method(){ int i = 10; bankaccount b; intvector v; // ...}

int main(){ // before method(); // after return 0;}

data

v

5size

0 balance

b

0 i

Page 28: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Variables going out of scopevoid method(){ int i = 10; bankaccount b; intvector v; // ...}

int main(){ // before method(); // after return 0;}

data

v

5size

0 balance

b

0 i

?

Memory leak!

Page 29: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

What needs to be programmed Destructor

De-allocates previously allocated memory(need to call delete on pointers)

Signature: ~classname() Copy constructor

Creates an object given an existing object Need to allocate (construct) and then copy Signature: classname( const classname& c )

Assignment operator Copies data from an existing object into an existing

object Need to de-allocate, allocate, then copy Signature: classname& operator=( const classname& c )

Page 30: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

intvector destructor

intvector::~intvector(){delete [] data;

}De-allocatesmemory allocated bydata = new int[size];

Page 31: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

intvector copy constructor

intvector::intvector(const intvector& c){size = c.size;data = new int[size];for(int i = 0; i < size; i++)

data[i] = c.data[i];}

allocate

then, copy

Page 32: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

intvector assignmentintvector&intvector::operator=(const intvector& c){

// TODO: to guard against self-assignmentdelete [] data;size = c.size;data = new int[size];for(int i = 0; i < size; i++)

data[i] = c.data[i];return *this;

}

De-allocate existing data

finally, copy

then, re-allocate

Page 33: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Points to ponder When is a copy constructor called?

someclass a = b; someclass c(a); When passing an object parameter during

method calls: somemethod(a); Why do we need to guard against self-

assignment? What happens to this statement?: x = x; Will a programmer ever perform self-

assigmment? ( maybe: a[i] = a[j]; )

Page 34: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Lab Write a dataset class with the following methods:

void addscore(double score): adds a score to the dataset

double computemean(): returns the average of the scores added so far(assume mean=0 if there are no scores in the dataset)

double computestdev(): returns the standard deviation: the squareroot of the average of squares of the difference of each element from the mean

A tester program (dstester.cpp) is provided The dataset class should be dynamic

Make the array of scores “grow” in size as necessary

Page 35: Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,

Lab, continued Rules on dataset capacity

Begin with a capacity of size 10 Whenever addscore is called on a full dataset,

double the capacity Make sure to implement a destructor, copy

constructor and assignment operator tester2() function tests whether you have

programmed this correctly