Download presentation
Presentation is loading. Please wait.
Published byMabel Carroll Modified over 9 years ago
1
IT PUTS THE ++ IN C++ Object Oriented Programming
2
Why O-O? It makes data the main focus Objects translate from real-world objects Allows for abstraction (Too many details!) Allows for encapsulation for security and reuse Promotes modularity Design, develop and test classes independently Promotes reuse (did I mention reuse?) Project management Memory management Stack vs. heap control over allocation and deallocation of memory Dynamic vs. static
3
The Header of the Class The class structure is the blueprint for future objects Class components are allowed to be private Mostly for protecting data Data can only be shared through a public function that YOU (the programmer) will control Class components are allowed to be public Mostly for sharing functions. Member function prototypes listed here Can be accessed (called) directly from a client’s function main() Other non-member function
4
The Header of the Class Class structure is an interface Provides a list of member functions without revealing details Provides parameters of each function and correct usage Info for client about what is available The class file is called the header file Filename ends in.h
5
Class Structure Interface Header file class.h Contains public and private components: data members and member functions
6
Class Interface Reveals what client can use in class and how Reveals to implementer what it needs to work Lists data and operations Data members (fields) Member functions (methods) Use public/private sections to control access to clients
7
Preconditions and Postconditions Communicates what a function accomplishes without indicating how it accomplishes it Contract between two programmers Client programmer Implementation programmer Precondition statement indicates what must be true before a function is called Required state or range of data being passed to the function Postcondition statement indicates what will be true when the function finishes its work What to expect from the function
8
Access to Data Members Members declared on stack, but can also be declared in heap Client can access public features only—error returned for calls to private members Member functions can access private data members and return to client Accessor method: method that returns or displays private data Control over data formatting Control over which data is accessible and in which state Mutator method Controls how data is modified Controls where data is saved Can constrain changes to a specified range Can enforce business rules for data
9
Implementation Contains complete member function code Kept in separate.cpp file in IDE project folder Must include #include “class.h” System header file in default location #include Client header file in current (IDE project) directory Preprocessor replaces #include line with entire contents of the file Function name gets class name identifier for class level scope Class::functionName Otherwise function is non-member function (global scope)
10
Class Structure InterfaceImplementation Header file class.h Implementation file class.cpp Contains public and private components: data members and member functions Code for member functions Keyword before member function name class:: #include “class.h”
11
Interface and Implementation Separate Separate physical files for interface and implementation Must keep them in sync; change to any part of prototype must be changed in both files
12
Accessing Private Data in Public Functions All data members are accessible directly within a member function No object needed Dot notation not necessary Can call other member functions (both public and private) from within a member function
13
Class Constructor Constructor Member function that initializes all data member functions to a valid state You must write it--not automatically set to default Unique Prototype No return type Exactly same name as class Can have parameters, but if provided the client must use them Can be overloaded to provide client more than one option for parameters Otherwise, just like other member functions Must be included in class definition and implementation file Must have class:: keyword in implementation
14
Destructor Function to clean up after an object is destroyed Called automatically on delete call or when leaving object scope Unlike the constructor, you don’t always need a destructor Can use it to remove very large objects from memory if memory is an issue Unique prototype Exactly same name as class, but prefixed with ~ (you’ll find this symbol above the tab on most keyboards) No parameters No return type
15
Client Program Client uses class as an abstraction Invokes public operations only Internal implementation not visible Client can’t and shouldn’t change internals Class data should be private Interface is used by the client programmer to use the class Get information to use objects of that class, without having to manage functionality associated with it.
16
Data Storage Client program will declare object on stack Each data member defined in the class is associated with that object declaration Client program can declare many objects on stack of same class Each object has its own copy of each data member defined in that class If a client program accesses data for an object, it is accessing the data for that copy of the member data for that class only.
17
Driver Client program could be a lot like a driver. Create objects of classes to be used Call functions of those objects Call non-member client functions To handle specific tasks of client Otherwise, may have no original code master controller program
18
Client Structure Uses #include Uses #include “class.h” Can have using namespace std; Client file has.cpp extension
19
Class Structure ClientInterfaceImplementation Client file program.cpp Header file class.h Implementation file class.cpp Code uses objects of classContains public and private components: data members and member functions Code for member functions Prototype for required function: int main( ) Keyword before member function name class:: #include “class.h” #include using namespace std; #include “class.h”
20
How to Use Objects Private Member Variables – How To Access Them… Public Member Functions From Member function implementation? From Client or non- member function? Variable name (no object)Not accessible – is there another way? From Member function implementation? From Client or non- member function? function_name(*) *Provide data if required object.function_name(*) *Provide data if required
21
Non-Member Functions Non-member functions: Not part of a class Not part of a client Can be implemented in either Class implementation file Client file Global scope Does not have access to private class data Has access to public class functions (must use an object)
22
Helper Function Private member function of a class Cannot be called by a client or non-member function (with or without an object)—it’s private! Can be called by another member function of the same class only Needed to help one or more public member function of the same class Can be used to provide private data manipulation needed as a precondition of a member function
23
Friend Function Non-member function with access to private class data Prototype in class header—public or private Includes friend keyword in prototype in header file only Implemented in implementation.cpp file without the class:: keyword Because it is a non-member function, the client calls this function without an object: function ( ) not object.function( ) Friend function can access private data, but not directly—must use an object of the class and dot operator: object.variable
24
How To Use Functions Interface Prototype Implementation Declaration Member Function Non-MemberFriend.h file in class { }Not in class { }.h file in class { } with friend keyword void print();friend Ratio sum(Ratio, Ratio); MemberNon-MemberFriend.cpp file class:: keyword Can be.cpp file if prototype in.h No keyword.h file after class { } No keywords void Ratio::print() {…} Ratio sum(Ratio x, Ratio y) { …} Private data accessible without object Private member data not available Private data accessible with object x._num = 10;
25
How To Use Functions Client Member FunctionNon-MemberFriend Client.cpp file Attached to object Client.cpp file Not attached to object Client.cpp file Not attached to object t.print();Sum(r,s);
26
Operator Overloading Syntax convenience Way of allowing more than one way to use the class as an object Client can create object and provide data to set the intial state of member variables OR Client can create object without providing data (contructor will set intial state) Way of using common operators for objects Example: + vs function called add
27
Overloaded Operator Precedence To overload for an operator, implement function named operatorX (Where X represents the operator you are interested in) For a class member function, the left hand side of the equation is the receiver object and other operands are passed as arguments f1 + f2 sum = f1.operator+(f2) For a non-member function, two operands are on equal footing f1 + f2 sum=operator+(f1, f2)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.