Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic allocation of classes

Similar presentations


Presentation on theme: "Dynamic allocation of classes"— Presentation transcript:

1 Dynamic allocation of classes

2 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

3 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; }

4 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" );

5 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

6 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; };

7 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

8 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

9 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

10 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_; };

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

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

13 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

14 References [1] No references?

15 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 for more information.

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


Download ppt "Dynamic allocation of classes"

Similar presentations


Ads by Google