Data structures and algorithms lab2

50 %
50 %
Information about Data structures and algorithms lab2

Published on February 22, 2014

Author: Bii03



OBJECTIVES Transition from C to C++  Struct vs. classes in C++  Templates 

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

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

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?

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;

STRUCTURES VS. CLASSES ‼ Exercise: Add to the complex structure new functions for the addition, division and multiplication of complex numbers.

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

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!

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

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.

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.

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++? 

Add a comment

Related presentations

Related pages

COMP20012. Algorithms and Data Structures: Laboratory ...

... (algorithms, data structures and outline ... /opt/info/courses/COMP20012/labs/lab2. However you come by them, you must understand how they work.
Read more

Lab2: Data Structure and Database Workshop

Lab2: Data Structure and Database Workshop I. Introduction ... In this lab, these two data structures will be reviewed extensively.
Read more

Data Structures and Algorithms - Table of Contents

Data Structures and Algorithms Course Notes, PLDS210 University of Western Australia. ... Data Structures. 3.1 Arrays; 3.2 Lists; 3.3 Stacks. 3.3.1 Stack ...
Read more

Data Structures And Algorithms Lab Manual - moimitamapf

Data Structures And Algorithms Lab Manual ... resources, you can find data structures. algorithms and applications in c++ 2nd edition. Review: ...
Read more

Data Structures And Algorithms Lab Manual For Me Cse

Data Structures And Algorithms Lab Manual For Me Cse ME NOTES CP7102, Advanced Data Structures and Algorithms, Click Here CP7111, Advanced Data Structures ...
Read more

Data Structures, Algorithms | LinkedIn

View 21376 Data Structures, Algorithms posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn. LinkedIn Home
Read more

Assignments — Problem Solving with Algorithms and Data ...

Problem Solving with Algorithms and Data Structures using Python by Bradley N. Miller, ...
Read more

Data structures and Algorithms | Comp 15 - Data Structures

Data-structures Data structures and Algorithms. ... Data structure: ... Programs = Algorithms + Data structures -Niklaus Wirth
Read more