1 / 15

COP330 Object Oriented Programming in C++ Fall 2008

COP330 Object Oriented Programming in C++ Fall 2008 . Dr. David A. Gaitros dgaitros@admin.fsu.edu. Introduction to Object Oriented Programming (OOP). OOP combines the data and functions that manipulate the data into a single package called objects. Data – The object’s attributes

kirk
Télécharger la présentation

COP330 Object Oriented Programming in C++ Fall 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP330Object Oriented Programming in C++Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

  2. Introduction to Object Oriented Programming (OOP) • OOP combines the data and functions that manipulate the data into a single package called objects. • Data – The object’s attributes • Functions – The object’s behavior • The “Packages” have the favorable Software Engineering property of Information Hiding • Objects know how to communicate with each other via well-defined interfaces • Implementation details are hidden

  3. Introduction to Object Oriented Programming (OOP) • C++ is a language that allows you to program using the OOP style. • You can program in C++ using non-OOP styles. • In OOP the unit of programming is called a class. • An object is an item that is an instantiation (actually define a variable ) of a class. • Remember that a class has both data and functions combined.

  4. Introduction to Object Oriented Programming (OOP) • C++ Classes • A programmer creates their own user-defined types called classes. • Classes do not set aside storage or define variables. They only tell the compiler what a class looks like. • These are also called programmer-defined types. • Each class contains data and functions to manipulate the data • Data components of a class are called data members. • Function are called member functions. • An instance of a user-defined type is called an object.

  5. Introduction to Object Oriented Programming (OOP) • Remember that the C language is a proper subset of the C++ language. So you can take a C program and give it to a C++ compiler and it will compile fine. • You can actually do OOP style in the C language but it is more difficult because of the lack of definition of a class. We use C “struct” concepts.

  6. Introduction to Object Oriented Programming (OOP) • C “Struct” Struct Time { int hour; int minute; int second; }; Time Todays_Time; Time Time_Array[10];

  7. Introduction to Object Oriented Programming (OOP) • So what is the difference: • In C, Data and functions are always separate • In C++, you can build objects that have both the data and functions put together. C++ Libraries can contain useful classes that store data in useful and reusable ways. Classes then become the building blocks for more complex programs.

  8. Introduction to Object Oriented Programming (OOP) • There are three parts in a C++ program related to objects: • DDU • Declare: Define the types of data that will be used, give them names, declare the functions and their interface. • Define: Put the details into the class. Write the code and the implementation details. • Use: The actual code that uses the class.

  9. Introduction to Object Oriented Programming (OOP) • Protected levels in a class: • Public: Can be accessed both within and external to the object. • Private: Can only be used by the object itself. • The public section of a class is usually called the interface. It is what the programmers sees. • Normally, All data is private and only the bare essential functions are made public.

  10. Introduction to Object Oriented Programming (OOP) • Reason’s for hiding • Reduces complexity much like the detailed workings of an automobile are hidden from the average driver. • Principle of least privilege (need-to-know) • More Secure. Less likely that someone will alter or misuse something. • Easier to change the implementation without affecting other modules that use it.

  11. Introduction to Object Oriented Programming (OOP) class <className> { public: // public data // public functions private: // private data // private functions };

  12. Introduction to Object Oriented Programming (OOP) • Example of a Definition of a class TimeType class TimeType { public: void Set(int, int, int) void Increment(); void Display(); intgethours(); intgetminutes(); intgetseconds(); private: int hours; int minutes; int seconds; };

  13. Introduction to Object Oriented Programming (OOP) • Constructors • Is a special member function visible to the user that has the same name as the class. • It has no return type nor “void” • It is called when an object is declared and is used to set up the object initially. Only called once during initialization. • You can have more than one constructor but each must have unique parameters.

  14. Introduction to Object Oriented Programming (OOP) • Example of a Definition of a class TimeType with contstructor class TimeType { public: TimeType(); TimeType(int h; int m; int s); void Set(int, int, int) void Increment(); void Display(); intgethours(); intgetminutes(); intgetseconds(); private: int hours; int minutes; int seconds; };

  15. Introduction to Object Oriented Programming (OOP) // Examples TimeType TodaysTime; TimeType Yesterday(12,24,26); // The first one uses the // constructor with no parameters. // We assume the code will assign // today’s date. // The second uses the constructor with // three parameters to set the actual // hour, minute, and second.

More Related