1 / 20

Object Oriented Programming

Object Oriented Programming. C++. ADT vs. Class. ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing how the functions and data are related An ADT implementation has several parts: interface function-names (methods)

brian
Download Presentation

Object Oriented Programming

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. Object Oriented Programming C++

  2. ADT vs. Class • ADT: a model of data AND its related functions • C++ Class: a syntactical & programmatic element for describing how the functions and data are related • An ADT implementation has several parts: • interface function-names (methods) • operators (possibly re-defined symbols) • data • Any/all of these may be public or private

  3. Classes • Collection of data elements and functions • Functions are called "methods" or members of the class • Tied to the data • May be hidden from other functions • Member access • exposed by "public" names • Hidden by "private" names

  4. Classes - example classComplx {public: float my_real_part; float my_imag_part; }; // note the semicolon!!!!! • Using "public" makes all elements "visible" • Using "private", would make them visible ONLY to methods (functions) inside the class (not shown). • NOTE: looks a lot like a struct

  5. Program structure – pt 2 - C++ • Usually, put the class definition in a header file • #include it later in BOTH : • the implementation file • where the methods are defined • the user-program file • where methods get used

  6. Objects • An object is just a struct with members • Default operations (members) • Constructor • Destructor • Assignment • Copy • YOU must provide any initialization code • in a constructor • YOU must provide return values (if any) • YOU must free any dynamic storage

  7. Constructors • C++ provides a "default" constructor • Only works if no parameters: Complx X; • Fails for uses like: Complx X(3,4); • variables get initialized • other setup must be done • IF you create a constructor, you MUST specify the Default Constructor too or it won't exist • Complx( ) { }; • no actual code needed inside the { }

  8. Class Constructors • Assign initial values to an object • Never returns a value • Same name as the class • Guaranteed to be called (automatic) upon instantiation of an object of type class

  9. Destructors • Automatically run when an object of type class goes out of scope • use to clean up after the object (return dynamically allocated storage to the storage management system • Same name as the class, but preceded by a ~ • no overloading allowed • never returns a value • not needed for statically allocated storage

  10. C++ console I/O • basic operators • << the "insertion" operator • >> the "extraction" operator • stream names for standard I/O • cin is the input "file" • cout is the output "file" • usage • cin>>x; // get a value • cout<<x; // output a value

  11. Operators • Ordinary variables have basic operators built-in + - * / % | & || && • New objects (structs, classes) may need more • How do you "add" 2 complex numbers? • Given: complex#'s a and b; • what does a+b mean??? • compiler knows nothing about complex #'s • Define a NEW action to perform "+" • but continue to use the "+" operator!!!! • add the real parts, add the imaginary parts • store each result in the proper answer-part.

  12. Operators – 2 • Often, we "want" to re-use an operator • i.e.; give a new meaning to "+" • this is called "overloading" • let "+" mean "add complex numbers" as well as add integers, floats, etc. • c=a+b; // a, b & c are Complex • May need a "friend" function, due to flaws in the C++ object model • "friend" functions do not get the leading "classname::" syntax

  13. More on Overloading • = overload this as a member function • == != <= >= <>overload as a non-member (friend function) returning a boolean • >> << (insertion and extraction) overload as non-members (friends) returning type iostream • + - * / %(arithmetic) as overload non-members • += and -=... overload same as + and - • cannot overload :: .sizeof ?:

  14. Polymorphism (multiple forms) • x=sqrt(y); // what data type is "y"? • could be: float, int, double, real • How does compiler know how to handle the incoming data? • An object allows us to create MULTIPLE methods • all have the same name • all have different parameter types • compiler links caller's code & the "right one" based on parameter types. • Object writer creates these multiple methods

  15. Operators • Ordinary variables have basic operators built-in + - * / % | & || && • New objects (structs, classes) may need more • How do you "add" 2 complex numbers? • Given: Complex#'s a and b; • what does a+b mean??? • compiler knows nothing about Complex #'s • Define a NEW action to perform "+" • add the real parts, add the imaginary parts • store each result in the proper answer-part.

  16. Example – Declare the methods class Complx {private:// hidden member data float my_real_part, my_imag_part; // below are member functions (i.e.; "methods") public: Complx (float a, float b); // initializing constructor Complx( ) { }; // default constructor float get_real(Complx) { return my_real_part; } // not shown Complxoperator + (Complx); // defines "+" as a member name // define what output means friendstd::ostream&operator<<(std::ostream&xout, constComplx&C ) { xout<< C.my_real_part <<"+"<< C.my_imag_part<<"j"; return xout; // modifies meaning of “cout” } }; // orange: declare parts of the class, green: use it, red: return-value

  17. Example- implementation // :: means the name after :: is a member of the class "Complx" Complx::Complx(float a, float b) // implement constructor {my_real_part=a; my_imag_part=b;} ComplxComplx::operator+ (ComplxC) {Complxtmp; // need a place for output tmp.my_real_part= my_real_part + C.my_real_part; tmp.my_imag_part= my_imag_part + C.my_imag_part; return tmp; } // orange: declare parts of the class, green: use it, red: return-value

  18. "friend"s friendstd::ostream&operator<<(std::ostream&xout, constComplx&C ) { xout<< C.my_real_part <<"+"<< C.my_imag_part<<"j"; return xout; } • What does it do? • Remember that we needed to define + for complex numbers? • Now need to define how to output them • Output functions don't understand class data • So we have now overloaded: • the + operator • the << operator

  19. "friend"s - pt-2 • Can access privateand protectedmembers • NOTa member of the class • Allows operations across multiple classes • <<is a member of the iostreamclass • Want << able to output private data in Complx

  20. Example-pt 2 void main() { ComplxA(2,2); ComplxB(1,1), C; C=A+B; }

More Related