CSC 1060 Week 12 Classes and OOP PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
This document covers concepts of object-oriented programming (OOP) in C++. It details classes, objects, constructors, and member functions. The document discusses various OOP principles such as inheritance and polymorphism.
Full Transcript
CSC 1060 CLASSES AND OOP OBJECTIVES AGENDA: WEEK 12 Use a built-in class data type and 1. OOP understand the data encapsulation and 2. Objects and Member Functions functional behaviors of the class 3. Intro OOP Concepts Apply...
CSC 1060 CLASSES AND OOP OBJECTIVES AGENDA: WEEK 12 Use a built-in class data type and 1. OOP understand the data encapsulation and 2. Objects and Member Functions functional behaviors of the class 3. Intro OOP Concepts Apply class abstraction to develop 4. ADT: Abstract Data Types software solutions. 5. Object Oriented Programming: OOP Recognize situations in 6. ADT – Abstract Data Type: Class which inheritance can simplify the implementation of a group 7. 4 Pillars of OOP of related classes 8. Demo: Date Class PythonTutor Recognize situations in which creating 9. Inheritance an abstract base class will allow the 10. Demo: Employee is a Person later implementation of many derived classes that share 11. Polymorphism underlying functions. 12. TODO & Resources for Help OBJECT ORIENTED PROGRAMMING OOP A class, creates represents a custom data type or ADT (abstract data type). A class encapsulates data and functionality around a central idea that becomes reusable. The data is kept private The class creates public functionality to apply to the data OBJECTS AND FUNCTIONS We have not taken advantage of the features C++ provides to support object-oriented programming (OOP). Strictly speaking, these features (OOP) are not necessary. For the most part they provide an alternate syntax for doing things we have already done, but in many cases the alternate syntax is more concise and more accurately conveys the structure of the program. INTRODUCTION TO OOP CONCEPTS Read the article, Introduction to OOP concepts in C++ https://www.enjoyalgorithms.com/blog/introduction-to-oops- concepts-in-cpp What is the difference between a default constructor and parameterized constructor? ADT: ABSTRACT DATA TYPES User-Defined Types (ADT) Classes allow new data types to be created by the developer Characteristics – Fields: Data members (C++) = Instance Variables/Fields (Java) The class "has a" data members (Encapsulation) Operations - Behaviors: Member Functions (C++) = Instance Methods (Java) DATA MEMBERS #ifndef DATE_H Data members (data for each object) #define DATE_H should be kept private class Date { // private data members to ensure the security int month = 1; of the data int day = 1; Default access is int year = 1970; private if no access public: category is indicated. //... }; #endif CONSTRUCTORS Constructors are the member function of the class used to create an object and initialize the data members What is special about constructors? 1. Name of function matches the name of the class 2. No return type All classes should write a default constructor (constructor that takes NO arguments) Date::Date() { } // default constructor Date::Date(int initMonth, int initDay, int initYear) { // other constructor setDate(initMonth, initDay, initYear); } MODIFIER MEMBER FUNCTIONS A modifier member function will modify void Date::setDate(int inMonth, int inDay, the private data int inYear) { member(s) if(inMonth >= 1 && inMonth = 1 && inDay 1000) @param: new data to set a data year = inYear; member to } @return: void ACCESSOR MEMBER FUNCTIONS A accessor member function will access the private data member The function should return the private data member and matches the return type to the private data member An accessor member function takes no arguments An accessor member function is a constant function int Date::getDay() const { return day; } int Date::getMonth() const { return month; } int Date::getYear() const { return year; } 4 PILLARS OF OOP 1. Encapsulation 2. Abstraction 3. Inheritance 4. Polymorphism STD::STRING CLASS (CPLUSPLUS) A class encapsulates data and functionality as members of the class Private Data Member: the data is not directly accessible outside the class sequence of characters that represent the std::string object's data Types of Member Function Constructors – std::string function creates the object ▪ How many constructors does std::string have? Getters – find, size, length, empty Setters – insert, replace, erase, clear Others – operator >>,