Dynamic allocation of classes

Slides:



Advertisements
Similar presentations
Throwing and catching exceptions
Advertisements

Recursive binary search
For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Reference variables, pass-by-reference and return-by-reference
Dynamically allocating arrays
Do-while loops.
Command-line arguments
Throwing exceptions.
Console input.
Dangling pointers.
This.
Dynamically allocating arrays within structures
Dynamic memory allocation
Break statements.
Linked Lists.
Wild pointers.
The comma as a separator and as an operator
Bucket sort.
The call stack and recursion and parameters revisited
The ternary conditional operator
Throwing exceptions.
Dynamically allocating structures
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Polymorphism.
Repetitious operations
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Code-development strategies
Throwing exceptions.
Anatomy of a program.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Recursive functions.
Class variables and class functions
Operator overloading.
The std::string class.
Dynamic allocation of arrays
Templates.
This.
Insertion sort.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Encapsulation.
Destructors.
Counting sort.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
Recursive functions.
Presentation transcript:

Dynamic allocation of classes

Outline In this lesson, we will: Describe issues with the primitive data types Introduce the 3-body problem and an attempt to solve it Introduce the struct keyword and member variables Create a 3-dimensional vector data structure Describe assigning to and using the member variables Describe passing instances of data structures as arguments We will create a library of functions for vectors Initialize instances of data structures Revisit our 3-body problem Determine if we have a solution to arrays

Accessing member variables Consider this erroneous code: int main() { Rational p( 1, 2 ); p.numerator = 2; // Now it is 2/2 std::cout << p.to_string() << std::endl; Date birthday( 1957, 2, 28 ); // February 28, 1957 birthday.day = 29; // 1957 is not a leap year! std::cout << waterloo.to_string_iso() << std::endl; return 0; }

Accessing member variables No matter what you do, nothing prevents a user from intentionally or inadvertently modifying member variables Does every member function have to validate the member variables? std::string Rational::to_string() const { if ( !is_valid() ) { throw std::range_error( "the rational number is invalid" ); } // Convert the valid date to a string... std::string DTG::to_string() const { throw std::range_error( "the date is invalid" );

Accessing member variables How about addition and division? Rational Rational::operator+( Rational const &q ) const { if ( !is_valid() || !q.is_valid() ) { throw std::range_error( "one operand is an invalid rational number" ); } // The constructor factors out the gcd return Rational( numerator*q.denominator + q.numerator*denominator, denominator*q.denominator

The alternative: protect your data The alternative is easy: do not let users access your data class Rational { public: // Member functions... protected: int numerator; unsigned int denominator; };

Data protection What does data protection do? Any member function within a class can: Access member variables that are protected within that class Call member functions that are protected within that class Users of a class can only: Access member variables that are public Call member functions that are public

The interface The public member functions and variables (if any) are described as the interface of the class They describe how a user can access and manipulate the class In many cases, most private member variables are protected Member functions extraneous or ancillary to the class are protected You do not use a rational number class to calculate the gcd Protected member functions are often described as helper functions

Data protection For example: In the rational number class: Both the numerator and denominator should be protected Calculating the greatest common divisor really has nothing tying it in to rational numbers—it is a tool used for the rational number class, so there is not Users of a class can only: Access member variables that are public Call member functions that are public

Manipulating member variables Often, classes will allow the user to access member variables through a functional interface Member functions and variables cannot have the same name, so it is common practice to append an underscore to all protected member variables class Rational { public: int numerator() const; unsigned int denominator() const; protected: // Protected member variables int numerator_; unsigned int denominator_; };

Manipulating member variables These accessors simply return the values: int Rational::numerator() const { return numerator_; } unsigned int Rational::denominator() const { return denominator_;

Manipulating member variables If you feel the user should be able to manipulate protected member variables, you can overload the member functions void Rational::numerator( int n ) const { numerator_ = n; normalize(); } void Rational::denominator( int d ) const { if ( d == 0 ) { throw illegal_argument(

Summary Following this lesson, you now Understand the struct keyword Know how to declare, access and manipulate the member variables Know how to pass instances of data structures to functions Understand how to initialize instances Understand how data structures can contain data structures Know that this simple solution cannot solve our issues with arrays

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.