Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class #3

Closed
jimin-kiim opened this issue Oct 20, 2022 · 6 comments
Closed

Class #3

jimin-kiim opened this issue Oct 20, 2022 · 6 comments

Comments

@jimin-kiim
Copy link
Owner

jimin-kiim commented Oct 20, 2022

@jimin-kiim
Copy link
Owner Author

Data Abstraction and Class

Data Abstraction is a concept and the class is fundamental unit of data abstraction in C++

  • Data Abstraction
    • creating classes with appropriate functionality, which hide the unnecessary details
    • a programming and design technique that relies on the separation of interface and implementation.
  • Data Abstraction is realized by class in C++. Using the class means utilizing the data abstraction

@jimin-kiim
Copy link
Owner Author

Components of class

  • Behavior(method, member functions, actions)
    • the actions that an instance can perform in response to a request.
    • parts of the method
      • name : matched to a message to determine
      • signature : the combination of return type and argument type. methods with the same name can be distinguished by different signatures
      • body : the code that will be executed when the method is invoked in response to a message
  • State(variable, attributes, data members, data fields)
    • the data that an object must maintain in order to successfully complete its behavior.

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Oct 20, 2022

Message

  • a message is always given to some object, called the receiver.
  • the action performed in response is determined by the receiver, different receivers can do different actions in response to the same message
  • message passing syntax : ${message receiver}.${message selector} (${arguments});

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Oct 20, 2022

Avoiding data members in the public interface

  • making all the member variables to be private variable is desirable
  • why
    • for consistency
    • for precise control over the accessibility of data members
      • precise controls : read-only, read-write, write-only
    • functional abstraction
      • can replace the data member with a computation not directly using it
  • accessor(getter) : a method that simply returns an internal data value
    • to make the data read-only
    • provides better documentation that the data field is accessible
    • make it easier to later change the access behavior
  • mutator(setter) : a method used to change the variable of an object

Visibility Modifier

  • differentiate the interface and implementation views of a class
  • public
    • external view (interface, sevice)
    • can be seen and manipulated by anybody (all clients)
  • private
    • internal view(implementation)
    • accessible only by the class implementators
    • can be accessed only within the member functions of the class
  • protected

@jimin-kiim
Copy link
Owner Author

Writing memory safe classes

  • Avoid making copies of objects. Copying an object is expensive
  • Avoid creating new objects. Try to reuse existing objects
  • Use const reference parameters when appropriate
  • Use const member functions
  • Use pointers instead of references as data members.
  • Avoid storage allocation in the default constructor. Delay it until the member is accessed.
  • Use reference counting wherever possible.

Resist tricks in the initial stage

  • Stick with safety first; make sure no memory leaks exist
  • Don’t worry about optimization in the early stages of development.

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Dec 15, 2022

Client’s responsibilities with classes and functions

  • Understand the purpose of the class.
  • Clearly understand what the class implementor expects from you, the client.
  • Pay attention to every member function; in particular, const member functions are safer
  • Understand the arguments being passed.
  • Understand what your responsibility is when pointers and references are returned from functions.
  • If the class documentation and the information in the header file differ, get clarification as to which is correct one before using the class
  • Prefer functions that accept arguments that are pointers and references to const. They are safer.
  • Don’t rely on any undocumented class details that the class implementor might have told you. Stick to the class interface and documentation.
  • Be wary of classes that don’t implement the minimum set of member functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant