Lecture 6 - ECE 2574 Introduction to Data Structures and...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ECE 2574 Introduction to Data Structures and Algorithms Lecture 6: ADTs and Classes Reading: Chapter 3 and parts of Chapter 8 Chris Wyatt Electrical and Computer Engineering and School of Biomedical Engineering and Sciences Virginia Tech Monday Schedule Implementing ADTs in C++ Class design fundamentals In class exercise Implementing an ADT using a data structure ADT Book Entry Book Title ISBN Program set title set ISBN Data Structure Interface Using structures and functions to implement ADT's Program struct BookEntry { char * title; int isbn; } /* function to set the book title */ void setTitle(BookEntry * b, char * title); /* function to set the isbn */ void setISBN(BookEntry * b, int id); What is the problem with this? C++ classes introduce the concept of private and public. Program class BookEntry { private: string title; int isbn; public: void setTitle(string aTitle); void setISBN(int anISBN); }; Review of the C++ class A type is a concrete representation of a concept. e.g. the type float approximates a real number A class is a user-defined type that extends the built-in types. e.g. the class BookEntry can represent the concept of the ADT bookEntry Classes provide many advantages for implementing ADT s. They can hide implementation details via private. They provide a means of forcing the ADT interface to be used. They enable type-checking on complex concepts. They assist with assertion checking. All of this helps to keep the type in a well-defined state. Example: Triangle ADT A triangle is an ordered set of three points in 3D space. They have properties such as perimeter and area, and categories such as right, isosceles, scalene, acute, obtuse. They also have invariants, such as sum of interior angles is 180 degrees and support operations (e.g. scaling) Classes control access via private/public class Triangle { private: float x[3], y[3], z[3]; public: void init(float* theX, float* theY, float* theZ); float getPerimeter(); float getArea(); bool isAcute(); }; Triangle t; float X[3] = {1,1,1}; float Y[3] = {1,2,1}; float Z[3] = {1,3,1}; t.init(&X[0], &Y[0], &Z[0]); std::cout << "Area is " << t.getArea(); float x = t.x[0]; // x is private! Constructors provide automatic initialization. class Triangle { private: float x[3], y[3], z[3]; you might forget to call this public: void init(float* theX, float* theY, float* theZ); ....... class Triangle { private: float x[3], y[3], z[3]; public: Triangle(); // default constructor ......... Typically a class definition is provided inside a header file, while the implementation is separate. Triangle.h (class definition) Triangle.cxx (class implementation) program.cxx An aside on multiple compilation units..... ClassName.h (class definition) ClassName.cxx (class implementation) Compiler main.cxx (main is implemented) ClassName.o Compiler main.o Linker program Destructors allow resources allocated by the class to be destroyed automatically (in some cases) class Triangle2 { float *x, *y, *z; public: Triangle2() { x = new float[3]; y = new float[3]; z = new float[3]; } ~Triangle2() { delete x; delete y; delete z; } }; Copy Constructors are required when pointers are used internally to get the desired behavior class Triangle2 { float *x, *y, *z; public: Triangle2(); ~Triangle2(); Triangle2(const Triangle2& t) { x = new float[3]; y = new float[3]; z = new float[3]; Triangle2 t1; Triangle2 t2 = t1; x[0] = t.x[0]; x[1] = t.x[1]; x[2] = t.x[2]; y[0] = t.y[0]; y[1] = t.y[1]; y[2] = t.y[2]; z[0] = t.z[0]; z[1] = t.z[1]; z[2] = t.z[2]; } }; Why is the copy constructor required for Triangle2 but not Triangle? Values are copied member-wise by default class Triangle { private: float x[3], y[3], z[3]; ....... class Triangle2 { private: float *x, *y, *z; ....... Methods provide access and control over the data. Example: Triangle::getPerimeter(); returns the perimeter of the triangle. Example: Triangle::isAcute(); returns a boolean indicating if the triangle is acute Instantiating Objects Classes are types and so can be instantiated in the same way. On the stack: Triangle t; On the heap: t = new Triangle; In arrays: Triangle* tarray= new Triangle[10]; Triangle tarray[10]; Passing Objects As with any type objects may be passed by reference or by value. Which you want depends on the application, but remember when passing by value, an implicit copy is made. There are 7 basic kinds of classes: Concrete types Abstract types Nodes Operations Interfaces Handles Application frameworks Inheritance in C++ was a major advancement over plain C. Inheritance allows for code reuse and adaptation. In some cases classes use other classes internally (sometimes called inner classes) In other cases they extend the class through inheritance. class Shape; class Triangle: public Shape { ... } class Circle: public Shape { ... } Shape Triangle In class exercise Write a "do nothing" class with a constructor, copy constructor, and destructor that just prints out (using clog) which member function it is. Then test the class by creating instances in the class both statically and dynamically. When you run it, what does it print? Why? Create dummy functions with your class as an argument, passing by pointer, reference and value. Test these functions. What is printed out? ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online