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