Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++...

Post on 22-Dec-2015

215 views 2 download

Transcript of Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++...

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

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

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 { }

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

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

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 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

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

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 constructorComplx( ) { }; // default constructor

double get_real(Complx); // not shown Complx operator + (Complx); // defines "+" as a non-member function

// define what stream output means for a “Complx” variablefriend std::ostream &operator<<(std::ostream & , const Complx & )

}; // orange: declare parts of the class, green: use it, red: return-value

17

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)Complx Complx ::operator+ (Complx C) // define what “plus” means {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; }

// define << for a Complx elementfriend std::ostream &operator<<(std::ostream & xout, const Complx &C )

{ xout << C.my_real_part <<"+"<< C.my_imag_part<<"j"; return xout; // actually do the output operation }

18

"friend"s

friend ostream &operator<<(ostream &xout, const Complx &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 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

20

Our program to add Complx’s

#include <iostream>#include “Complx.h”using namespace std;void main(){ Complx A(2,2);

Complx B(1,1),C; C=A+B;}