Download presentation
Presentation is loading. Please wait.
1
C++ Training Datascope Lawrence D’Antonio Lecture 4 An Overview of C++: What is a Class/Object?
2
What is a class? A class is a set of objects sharing common features. A class defines an object’s attributes and behavior. Methods are provided to act on an object and to pass messages between objects. A class is the basic unit of abstraction. A class is the basic unit of modularity. A class can be concrete or abstract.
3
Class Design as Type Design Scott Meyers, Item #19 How should objects of your new type be created and destroyed? How should object initialization differ from object assignment? What does it mean for objects of your new type to be passed by value?
4
Class Design as Type Design 2 What are the restrictions on legal values for your new type? Does your new type fit into an inheritance graph? What kind of type conversions are allowed for your new type? What operators and functions make sense for the new type?
5
Class Design as Type Design 3 What standard functions should be disallowed? Who should have access to members of your new type? What is the “undeclared interface” of your new type? How general is your new type? Is a new type really what you need?
6
What is an object? An object is an instance of a class. An object has state, behavior, identity.
7
What is an object? Coad-Yourdon An abstraction of something in a problem domain, reflecting the capabilities of the system to keep information about it, interact with it, or both; an encapsulation of attribute values and their exclusive services.
8
What is an object? OMG An object is a thing. It is created as the instance of an object type. Each object has a unique identity that is distinct from and independent of any of its characteristics. Each object offers one or more operations.
9
What is an object? Firesmith An object is defined as a software abstraction that models all relevant aspects of a single tangible or conceptual entity or thing from the application domain or solution space. An object is one of the primary entities in an object-oriented application, typically corresponds to a software module, and consists of a set of related attribute types, messages, exceptions, operations, and optional component objects.
10
What is an object? Booch From the perspective of human cognition, an object is any of the following: A tangible and/or visible thing. Something that may be apprehended intellectually. Something toward which thought or action is directed.
11
What is an object? Booch continued. An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class; the terms instance and object are interchangeable.
12
What is an object? Shlaer-Mellor An object is an abstraction of a set of real- world things such that: All the things in the set have the same characteristic. All instances are subject to and conform to the same set of rules and policies.
13
What is an object? Jacobson An object is characterized by a number of operations and a state which remembers the effect of these operations.
14
What is encapsulation? Internal details of objects are concealed from the objects users (information hiding). Both data and implementation may be hidden. The object is a black box. Access to members is controlled through the class definition. The accessible part of a class is called its interface.
15
Data encapsulation example class Clock { private: int hours; // 1-12 private int minutes; // 0-59 public: Clock(int h, int m) { if (h 12) { throw(”Hours must be between 1 and 12″); } if (m 59) { throw(”Minutes must be between 0 and 59″); } h = hours; m = minutes; } //... };
16
Class Invariants The above is an example of “Programming by Contract.” The class guarantees that These are called class invariants
17
Data Members Data members can be declared as: const – a declaration that an object is read only. The object may be stored in a CPU register. volatile – a declaration that an object’s value may be changed asynchronously. The object may not be stored in a CPU register.
18
Data Members 2 static – A data member shared by all objects of a class. There is only one copy of a static member, it is not part of the object memory layout. mutable – A data member that is allowed to be modified, even if it a member of a const object.
19
Is the following code legal? struct X { static int a = 2; }; main() { X my_x; X::a = 4; my_x::a = 5; }
20
Not legal! Cannot initialize static data member a within class. static variables are similar to extern variables.
21
Is the following code legal? struct X { static int a; }; main() { X my_x; X::a = 4; my_x::a = 5; }
22
Not legal! This is a linker error. X::a was used but never defined.
23
Is the following code legal? struct X { static int a; }; int X::a; main() { X my_x; X::a = 4; my_x::a = 5; }
24
Legal. X::a was defined before being used. Note: it is okay that X::a was not initialized.
25
Is the following code legal? struct X { static int a; static const int b = 3; }; int X::a = 2; main() { X my_x; X::a = 4; my_x::a = X::b; }
26
Legal. static const members can be declared and defined at the same time.
27
Is the following code legal? void func(volatile std::list li) { int n = li.front(); } Not legal! Only volatile member functions can be called on a volatile object. list::front() not a volatile function
28
What is a method? A method is a member function that acts upon an object. A method is generally called for one object (exception: static members). Commonly found methods are constructors, destructors, assignment, mutators, accessors.
29
Static Members #include using namespace std; class X { public: int a; void f(int b) { cout << “X::f()\n”;} }; int main() { int X::*ptiptr = &X::a; //pointer to data member void (X::* ptfptr) (int) = &X::f; //pointer to member function X xobject; xobject.*ptiptr = 10; (xobject.*ptfptr) (20); }
30
What is message passing? Messages are transfers of data or requests for another object to take an action.
31
Message passing example Adapter pattern
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.