Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,"— Presentation transcript:

1 Programming Languages and Paradigms C++

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

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

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

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

6 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

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

8 Example: class declaration class car { private: int dist; double gas; public: car(); void drive( int km ); void loadGas( double lit ); int getOdometerReading(); double getGasLeft(); }; fields constructor methods

9 Example: method definitions car::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; }

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

11 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

12 Exercise: defining a C++ class Assemble 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

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

14 C++ objects and copies bankaccount a(100); bankaccount b; bankaccount c = a; b = a; 100 balance a

15 C++ objects and copies bankaccount a(100); bankaccount b; bankaccount c = a; b = a; 100 balance a 0 b

16 C++ objects and copies bankaccount a(100); bankaccount b; bankaccount c = a; b = a; 100 balance a 0 b 100 balance c

17 C++ objects and copies bankaccount a(100); bankaccount b; bankaccount c = a; b = a; 100 balance a 100 balance b 100 balance c

18 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

19 Example: integer vector class intvector { private: int size; int * data; public: intvector(); intvector(int s); // other methods... };

20 Example: integer vector intvector::intvector() { size = 5; data = new int[5]; } intvector::intvector(int s) { size = s; data = new int[s]; }

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

22 C++ objects and copies (pointers) intvector a; intvector b(10); intvector c = a; b = a; data a 5 size

23 C++ objects and copies (pointers) intvector a; intvector b(10); intvector c = a; b = a; data a 5 sizedata b 10 size

24 C++ objects and copies (pointers) intvector a; intvector b(10); intvector c = a; b = a; data a 5 sizedata b 10 sizedata c 5 size

25 C++ objects and copies (pointers) intvector a; intvector b(10); intvector c = a; b = a; data a 5 sizedata b 5 sizedata c 5 size

26 The intention intvector a; intvector b(10); intvector c = a; b = a; data a 5 sizedata b 10 sizedata c 5 size 5

27 Variables going out of scope void method() { int i = 10; bankaccount b; intvector v; //... } int main() { // before method(); // after return 0; } data v 5 size 0 balance b 0 i

28 Variables going out of scope void method() { int i = 10; bankaccount b; intvector v; //... } int main() { // before method(); // after return 0; } data v 5 size 0 balance b 0 i ? Memory leak!

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

30 intvector destructor intvector::~intvector() { delete [] data; } De-allocates memory allocated by data = new int[size];

31 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

32 intvector assignment intvector& intvector::operator=(const intvector& c) { // TODO: to guard against self-assignment delete [] 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

33 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]; )

34 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

35 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


Download ppt "Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,"

Similar presentations


Ads by Google