CSC 1061 Week 02 ADT Classes and OOP PDF

Summary

This document covers various topics related to object-oriented programming (OOP) and abstract data types (ADTs) in C++. It includes discussions on classes, objects, constructors, and member functions in programming context. This document also includes information about header files, compilation techniques, and more.

Full Transcript

CSC 1061 OOP & ADT CLASSES OBJECTIVES AGENDA: WEEK 02 To understand abstraction and the role it plays 1. Jeopardy Semester in the problem-solving process. Assignment Specify and des...

CSC 1061 OOP & ADT CLASSES OBJECTIVES AGENDA: WEEK 02 To understand abstraction and the role it plays 1. Jeopardy Semester in the problem-solving process. Assignment Specify and design new classes using a pattern of information hiding with private 2. ADT and OOP member variables, constant member functions, and modification member functions 3. Classes, Objects, *this, Write a header file and separate static implementation file for any new class 4. Macro/Include Guards Create objects from the new class in test programs 5. Build/Make Project Identify situations in which member 6. Demo: Date Class functions and constructors can benefit from 7. TODO using default arguments and constructor initialers. 8. Resources for Help SEMESTER ASSIGNMENT Resource File: "categories.csv ADT: Abstract Data Type (Class) Player Question Record inherited class of std::string Data Structure / Container Classes Contestants – Bag of Players Category Filenames – Dynamic List Template Category of Questions – Linked List Template Board – BinaryTree Template OBJECT ORIENTED PROGRAMMING OOP A class, 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 OOP ADT: ABSTRACT DATA TYPES Built-In Simple Data User-Defined Types (ADT) Types in C++ Classes allow new data types to be int created by the developer float Characteristics – Properties: double Data members char Instances or Fields bool The class "has a" data Standard Library Classes members (Encapsulation) C++ std::string Operations / Behaviors / Tasks: class Member Functions CLASSES AND OBJECTS Class: House A class is a blueprint for the object We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. When the house is built it becomes an House Objects object with memory and values. When a class is defined, only the specification (blueprint) is set no memory or storage is allocated. Objects are created from the class. OOP IN C++ Complete 1.12. Object-Oriented Programming in C++: Defining Classes 1.12.1. A Fraction Class 1.12.2. Abstraction and Encapsulation HEADER FILES AND SOURCE FILES What's the difference between header files and source files? Header files are #included and NOT compiled, whereas source files are compiled and not #included. Header files contain declarations (function prototypes) Source files contain implementation (definitions) -NEVER #include "fileName.cpp" file! -ONLY #include or #include "programmer.h" 1. Only #include things you need to include 2. Guard against incidental multiple includes with include guards. HEADER FILES: #INCLUDE System header files: Files which are already available in C++ #include #include Programmer-defined header files (.h): These files are defined by the programmer and contain the declarations (interface) #include "programmerHeader.h" MACRO (INCLUDE) GUARD (CPLUSPLUS) An Include Guard (macro guard) is a technique which uses a unique identifier that you #define at the top of header files (.h files) Macro guards ensure that declarations do NOT get doubly declared Here's an example: #ifndef MYFUNCTS_H #define MYFUNCTS_H #include // function prototypes; #endif BUILD (MAKE) A MULTI-FILE PROGRAM 1. Preprocessor - #include header files - the preprocessor replaces the #include by the entire content of the specified header or file. 2. Compiler – compiles each source file (.cpp) checking syntax and if successful, generates corresponding object file (.o) 3. Linker – links all object files together and generates the application executable file (.exe) Source.cpp #include #include "myfuncts.h" Source.o main() {} Source.exe Myfuncts.cpp #include "myfuncts.h" Myfuncts.o Function definitions {} MAKEFILE: PROJECT FILE To create a multi-file project in CodeHS, the makefile (project file) must be updated so that all the source files (.cpp) will compile after all dependencies (.h) files are included. When updating CodeHS makefile, the example makefile in the article can be used as a template to follow. Compiler command: g++ Compiler Flags: -Wall (turns on most warnings) C++ MakeFile Tutorial: https://www.softwaretestinghelp.com/cpp- makefile-tutorial/ REVIEW CLASS AND OBJECTS Classes are (ADT) abstract data types: they contain data members and member functions. The most important property of a class is that it is a type, and as such, we can declare multiple objects of it. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Given: class Car{ … }; Car is the data type Car uberCar; uberCar is an object of Car DATA MEMBERS (ENCAPSULATION) - NOUNS A class "has a" data member(s). Encapsulation, ensures that "sensitive" data of a class is hidden from the user. Declare class data members as private Private data cannot be accessed from outside the class. To read or modify the value outside of the class, public get and set member functions must be created. Read the given article and make note of Why Encapsulation? CONSTRUCTORS A constructor allows an object to be created (constructed), and its data members initialized. All classes should write a default constructor. The default constructor enables an object with default values to be created (constructed). A default constructor does NOT take any parameters (). Name the 2 things that distinguish a function as a Constructor of a class as noted in the tutorial: https://cplusplus.com/doc/tutorial/classes/ CONSTRUCTORS (MANY OPTIONS) Constructors cannot be called explicitly as if they were regular member functions. They are only executed once, when a new object of that class is created (object is declared) Like any other function, a constructor can also be overloaded with different versions taking on many forms by having a different number of parameters different types of parameters Read 1.12.3. Polymorphism section through Listing 3: Activity: 1.12.3.1 Show method implementation and complete the activity CLASS MEMBER FUNCTIONS - VERBS A member function is a function that belongs to the class and performs tasks on the data members of the class. returnType class::memberFunction(type param, …) A member function can only be called on an object of the class object.memberFunction(); Member functions are Access functions – getters – the function returns a data member Modifier functions – setters – the function changes a data member to a new value, generally after satisfying a validation Other functions – any other function that performs a task on the data member(s). PYTHONTUTOR DEMO: DATE CLASS Visualize and Run the demo. Review the code for each of the following and label each part using a comment: Class declaration Constructors? Member Functions? Data Members? Class definition Constructors? Member Functions? Main driver Objects? Member function calls?

Use Quizgecko on...
Browser
Browser