Introduction to classes

Slides:



Advertisements
Similar presentations
Throwing and catching exceptions
Advertisements

Recursive binary search
Templates.
Static variables.
Default values of parameters
Pointers.
Dynamically allocating arrays
Binary search.
Command-line arguments
Throwing exceptions.
Pointer arithmetic.
Console input.
Dangling pointers.
This.
Sorted arrays.
Dynamically allocating arrays within structures
Dynamic memory allocation
Break statements.
Introduction to classes
Linked Lists.
Wild pointers.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The ternary conditional operator
Throwing exceptions.
Dynamically allocating structures
Memory leaks.
Pushing at the back.
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Throwing exceptions.
Selection sort.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Pointer arithmetic.
Class variables and class functions
Operator overloading.
The std::string class.
Dynamic allocation of arrays
Templates.
Dynamic memory allocation
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
Presentation transcript:

Introduction to classes

Outline In this lesson, we will: Describe the insertion sort algorithm Look at an example Determine how the algorithm work Create a flow chart Implement the algorithm Look at the run times Consider some optimizations

Accessing member variables Suppose we go back to our string class: class String; class String { public: // Constructors String(); String( std::size_t max_length ); private: std::size_t array_capacity; std::size_t string_length; char *character_array; }; We can create them and destroy them, but that’s about it…

Accessing member variables If you tried to access a member variable, you’d get a compile-time error: int main() { String str{10}; std::cout << "The capacity is " << str.array_capacity << std::endl; return 0; } example.cpp: In function 'int main()': example.cpp:15:21: error: 'std::size_t String::array_capacity' is private std::size_t array_capacity; ^ example.cpp:39:44: error: within this context std::cout << "The capacity is " << str.array_capacity

Accessing member variables You may not want the user to directly access the member variables because: They may incorrectly update the location of '\0' They may not update string_length They may go beyond the end of the array

Accessing member variables We can add publicly visible member functions: class String { public: // Constructors and destructor String(); String( std::size_t max_length ); ~String(); // Member functions char get_char( std::size_t n ) const; void set_char( std::size_t n, char ch ); private: std::size_t array_capacity; std::size_t string_length; char *character_array; };

Accessing member variables We must specify their functionality: char get_char( std::size_t n ) const; This function will not change any member variables (const) This member function will either Return the nth character if n < array_length, Otherwise it will return the null character '\0' char set_char( std::size_t n, char ch ); Set the nth character to ch if n < array_length, Update the nth character to ch if n >= array_length && n < (array_capacity - 1) but also update array_length and the position of '\0', filling in all intermediate spaces with the new character, as well

Accessing member variables Getting a character is straight-forward: char String::get_char( std::size_t n ) const { return ( n < string_length ) ? character_array[n] : '\0'; }

Accessing member variables Setting a character is more difficult: void String::set_char( std::size_t n, char ch ) { if ( n < string_length ) { character_array[n] = ch; } else if ( n < (array_capacity - 1) ) { for ( std::size_t k{string_length}; k <= n; ++k ) { character_array[k] = ch; } string_length = n + 1; character_array[string_length] = '\0'; } else { throw n;

Calling member functions When we worked with structures, we implemented functions as follows: string_t str_1, str_2; // Allocate memory for strings of max length 16 and 32 string_init( str_1, 16 ); string_init( str_2, 32 ); The instance was always passed as the first argument by reference void string_init( string_t &str, std::size_t max_len ) { str.capacity = max_len + 1; str.character_array = new char[str.capacity]; str.character_array[0] = '\0'; str.length = 0; }

Calling member functions With classes, we call member functions on an instance the exact same way we access member variables: int main() { String str{11}; str.set_char( 0, 'H' ); str.set_char( 1, 'i' ); str.set_char( 10, '!' ); for ( std::size_t k{0}; k <= 10; ++k ) { std::cout << str.get_char( k ); } std::cout << std::endl; return 0; 1 2 3 4 5 6 7 8 9 10 11 H i ! \0 Output: Hi!!!!!!!!!

Calling member functions If we are dealing with pointers, we use the arrow operator: int main() { String p_str{new String{11}}; p_str->set_char( 0, 'H' ); p_str->set_char( 1, 'i' ); p_str->set_char( 10, '!' ); for ( std::size_t k{0}; k <= 10; ++k ) { std::cout << p_str->get_char( k ); } std::cout << std::endl; delete p_str; p_str = nullptr; return 0; Output: Hi!!!!!!!!!

Accessing member variables There may be other questions we may ask about our string: // Member functions char get_char( std::size_t n ) const; std::size_t length() const; std::size_t max_length() const; char *get_character_array() const; void set_char( std::size_t n, char ch );

Accessing member variables There may be other questions we may ask about our string: std::size_t String::length() const { return string_length; } std::size_t String::max_length() const { return array_capacity - 1;

Accessing member variables If we want to return a character array, we cannot just return the value of the member variable? char *String::get_character_array() const { return character_array; } Why?

Accessing member variables Instead, we must copy everything to a new dynamically-allocated array, and then return the address of that new array char *String::get_character_array() const { char *return_array{new char[string_length + 1]}; for ( std::size_t k{0}; k <= string_length; ++k ) { return_array[k] = character_array[k]; } return return_array;

Summary Following this lesson, you now Understand the insertion sort algorithm You saw an example Watched how to convert the flow chart to an algorithm There is a lot of time spent swapping, which can be reduced significantly The run time is still approximately the same as selection sort

References [1] No references?

Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information.

Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.