Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
CS331 Lecture #2

2 Ch. 2 an introduction to classes and objects, and how member functions operate on objects.

3 C struct [ . . . . ] struct Fraction { int numer, denom;
string description; }; structdemo/demostruct.cpp

4 Class Definition C++ has another datatype called class which is very similar to struct. A class definition looks like this: class ClassName { public: publicMembers private: privateMembers };

5 Header files To define a class (or any other type) we generally place the definition in a header file, preferably with the same name as the class, and with the .h extension.  classes/fraction.h

6 .cpp files The implementation file has .cpp extension.
The definition of any class member outside the class definition requires a scope resolution operator of the form ClassName:: before its name. Every identifier has a scope, a region of code where a name is “known” and accessible. Class member names have class scope. classes/fraction.cpp

7 Member Access Specifiers
Client code is code that is outside the scope of the class, but which uses objects or members of that class. client code #includes the header file that contains the class definition. classes/fraction-client.cpp

8 Encapsulation Encapsulation is the first conceptual step in object-oriented programming. It involves: Packaging data with the functions that can operate on that data in well-named classes Providing clearly-named and well-documented public functions that allow users of the class to do whatever needs to be done with objects of this class Hiding implementation details The set of public function prototypes in a class is called its public interface. The set of non-public members, as well as the function definitions themselves, comprise the implementation.

9 Qt Creator Qt Creator is a new cross-platform IDE designed for development of C++/Qt apps. Windows, Mac, and Linux platforms

10 UML UML, the Unified Modeling Language, is a language for object oriented design. UML class diagrams can show the important elements and relationships between classes in a concise and intuitive way. UML is much more than just class diagrams. Other kinds of UML diagrams can illustrate how classes collaborate with one another and how users interact with classes. Most diagrams were created with a design tool called Umbrello.

11 UML Relationships

12 Friends of a Class The friend mechanism makes it possible for a class to allow non-member functions to access its private data. The keyword friend is followed by a class or a function declaration. friend declarations are located inside a class definition. A friend can be a class, a member function of another class, or a non-member function.

13 Example class Timer { friend class Clock;
friend void Time::toString(); friend ostream& operator <<(ostream& os, const Timer& obj); [...] private: long m_Elapsed; };

14 Constructors A constructor (ctor), is a special member function that controls the process of object initialization. Each constructor must have the same name as its class. Constructors do not return anything and do not have return types.  ctor/complex.h ctor/complex.hcpp

15 Destructors A destructor, (dtor), is a special member function that automates cleanup actions just before an object is destroyed. The destructor's name is the classname preceded by the tilde (~) character. It has no return type, and takes no parameters. Therefore, it cannot be overloaded.

16 Static static Local Variables static Class Members Block-scope static
Data member member function statics/static.cpp Block-scope static static initialization statics/static-test.cpp

17 Class Declarations and Definitions
circular/badegg/egg.h circular/badegg/chicken.h

18 Class Declarations and Definitions
Forward class declaration: circular/goodegg/egg.h circular/badegg/chicken.h circular/goodegg/egg.cpp

19 Copy Constructors and Assignment Operators
A copy constructor is a constructor that has a prototype like this: ClassName(const ClassName & x); An assignment operator for a class overloads the symbol = and gives it a meaning that is specific to the class. There is one particular version of the assignment operator that has the following prototype: ClassName& operator=(const ClassName& x); lifecycle/copyassign/fraction.cpp lifecycle/copyassign/copyassign.cpp

20 Conversions A constructor that can be called with a single argument (of a different type) is a conversion constructor, because it defines a conversion from the argument type to the constructor's class. Example 2.19. src/ctor/conversion/fraction.cpp

21 const Member Functions
When a class member function X::f() is invoked through an object x x.f(); we refer to x as the host object. const can be applied to non-static class member functions. Example 2.20. src/const/constmembers.cpp

22 Subobjects An object can contain another object, in which case the contained object is considered to be a subobject. Example 2.21. src/subobject/subobject.h Example 2.22. src/subobject/subobject.cpp


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google