Tuesday, December 9, 2014

cs nov 6

shape s cannot be declared as the contructor for shape is protected...
Triangle T ( inherited for shape) is ok  since derived classes can use protected member functions such as the constructor..



Triangle

     -add(POint);  vector<Point> points


Polygon also inherits everything from shape. it also has a vector of points and and has the add method for it.

struct Triangle : Shape
{
  Triangle(p1,p2, p3, c)
  :
  {
    p.add(point1);
  };
}

struct Polygon : Closed_polyline {
  {
      void add(Point p);  // add is different from shape so it is being overridden from Shape, there is an invariant no parallels and intersection..
    void draw_lines() const;
 };


open_polyline <- Closed_polyline<- Polygon

Polygon <- Triangle  (so not needing a separate storage for points.

public :

Point point(int i) const {returns points[i]; }  // read only access to points vector in the shape class.


Triangle - > Point (Association relationship, Point does not know about Triangle but Triangle can see point)

violet -- metauml diagram

Inheritance is a relationship between classes.

Association/Aggregation/Composition -- relationship between class and the objects in it or between objects. specific/concrete.

3 Relationships
is-a
has-a
uses

After refactoring ::

class Triangle : Polygon
{
    Triangle ( )
  {
       this.add();
   // the number of points cannot be more than 3;
}

cs Nov 4

Study Graphlib
ch12-16

-ch 17
ch18-21 parts  (template), memory management

differences between procedural and Object oriented programming..


ADT -- hides details set of values and operations on those values.. (abstract interface), design stage, language neutral

API -- Application Porgrammers Interface eg. Graphlib  (concrete interface)

       - details of how to use the methods (functions, operations) of the classes

      - the interface, declarations, parameters.

diff between api, adt .. api shows all the interface details , adt hides the details, complexity.., implementation stage, specific to the implementation

UDT - User defined type
         - built in types..

Line class is not overriding the add method, as invoking the shape add method..

for Lines class derived from shape, are the two methods add, draw_lines

virtual -- specify polymorphism..  ie. the compiler won't wait to make the decision to which defined method to use , it would pick the method to use at compile time..

2 requirements to implement polymorphism.
-- delay the binding of calling to run time..

1. invoke the function using a  pointer or reference to the object. sh->draw() or f(Shape& s)
2. invoke a virtual function

Polymorphism

-- compile time (function operator overloading - static binding)
-- run time (dynamic binding)

Difference between overloading and overriding..
overloading -- diff parameter list
overriding -- same parameter list..

Lines -- add(Point p1, Point p2) ; overloading..
add is not polymorphic as it is not declared using the virtual keyword.
draw_lines() is polymorphic as it is declared using the virtual keyword in the base class - shape

move, draw_lines is virtual.
number_of_points is using points vector to return the number of points in shape class
return type is unsigned integer so converting it to int..

Lab :

UML diagram take a look to make it work.
ask why you got 9 and not 10 :)
use the setmask to mask and show only one picture at a time.
refactor the code using inheritance for Polygon instead of composition

Shape<- Polygon<- Triangle
later member functions in their own file.
Design Stage language independent

Thursday, December 4, 2014

cs quiz 5

write code to increment..

Date d = wed;

 d =Day(int(d) + 1); // for class enum have to do this way.

for plain enumeration the compiler will promote(widening conversion) the value to int if needed.

or plain enum (ie. not class) compiler will promote (cast0 convert widening. d = Day(d+1);

Date dates[10]; // won't work without default constructor;

Date (int y = 2000 , int m=1, int d = 1); /// all 3 are optional..

Date d1;

default constructor for Date has no parameters...

Date::Date(){}

cs Oct 30



CppDoc

Create an API from your code

JavaDoc for Java

Shape -- Circle
          -- openPolyline
              -- closedPolyline
                  -- Polygon
                   -- Triangle

Later rewrite Triangle to inherit directly from Polygon rather than inheriting from Shape.

Polygon P;

P.add(Point(100.200));

function overloading means same name different parameter list.. could take a different number or different type or both.
function overriding  -- same name, same parameter list. This can only occur in an inheritance hierarchy. (there is a hidden parameter passed which is pointer to the object type)



protected : keeps it in the family. (inheritance hierarchy). access is ok within the family ie. classes derived derived from it but not outside..

but is the same as private if the class is standalone.

struct Triangle : Polygon

For square just add 4 points instead of 3.

inherit only the protected and public but not the private data members... by the derived from the base class

editor ../.ash to check the history and copy from there.

create a shell file

https://en.wikipedia.org/wiki/Class_diagram








cs Oct 28

Oct 28

function overloading -operator overloading

f(2);
void f(int)
f()
void f()

compitle time binding -- early binding

run-time binding - late binding


Inheritance -- is-a
aggregation -- contains, has-a

ch12 - ch16

a point is a circle (could be with radius = 0)
a circle is a point (could be with radius )
a circle has a point
a point has a  circle

compile-time binding -- static
runtime-binding -- dynamic

runtime binding

Shape *sh;
Triangle T;
Square S;

T.draw();
S.draw();
Sh = &T;
Sh->draw();

or a function

f(Shape & s)
{
 s.draw()
}

inheritance hierarchy using pointers
using ponter to to the base object...

in C++ this is implemented using virtual functions.

add keyword virtual  (for this to work the compiler must add code to make it happen)
virtual void draw();  // possibly runtime polymorphism

invoke the function using a pointer.

1. invoke the function using a  pointer or reference to the object. sh->draw() or f(Shape& s)
2. virtual keyword.

Graphlib, javadoc

compile chapter.12.3.cpp

1. install  fltk-1.1.10-dev.tcz to access fltk dependent tools/libraries especially fltk-config

- Build the author's library
-compile chapter.12.3.cpp
-tangram lab(1) development

4th parameter is default..

sudo  find -name "simple_window.h"
sudo find / -name Fl.h



g++ -I ../GUI   chapter.12.3.cpp -lbookgui -L../GUI -fno-rtti `fltk-config --ldflgs --use-images`

Libraries lib___.a

compile -l libraryname(ending with .a)

atoi : ascii to integer..

1 < atoi("1")  <cstdlib>

/GUI directory ran make



editor GUI.cpp
added <cstdlib>
qualified atoi by std::atoi

fltk-config

fltk-config --compile program.cxx
fltk-config --libs

/usr/local/lib/libfltk.a

   --ldflags

-lfltk

RTTI (Runtime type Instantiation)

cd

adding -fno-rtti in makefile of /GUI for CXXflags

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=fitting%20the%207%20tangram%20shapes%20into%20a%20square%20algorithm

Building the tangram square with the seven pieces.


cs Oct 23



Class Enumeration v/s Plain Enumeration

Month::jan

Month m = jan;

enum class Month, have to always use Month::jan , scope to use

enum Month

FINAL : given enum for Month do an operloading for the ++ operator.

lab5.cpp.pdf has a dependency on CreditCards.tex

after every operation , do a check_invariant..

credit_cards.cpp

#include "credit_cards.h"
#include <stdexcept>

bool CreditCards::invariant_violation()
{
   bool r = (coomonlimit != 1000 and  ..;
  for (CreditCard* x: cards) if (x->limit !=  commonlimit) r = true; // r should be true ?
  return r;
}

CreditCards::CreditCards(unsigned int limit)
:commonlimit(limit)
{
  if (invariant_violation()) throw std::runtime_error("invariant violation");
}

void CreditCards::add(CreditCard* c)
{
    if(cards.find(c) ! = cards.end()) throw std::runtime_error("pre-condition violation");
  cards.insert(c);
  if (invariant_violation()) throw std::runtime_error("invariant violation");

}

void CreditCards::card_delete(CreditCard* c)
{
   if (invariant_violation()) throw std::runtime_error("invariant violation");
}

void CreditCards::withdraw(CreditCard* c, unsigned int)
{
   if (invariant_violation()) throw std::runtime_error("invariant violation");
}
void CreditCards::deposit(CreditCard* c, unsigned int)
{
   if (invariant_violation()) throw std::runtime_error("invariant violation");
}

void CreditCards::withdrawAvail(CreditCard* c)
{
   if (invariant_violation()) throw std::runtime_error("invariant violation");
}

CreditCars cc(3000); fail_("should be failed to create cc with invalid limit");
catch(runtime_error) {succeed_(); }
try(CreditCards cc(5000); succeed_(); }
catch(runtime_error) { fail_("should not have failed to create cc with valid limit");}
 CreditCard c1(1000);
 CreditCards cc(2000);
 try(cc.add(&c1); fail_("illegal to have different limit");} catch(){succeed_();}
 CreditCard c2(2000);
 try(cc.add(&c2); succeed_();}fail_("illegal to have different limit");} catch(){succeed_();}

cs Oct 21



struct is not adequate
-doesn't allow you to protect the integrity of the objects data

 has correct values

class -- class invariant
           in C++ describes the valid values
   integrity of the data , data hiding , data encapsulation

 allows us to use encapsulation and data hiding to protect the data.

Why would you defined the constructor (pg 14) definition outside the class versus defining it inside the class definition.

typically in a different file (.cpp))

-- you link to update definition without recompiling your code.

-- helps to clarify the interface when reading the code.

may be mre efficient to define -- inline function rather than in class definition.

con if not inline it : making function calls

int main()
{
   f();
   f()
  f()
  f()

{
eg.. if f has 10 lines then the total lines of code are 14 but if f is inline then there are 40 lines of code as the calls are replaces by the actual code.
when to inline it and when not.. if the number of lines in the function are very less then it would be better to inline it as the cost of calling a function maybe mch more..

recursive function would be impossible to inline it..

Pg 16

 class Date{

public:
   class Invalid {};

}
Date::Date

{

    if !(check) throw Invalid();

}

In exception handling

 catch (Date::Invalid)

constructor would be Date::Invalid::Invalid
summaries of types/exceptions/classes. (Pg 17)

an enumerations creates a  new type whic is the subset of integer
two versions of enumeration.

1.enum class Month (ne in C++)  -- enumerators are class scope

2. enum Month -- enumerators are global scope.

Pg 18 internal representation of enum should not matter. almost constants...

cout << dec <<x ; { output in decimal}

cout << hex<<x;  // output in hex format.


const

const int x = 0; // constant value
  int f(const int  &x) // constant reference
{
}

const Date& f(int x)  // return a reference to the Date which cannot be changed.. : what is returned cannot be changed
{
}

int f() const ; // cant modify the data members of the class. -- object.

http://www.cplusplus.com/forum/general/12087/

global scope



 Month m = feb;

Month h = Month::feb ; // class scope

Scoped enum

1. clash with the scope definitions.
2. the int can be used to make invalid comparison...  which will compile correctly

Operator Overloading

overload some operator

 x + y ; Date x, int y

x + y

Date operator+(Date d, int a)
{

    d.add_day(a);
    return d;
}

enum class Month{
  Jan=1, Feb. ..
};

Month Operator++(Month& m)
{
    m =(m == Dec) ? Jan : Month(int(m)+1);
    return m;
}

insertion operator or output operator..

vector<string> month_tbl;

ostream& operator<<(ostream& os, Month m)
{
   return os << month_tbl[int(m)];
}

  ostream& os, const Date &d) ; // as there are three members in a date
{
    return os << d.y <<" "<<d.m<< " "<<d.d;
}

Lab ::

1. Build the doc.
2. Build the executable
3. Write Unit tests for all the seven member functions. (pass, fail) so min 14

cp/mnt/sdc1/CreditCards.tex

copy , shift insert to paste to terminal

All credit cards have the same limit..

P in Object specification indicates Powerset Or all the subsets in the set..
class invariant :  all cards have the same limit.

tce-load -wio ImageMagick