Class variables and class functions
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
Shared information Suppose that some information is shared by all instances of a class class Quantum_mechanics { public: static double const IMPEDANCE_VACUUM; // ohms static double const PERMITTIVITY_VACUUM; // F/m static double const PERMEABILITY_VACUUM; // N/A^2 static double const SPEED_LIGHT_VACUUM; // m/s static double const PLANCK_CONSTANT; // Js static double const REDUCED_PLANCK_CONSTANT; // Js // Other member functions, member variables and class variables }; double Quantum_mechanics::IMPEDANCE_VACUUM {3.76730313667e-2}; double Quantum_mechanics::PERMITTIVITY_VACUUM {8.8541878128e-12}; double Quantum_mechanics::PERMEABILITY_VACUUM; {1.25663706212e−6}; double Quantum_mechanics::SPEED_LIGHT_VACUUM; {299792458}; double Quantum_mechanics::PLANCK_CONSTANT; {6.62607015e-34}; double Quantum_mechanics::REDUCED_PLANCK_CONSTANT{1.054571817e-34};
Shared information These can be used directly or indirectly: class Quantum_mechanics; int main() { double c{ Quantum_mechanics::SPEED_LIGHT_VACUUM }; double m, v; // uninitialized std::cout << "How much do you weigh (kg)? "; std::cin >> m; std::cout << "You have " << (m*c*c) << " J of energy" << std::endl; std::cout << "How fast are you moving (m/s)? "; std::cin >> v; std::cout << "Your De Broglie wavelength is " << (Quantum_mechanics::PLANCK_CONSTANT/m/v) << " m" << std::endl; return 0; }
Class variables Suppose you want to count the number of instances A variable will store the number of instances That variable must be initialized to 0 Each time the constructor is called, this variable is incremented The variable must be shared by all instances of the class We will refer to such a variable as a class variable
Class variables Suppose you want to count the number of instances class Class_name; // Each time an instance is created, increment 'count_' class Class_name { public: Class_name(); protected: static int count_; }; // Initialize the static member variable int Class_name::count_{0}; // Increment the count in the constructor Class_name::Class_name() { ++count_; // Increment the count }
Class functions Suppose we want to access this value: A member function can only be called if an instance exists No instance of the class must exist for this function to be called We can declare a function to be static We will call such a function to be a class function
Class functions Suppose we want to access this value class Class_name; // Each time an instance is created, increment 'count_' class Class_name { public: Class_name(); static int count(); protected: static int count_; }; // Other initializations and member function definitions... int Class_name::count() { return count_; }
Counting the number of instances created We can use it now: #include <iostream> class Class_name; int main(); // Class definition, initializations and member function definitions... int main() { std::cout << Class_name::count() << " instances created" << std::endl; Class_name a{}; Class_name b{}; return 0; } Output: 0 instances created 2 instances created
Class functions Class functions can They cannot access all parameters access all class variables call other class functions They cannot access any member variables call any member functions
Creating unique identifiers Given a class, it may be useful to give each instance a different identifier class Unique { public: static int create_unique_id(); protected: static unsigned int next_id; }; unsigned int Unique::next_id{20000000}; // Assign the ID of this item to be the next ID; // then update the next ID. int Unique::create_unique_id() { ++next_id; }
Creating unique identifiers A class function is a function that is not associated with any one instance of a class class Unique { public: static int create_unique_id(); protected: unsigned int this_id; static unsigned int next_id; }; unsigned int Counter::next_id{0}; // Assign the ID of this item to be the next ID; // then update the next ID. Counter::Counter(): this_id{ next_id } { ++next_id; }
Creating unique identifiers We could create a class function that returns a unique identifier each time the function is called class Unique { public: static int create_unique_id(); protected: static unsigned int next_id; }; unsigned int Counter::next_id{0}; // Assign the ID of this item to be the next ID; // then update the next ID. int Unique::create_unique_id() { ++next_id; }
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.