200 likes | 275 Views
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)
E N D
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
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
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
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
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
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 { }
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 is the input "file" • cout is the output "file" • usage • cin>>x; // get a value • cout<<x; // output a value
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.
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
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 ?:
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
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.
Declare the methodsComplx.h #include <iostream> class Complx {private:// hidden member data double my_real_part, my_imag_part; // below are prototypes for the member functions (i.e.; "methods") public: Complx(double a, double b); // initializing constructor Complx( ) { }; // default constructor double get_real(Complx); // not shown Complxoperator + (Complx); // defines "+" as a non-member function // define what stream output means for a “Complx” variable friendstd::ostream&operator<<(std::ostream & , constComplx& ) }; // orange: declare parts of the class, green: use it, red: return-value
Implement the methods Complx.cpp #include <iostream> program need to know about “ostream” #include Complx.h program needs to know what the names mean // :: means the name after :: is a member of the class "Complx" Complx::Complx(double a, double b) // implement constructor {my_real_part=a; my_imag_part=b;} Complx::Complx() {} // default constructor ( required because of initializing constructor) ComplxComplx::operator+ (ComplxC) // define what “plus” means {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; } // define << for a Complx element friendstd::ostream &operator<<(std::ostream & xout, constComplx&C ) { xout<< C.my_real_part <<"+"<< C.my_imag_part<<"j"; return xout; // actually do the output operation }
"friend"s friendostream &operator<<(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
"friend"s - pt-2 • Can access privateand protectedmembers • NOTa member of the class • Allows operations across multiple classes • << is a member of the iostream class • Want << able to output private data in Complx
Our program to add Complx’s #include <iostream> #include “Complx.h” using namespace std; void main() { ComplxA(2,2); ComplxB(1,1),C; C=A+B; }