Unformatted text preview:

COP3330 Exam Notes Chapter 6 Slide Notes Some goals of OOP To make VERY LARGE programs Better organized Easier to comprehend Easier and better managed To create classes which can easily be ported from one programming situation to another To provide a class interface to application programmers allowing them to use the class but not alter it and thereby mess it up Encapsulation Inheritance Polymorphism Group data and functionality together Hide implementation details so that they are protected Allows one data type class to acquire properties of other data types classes A fruit is edible An apple is a fruit Therefore an apple is edible Allow objects to act differently in different situations A steering wheel is used the same way whether the context is a car truck boat power steering or manual class a structured data type used to represent an ADT abstract data type an ADT combines both a domain of values and operations on those values class member a component of a class may be either data values or functions object an instance of a class Structures versus Classes Structures Typically all members public No member functions Classes Typically all data members private Interface member functions public Technically syntax same By convention both style and behavior very different mechanisms A struct is similar to a class in syntax However in conventional usage very different struct members are typically ONLY data members no functions struct members are all public by default Semicolon after structure definition MUST exist struct WeatherData double temperature double windVelocity semicolon is required Classes are similar to structures but Adds member FUNCTIONS Not just member data Adds access controls private public etc Integral to object oriented programming Focus on objects Object Contains data and operations In C variables of class type are objects Dot and Scope Resolution Operator Used to specify of what thing they are members what class Dot operator Specifies member of a particular object Scope resolution operator Any data type includes Specifies what class the function definition comes from Data range of data Operations that can be performed on data Same with classes Principles of OOP Information Hiding Data Abstraction Encapsulation But WE specify the data and the operations to be allowed on our data Details of how operations work not known to user of class Details of how data is manipulated within ADT class not known to user Bring together data and operations but keep details hidden Data in class almost always designated private in definition Upholds principles of OOP Hide data from user Allow manipulation only via operations Which are member functions Public items usually member functions are user accessible C class definitions Should separate two key parts Interface what user application programmer needs Implementation details of how class works Chapter 7 Slide Notes Constructors Initialization of objects Initialize some or all member variables Other actions possible as well A special kind of member function Automatically called when object declared Very useful tool Key principle of OOP Defined like member function except Must have same name as class Cannot return a value not even void Default Constructor Defined as constructor w no arguments One should always be defined Auto Generated If no constructors AT ALL are defined Yes If any constructors are defined No If no default constructor Cannot declare MyClass myObject Use of Const Protect parameter with const Protect ALL such parameters Makes parameter read only This includes class member function parameters Vector Basics Similar to array Has base type Stores collection of base type values Declared differently Syntax vector Base Type Indicates template class Any type can be plugged in to Base Type Produces new class for vectors with that type Example declaration vector int v v is vector of type int Calls class default constructor Empty vector object created Indexed like arrays for access But to add elements Must call member function push back Member function size Returns current number of elements Chapter 8 Slide Notes Overloading operators VERY similar to overloading functions Operator itself is name of function Most operators can be overloaded as a member of the class a friend of the class or a nonmember nonfriend 1st operand is the calling object overloaded operator inputs a matrix friend istream operator istream input Matrix matrix overloaded operator outputs a matrix friend ostream operator ostream out const Matrix overloaded operator add two matrices const Matrix operator const Matrix matrix const overloaded operator subtract two matrices const Matrix operator const Matrix matrix const overloaded unary operator negate two matrices const friend Matrix operator Matrix matrix overloaded operator multiply two matrices const Matrix operator const Matrix matrix const overloaded operator increment a matrix const Matrix operator overloaded operator decrement a matrix const Matrix operator overloaded operator check equality of two matrices const bool operator const Matrix matrix overloaded operator sets all values to a certain number const Matrix operator const int num Is it possible using operator overloading to change the behavior of on integers Why or why not NO When overloading an operator at least one of the arguments to the operator must be of a class type This prevents changing the behavior of for integers When to make function const Constant functions not allowed to alter class member data Constant objects can ONLY call constant member functions Good style dictates Any member function that will NOT modify data should be made const Use keyword const after function declaration and heading Generally we should not overload and the comma If the overloaded is defined as a member function then the first argument is a calling object and must be a class instance Nonmember vs member overloads Nonmember Member Advantage automatic type conversion of all arguments is possible Disadvantage no direct access to class private data must call gets and sets Advantage direct access to private data Disadvantage asymmetric calls with problem of no type conversion on left side of operand Friend Functions Nonmember functions Recall operator overloads as nonmembers They access data through accessor and mutator functions sets and gets Can be inefficient overhead of calls Friends can directly access private class data No overhead more efficient don t


View Full Document

FSU COP 3330 - Chapter – 6 Slide Notes

Download Chapter – 6 Slide Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter – 6 Slide Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter – 6 Slide Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?