Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Data structures and algorithms lab2

1,851 views

Published on

Published in: Education
  • Be the first to comment

Data structures and algorithms lab2

  1. 1. DATA STRUCTURES AND ALGORITHMS LAB 2 Bianca Tesila FILS, Feb 2014
  2. 2. OBJECTIVES Transition from C to C++  Struct vs. classes in C++  Templates 
  3. 3. WHY C++?   C + + allows the implementation of data structures with generic data types via templates. C follows the procedural programming paradigm while C++ can follow both procedural paradigm and OOP (Which are the OOP principles?)
  4. 4. TRANSITION FROM C TO C++     Any program written in C (“.c” extension) can be compiled by a C++ compiler (“.cpp” extension); not vice versa In C we don’t have classes!!! The NAMESPACE feature in C++ is absent in case of C: avoid name collisions (namespaces are similar to Java packages- you can look for differences & similarities for the next time) Standard input & output functions differ in the two languages: in C, we have scanf and printf; in C++ we have cin>> and cout<<
  5. 5. TRANSITION FROM C TO C++ ‼ Exercise: Implement a function to sort an array of 5 elements of type double. Use a swap mechanism, which has to be implemented in another function. Hint: Pay attention to passing by value/ passing by reference! Do you remember which is the difference between them?
  6. 6. STRUCTURES VS CLASSES: STRUCTURES IN C++    We can define functions inside a structure; we can access the structure’s fields by using this-><field_name> C++ structures support inheritance Everything inside a structure is public by default Example: typedef struct complex { double re; double im; void complex_initialize(double param_re, double param_im) { this->re = param_re; this->im = param_im; } struct complex complex_conjugate() { struct complex conjugate; conjugate.complex_initialize(this->re, -(this->im)); return conjugate; } }complex;
  7. 7. STRUCTURES VS. CLASSES ‼ Exercise: Add to the complex structure new functions for the addition, division and multiplication of complex numbers.
  8. 8. STRUCTURES VS. CLASSES: CLASSES  What is a class?  What is an object?  Replace the keyword struct with the keyword class in the last exercise  C++ structures behave like C++ classes, allowing functions, contructors, destructors. The main diffrence between classes and C++ structures is that everything inside a structure is public, by default, while everything inside a class is private, by default
  9. 9. STRUCTURES VS. CLASSES: CLASSES ‼ Exercise: Make a Complex class using the previous exercise. Hint: Don’t forget about the fact that everything inside a class is private, by default. Take into account the Encapsulation principle!
  10. 10. TEMPLATE CLASSES    Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. Similar to Java Generics template<typename T> class class_name { ... }  A normal class definition will be prefixed by template<typename T>  The type T can now be used as a valid type within the class  we can have variables, function arguments and function return values of type T  Everything happens at compile time, not runtime  The compiler analyzes how you use the class
  11. 11. TEMPLATE CLASSES: EXAMPLE template<typename T> class KeyStorage { public: int key; T member; //a generic member: we don't know its type when creating the class }; int main() { //Everything happens to compile time, not to run time //The compiler analyses the way in which you use the class KeyStorage<long> keyElement1; KeyStorage<int> keyElement2; return 0; } ‼ Exercise: Make a constructor, a destructor, a getter and a setter for the template class KeyStorage.
  12. 12. HOMEWORK    Finish all the lab exercises. Implement a template class for storing the coordinates of a point. Add corresponding methods for moving a point(along Ox, along Oy, along both Ox and Oy). Using the implemented class, develop an application for moving a point inside a rectangle of dimensions 1 x N, where N is given by the user. Start from the origin. Implement a template class for bank accounts with corresponding methods(deposit, withdrawal, balance display, display owner etc). Develop an application that uses this class.
  13. 13. INTERVIEW: Does overloading exist in C++?  Do abstract classes exist in C++?  When is it better to use abstract classes and when templates?  Do static classes exist in C++? 

×