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) – operators (possibly re-defined symbols) – data • Any/all of these may be public or private 2 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 3 Classes - example class Complx {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 4 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 5 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 6 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 { } 7 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 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 C++ console I/O • basic operators – << the "insertion" operator – >> the "extraction" operator • stream names for standard I/O – cin – cout is the input "file" is the output "file" • usage – cin>>x; – cout<<x; // get a value // output a value 10 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. 11 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 12 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 nonmembers • += and -= ... overload same as + and • cannot overload :: . sizeof ?: 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 ADT 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 14 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. 15 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 Complx get_real(return my_real_part); // not shown Complx operator + (Complx); // defines "+" as a member name // define what output means friend ostream &operator<<(ostream &xout, const Complx &C ) { xout << C.my_real_part <<"+"<< C.my_imag_part<<"j"; return output; } }; // orange: declare parts of the class, green: use it, red: return-value 16 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;} Complx Complx ::operator+ (Complx C) {Complx tmp; // 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 17 "friend"s friend ostream &operator<<(ostream &xout, const Complx &C ) { xout << C.my_real_part <<"+"<< C.my_imag_part<<"j"; return output; } • • • • • 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 18 "friend"s - pt-2 • • • • • Access private and protected members NOT a member of the class Allows operations across multiple classes << is a member of the iostream class Want << able to output private data in Complx 19 Example-pt 2 void main() { Complx A(2,2); Complx B(1,1),C; C=A+B; } 20