Parte 1 del riassunto del corso di Algorithms and Parallel Computing tenuto dal prof Danilo Ardagna al Politecnico di Milano. File diviso in 4 parti perché troppo pesante. Questo è un riassunto completo e dettagliato degli argomenti fondamentali del corso (voto preso all’esame: 30L)
Scarica Algorithms and Parallel Computing - PARTE 1 e più Appunti in PDF di Algoritmi E Programmazione Avanzata solo su Docsity!
CONSTRUCTORS each class has To define how objecis of its type Can be initiauzed conSiruciors are special member funciions har control Objeci initializarion = =» it initializes the data members of a class. object * afe fun whenever. an object of a class type is created * have the same name as ine dass have no feturm Type have a paramerer ust and a funciion body and bcîh can be empty cicial a class can have multiple Construciors nat must differ in the number or types of their parameters e dirt le may not be dedafed as const but can write to consi Objecis during Aheir construction * DEFAULI CONSIRUCIORS : impuiciruy defiued by the compiler, take no OFgUMENTS — classes. must have a defautt consituctor initialize each dafa member of 4ne dass < use he in-class initializer if it exists otherwise, defanu- initiauze the member if we do not define any othef consucfofs it will be inpucitty defiwed bj the compier PROBLEMS +» fot some classes the default consiructor dog the wrong dhiuo. * sometimes the compier is unable to Symnesize one HIS almost alwayS fight to provide a defautr construcror if 0iner consifucrors ore being deued (ib USED = when an cojeci is defaur Or Vawe initialized ® Defouli inibialization : happens when we define non-Stalit Variobles or arrays. AT block Scope without initializers “ a class that itself has members of class type uses the synihesized default consiructor members of dass type ® Value initialization : happens * dufing array initioization when we provide fewer initializers than ine size of 4ne atray. * when we define a local Sstaric Object without an initiauzer When Wwe esplicitiy fequetr Vaw® initialization by writing TC) where T is a type name (ex. vector» v(10)) SYNTAX : Sales_data(const std::string &s, unsigned n, double p) : pookiol5), units_sold(n), revenue(p*n) { } If you define any constructors, C++ assumes you have defined all the ones you intend to be available and will not add any others ine member bookNo of ine cinss Sales_data is. iniiaizeà by S Sales_data(const std::string &s) : bookNo(s) { } <— au the paramerers inar are not expuciuy} defined are initialized ar defauu vawe Sales_data() : Sales_data(“”, 0, 0) { } L> deleRATINQ ONSITUCIOr: Uses anofner consirucror from its own class fo perform its initiabzation É members are initiauzea in order as they appear in ine class dedaration 4 different types of consiructors * COPY, ASSIGNMENT AND DESTRUCTION * tefaull cTor: takes zero argumenti + Objects are COPIED when + we initialize a variable + CODU CHOC : takes a reference. To another obj cf the same ge * we pass/fefurm an object by value *user-defined CTOF : intiouze fieias and take mpalever argumenii you apeity * we Use assignment Operator CONVESION CHOC: impucit, qakes a iagle argameni > objects are DESTROYED when - they cease to @xist * objecTs stofed in a vector are desttoged when the vecror is desîroyed «> if we do not define these operation, the compier wiù syaThesize them for us but synihezized version afe unlikely 40 work. coreeriy for classes thor allocate, fesources that reside ouîside the class cejers themselves Ce.g., use dynamic memory ) * TYPE MEMBER: ipo alias is a Synonym dor another type. SYNIAX: using SD = sales data typedef std::string::size_type pos; Lar that define -tgpes must appear before they are. used INITIALIZATION LIST + initialize fierds according to parameters în the ust 3 fer ta ci ta ipa dre folowing wo ore neorty Identico» POin':*Frint cont int no const into0€ MOLTO Le mine isttutteti ot siate € dala members ore ina n the } ‘are defined in me dass, COUt << “Point constructed”