Slides version: lecture4_slides.html Website version: lecture4.html
public
private
const
Guidelines:
operator=
get_
set_
is_
has_
add_
remove_
clear_
size_
to_string_
from_string_
Example: Point_2D
Point_2D
#include <iostream> using namespace std; class Point_2D // the class name // we use first upper case letter to indicate a class { private: // private data members & member functions // Data members // we use `m_` to indicate a data member // record the x coordinate int m_x; // record the y coordinate int m_y; // Member functions // we use `_` to indicate a member function // check if the point is valid void _check_validity();
public: // public member functions // Constructor // Constructor & Default constructor // initialize data members, with default values // accessable to const object by default Point_2D(const int &arg_x = 0, const int &arg_y = 0); // Copy constructor // copy the data members from the given object // accessable to const object by default Point_2D(const Point_2D &arg_point); // Destructor ~Point_2D() // destructor { // do nothing, because we don't // have any dynamic memory }
// Member functions // we use lower case letter to indicate a member function // also, we just define the function declaration // and leave the definition to the end of the class // print the x and y coordinate in format (x, y) // accessable to const object void print() const; // modify the x and y coordinate // we use `arg_` to indicate the arguments // and re-write with setter void set_x(const int &arg_x); void set_y(const int &arg_y); void set(const int &arg_x, const int &arg_y); // get the x coordinate and y coordinate // accessable to const object int get_x() const; int get_y() const; };
// function definition // check if the point is valid void Point_2D::_check_validity() { // check if the x coordinate is valid if (m_x < 0) { // if not, set it to 0 m_x = 0; } // check if the y coordinate is valid if (m_y < 0) { // if not, set it to 0 m_y = 0; } }
// Constructor & Default constructor // initialize data members, with default values Point_2D::Point_2D(const int &arg_x, const int &arg_y) // use `: var_name1(arg_var_name1), var_name2(arg_var_name2)` // to initialize data members : m_x(arg_x), m_y(arg_y) { // check if the point is valid _check_validity(); } // Copy constructor // copy the data members from the given object Point_2D::Point_2D(const Point_2D &arg_point) : m_x(arg_point.m_x), m_y(arg_point.m_y) { // check if the point is valid _check_validity(); }
// print the x and y coordinate // accessable to const object void Point_2D::print() const { cout << "(" << m_x << ", " << m_y << ")"; } // modify the x and y coordinate // we use `arg_` to indicate the arguments // and re-write with setter void Point_2D::set_x(const int &arg_x) { m_x = arg_x; // check if the point is valid _check_validity(); } void Point_2D::set_y(const int &arg_y) { m_y = arg_y; // check if the point is valid _check_validity(); }
void Point_2D::set(const int &arg_x, const int &arg_y) { m_x = arg_x; m_y = arg_y; // check if the point is valid _check_validity(); } // get the x coordinate and y coordinate // accessable to const object int Point_2D::get_x() const { return m_x; } int Point_2D::get_y() const { return m_y; }
// main function int main() { // create a Point_2D object with initial values Point_2D p1(1, 2); // or create with another Point_2D object Point_2D p2(p1); // print 2 instances of Point p1.print(); cout << endl; // or use getter cout << "(" << p2.get_x() << ", " << p2.get_y() << ")" << endl;
// modify each instance with setter p1.set(3, 4); p2.set_x(5); p2.set_y(6); // you can see the change in p1 and p2 p1.print(); cout << endl; // or use getter cout << "(" << p2.get_x() << ", " << p2.get_y() << ")" << endl; return 0; }
Syntax:
class class_name { public: // default constructor class_name(); // constructor with arguments class_name(int arg_x, int arg_y); // constructor with arguments that has default value // the usage is the same as the default constructor class_name(int arg_x = 0, int arg_y = 0); // copy constructor class_name(const class_name &arg_class_name); };
Example: Point_2D (partial)
class Point_2D { private: // private data members & member functions // record the x coordinate int m_x; // record the y coordinate int m_y; public: // public member functions // Setter // modify the x and y coordinate // we use `arg_` to indicate the arguments // and re-write with setter void set_x(const int &arg_x); void set_y(const int &arg_y); void set(const int &arg_x, const int &arg_y); // Getter // get the x coordinate and y coordinate // accessable to const object int get_x() const; int get_y() const; };
class class_name { public: // default constructor class_name(); // destructor ~class_name(); };
class Point_2D { private: // private data members & member functions // record the x coordinate int m_x; // record the y coordinate int m_y; public: // public member functions // print the x and y coordinate in format (x, y) // we add `const` so that it can access the value form a const object void print() const; // Setter // modify the x and y coordinate // we don't add `const` because it will modify the value void set_x(const int &arg_x); void set_y(const int &arg_y); void set(const int &arg_x, const int &arg_y); // Getter // get the x coordinate and y coordinate // we add `const` so that it can access the value form a const object int get_x() const; int get_y() const; };
// main function int main() { // create a Point_2D object with initial values Point_2D p1(1, 2); // also create a `const` Point_2D object const Point_2D p2(3, 4); // create a `const` with another Point_2D object const Point_2D p3(p1); // print 3 instances of Point p1.print(); cout << endl; // getter can be used to access the member of a `const` object cout << "(" << p2.get_x() << ", " << p2.get_y() << ")" << endl; // also `print()` can be used to access the member of a `const` object p3.print(); cout << endl; // modify each instance with setter p1.set(3, 4); p2.set_x(5); // error, cannot modify the member of a `const` object p2.set_y(6); // error, cannot modify the member of a `const` object return 0; }
Rather than design a member function in a class, we can design a global function to process a class instance.
#include <iostream> using namespace std; class Point_2D // the class name // we use first upper case letter to indicate a class { private: // private data members & member functions // Data members // we use `m_` to indicate a data member // record the x coordinate int m_x; // record the y coordinate int m_y; // Private member functions // we use `_` to indicate a private member function // utility: check if the point is valid void _check_validity();
Guidelines: (again)
A Triangle:
A Vector:
A Integer Calculator: