The c ++ Programming Language Lecture 4 Object-Based Programming
The C++ Programming Language Lecture 4: Object-Based Programming
Basic Concepts and Syntaxes
Basic Concepts and Syntaxes
You have no class Class object c++C u Class- user defined data type More operations not only operators but also functions a Object-an instance or running entity of certain class Class vs, object Class a static concept object, a dynamic concept One class definition multiple object entities
Class & Object ◼ Class – user defined data type ◼ More operations, not only operators but also functions ◼ Object – an instance or running entity of certain class ◼ Class vs. Object ◼ Class, a static concept; Object, a dynamic concept ◼ One class definition, multiple object entities
Concepts about class u Class components From the view of Type Data members ( Built-in, user-defined), or Attributes Member functions(function, operator), or Methods, Behaviors From the view of role Public Interface Private Implementation Encapsulation Class users only access public interface, need not be aware of the internal details of implementation No matter how dramatically the internal implementation changed client codes need not be modified if interface is fixed ■ struct in c++ a Taken as a special type of class which has no member functions, and all data members are public
Concepts about class ◼ Class components ◼ From the view of Type – Data members (Built-in, user-defined), or Attributes – Member functions (function, operator), or Methods, Behaviors ◼ From the view of Role – Public Interface – Private Implementation ◼ Encapsulation ◼ Class users only access public interface, need not be aware of the internal details of implementation ◼ No matter how dramatically the internal implementation changed, client codes need not be modified if interface is fixed ◼ struct in C++ ◼ Taken as a special type of class, which has no member functions, and all data members are public
Design our own classes A simple start-a string stack class u An important LIFo data structure Required operations Push- add an element to the stack Pop-take an element out from the top of stack Full -query whether the stack is full Empty -query whether the stack is empty a Size-query the number of elements in the stack Peek -access the element on the top of the stack, but not to pop it out
Design our own classes ◼ A simple start – a string stack class ◼ An important LIFO data structure ◼ Required operations: ◼ Push – add an element to the stack ◼ Pop – take an element out from the top of stack ◼ Full – query whether the stack is full ◼ Empty – query whether the stack is empty ◼ Size – query the number of elements in the stack ◼ Peek – access the element on the top of the stack, but not to pop it out