1 / 59

Advanced Program Design with C++

Advanced Program Design with C++. Aggregate data types: struct class. Aggregate data types. Aggregate data type: array, struct and class Array: collection of values of same type Struct : collection of values of different types Class: a structure that can also contain functions

jspillman
Télécharger la présentation

Advanced Program Design with C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Program Design with C++ Aggregate data types: struct class Joey Paquet, 2007-2019

  2. Aggregate data types • Aggregate data type: array, struct and class • Array: collection of values of same type • Struct: collection of values of different types • Class: a structure that can also contain functions • Treated as an aggregated entity that can be manipulated as a unit. • A structor a class is a user-defined data type, so it must first be declared prior to declaring any variables of this new type. Joey Paquet, 2007-2019

  3. struct Joey Paquet, 2007-2019

  4. Structure vs. class • C++ expanded the notion of struct from C to create classes. • For backward compatibility, the struct syntax was kept is C++. • As classes are an expanded struct, it is also possible to declare and use a struct that includes member functions, and that even inherits from another struct, just like classes. • The only difference between class and struct is that the members of a struct are public by default, but private by default for a class. • A struct that does not use object-oriented features is nicknamed a POD for “plain old data structure”. Joey Paquet, 2007-2019

  5. Using a POD • Once the structhas been declared, it can be used as any other type • The member of a struct can be referred to using the dot notation Joey Paquet, 2007-2019

  6. Using a POD Joey Paquet, 2007-2019

  7. Using a POD Joey Paquet, 2007-2019

  8. Using a POD • Structures can be initialized, assigned, passed as parameters to functions, or referred to by pointers. • Pitfall: if a structure contains a pointer variable, then its memory allocation/deallocation must be done explicitly for that member as, for example, assigning a structure to another would only copy the pointer value and not do a copy of the value pointed to by the pointer, i.e. a deep copy. • As soon as one declares any data structure that contains a pointer, great care has to be taken to ensure that the memory allocated to the object pointed to by the pointer is managed correctly. • We will see more about that when we talk about classes. Joey Paquet, 2007-2019

  9. class Joey Paquet, 2007-2019

  10. Class declaration • A class is an Abstract Data Type, i.e. a data type that defines both the data contained in the data elements it defines, as well as the behavior and/or constraints that applies to elements of this type. • A C++ class represents an abstract data type by allowing functions to be syntactically encapsulated into its definition, along with its data elements. • The syntax of a class definition is the same as for a struct. • As opposed to Java classes, C++ allows to have class declarations that do not include the definition of the functions it encapsulates. • This enables the possibility to separate the class declaration from its implementation code, which provides the implementation of the member functions of a class. • Class declaration(in the.h file): Joey Paquet, 2007-2019

  11. Declaring and using objects • Variables of a class type are referred to as objects. • Objects are declared using the same syntax as for basic types. DayOfYear today; DayOfYear *birthday = new DayOfYear(); • Class types can be used anywhere a type can be used: • As type of a variable • As parameter to a function • As a return type to a function • As a value pointed to by a pointer • Members of a class are referred to using the dot notation and their access is regulated by the private, public and protected access specifiers. • The members of an object can be referred to using the dot notation: int day1 = today.getDay(); int day2 = *birthday.getDay(); // equivalent int day3 = birthday->getDay(); // equivalent Joey Paquet, 2007-2019

  12. Class implementation • Use the scope resolution operator to specify to which class the member functions that you define belong to (in the .cpp file). Joey Paquet, 2007-2019

  13. Header file, implementation file • Good physical design of your program dictates that you should have: • Class declarations in header files (.h) • Implementation code for all member functions in a corresponding implementation file (.cpp) • The implementation file is the compilation unit, and needs to #include its corresponding header file, as it does not contain a class declaration. • If your program uses free functions, or free operators, then: • Free function and free operator headers go in the header file. • Free function and free operator implementation go in the implementation file. Joey Paquet, 2007-2019

  14. Kinds of methods • Member functions can be categorized as: • Accessor: method whose goal is to expose a value in the state of an object, generally the value of a particular data member. • Mutator: method whose goal is to change a value in the state of an object. • Service method: method that exposes some service or desired behavior to other classes/objects. • Internal behavior methods: methods that are used by the class/object itself internally, that defines some of its behavior that should not be triggered explicitly from the exterior. These methods should thus be private. • Constructor: method whose goal is to manage the operations to be performed as an object is created. • Destructor: method whose goal is to manage the operations to be performed as an object is destroyed. Joey Paquet, 2007-2019

  15. inline functions and methods Joey Paquet, 2007-2019

  16. Inline functions/methods • A function (or method) can be declared as inline, meaning that its entire code is to be replacing any call to this function. • Function inlining aims at execution optimization by eliminating the function call mechanism overhead. • However, it has the disadvantage of leading to code bloat if the body of the inline function contains a large amount of code and/or is consuming a large amount of memory and is called frequently at different places in the code. • To define a function as inline, one needs to include the inlinekeyword as a prefix to the function definition. The function declaration does not need to have theinlineprefix. Joey Paquet, 2007-2019

  17. Inline functions/methods • If the function is a method, one can provide the implementation code of the method in the class declaration, implicitly stating that the method is to be considered inline. • However, one might also declare the method in the regular fashion in the class declaration, and prefix the method definition with the inline prefix. OR Joey Paquet, 2007-2019

  18. Inline functions/methods • Note that declaring a function/method as inline is only a hint for the compiler, and that the compiler may choose not to inline a function if it is not possible, e.g. for recursive functions. • Also note that the compiler can only inline a function if the definition of the function is available in the compilation unit where it is called. • Rule of thumb: Inline your functions that have very short function definitions. Accessor methods are very good candidates for method inlining. Joey Paquet, 2007-2019

  19. const specifier Joey Paquet, 2007-2019

  20. The constspecifier • Specifies that its subject is constant, i.e. that its value cannot be changed after it is set. Can be applied to: • Variables: A variable may be declared as const, which signifies that its value cannot be changed after it is initialized. It assumes that the variable is initialized upon declaration. • Function parameters: Declaring a function parameter as const means that the value of this parameter cannot be changed by the execution of the function. • Methods: Declaring a method as constmeans that this function will not alter the state of the object to which it belongs. By extension, only methods declared as const can be called on an object that was itself declared as const upon declaration. Joey Paquet, 2007-2019

  21. static specifier Joey Paquet, 2007-2019

  22. Static members • A member of a class can be declared as static, which means that this member is a class member, i.e. it belongs to the class as a whole an can be called on the class rather than on the objects. • If it is a data member, its value is unique across all objects of the class, i.e. it is a “class global variable”. • If a member function is declared as static, it can only use other members that are also declared as static. • Used to define state and behavior that is independent of any particular object’s state instantiated from this class. • Static member variables are generally initialized outside of the class declaration. Only constant static integral members can be initialized inside of the class declaration. The initialized value must be a constant expression. • Other languages allow static classes (e.g. C#), which C++ does not have. Joey Paquet, 2007-2019

  23. Static members: use and initialization • Pitfall: There are variations of these restrictions depending on the specific version/standard of C++ you use. Joey Paquet, 2007-2019

  24. Static variables • Non-member variables can also be declared as static, which has a slightly different meaning: • When you declare a variable or function at file scope the static keyword specifies that the variable or function has internal linkage, i.e. it cannot be referred to outside of the compilation unit in which it is declared. • When you declare a local variableto a function as static, the variable has static duration, i.e. its value is kept even when the execution goes out of the scope of this function. In other words, it is a “global variable that has local scope to this function”. Joey Paquet, 2007-2019

  25. Static variables • Allstatic variables persist from the start of the execution of the program until the program terminates. • Depending on where it is used, the static keyword has different meaning for non-member variables: • Variabled has local scope and no external linkage. • So does variable c, plus, itretains its value even when the f() function is not being executed, and is initialized only once. • Both a and b can be accessed from the point of declaration by all the code to the end of the file. But a can be used in other files because it has external linkage. Joey Paquet, 2007-2019

  26. Static: sum-up • To sum-up: • A variable declared static within the body of a function maintains its value between invocations of the function. • A variable declared static within a file scope is accessible by all functions within that compilation unit. However, it is not accessible by functions from other compilation units. • Free functions declared static within a compilation unit may only be called by other functions within that compilation unit. • static class members exist as members of the class rather than as an instance in each object of the class. There is only a single instance of each static data member for the entire class. • Non-static member functions can access all data members of the class: static and non-static. • static member functions can only operate on the static data members, or call other static member functions. Joey Paquet, 2007-2019

  27. friends Joey Paquet, 2007-2019

  28. Friends • In principle, private and protected members of a class cannot be accessed from outside the class in which they are declared. • A friend (function or class) of a class may access the members designated as private or protected. • Friends are functions/methods or classes declared as such within a class. • Friends are not members, so they are not inherited from a superclass. • Widely criticized for being a “rogue feature”: • Makes the language more complex to implement. • Breach of the information hiding principle. • If overused, creates a proliferation of exceptions to information hiding, and thus confusion in the design. Joey Paquet, 2007-2019

  29. Friends (free function friend) Joey Paquet, 2007-2019

  30. Friends (free function friend) Joey Paquet, 2007-2019

  31. Friends (friend class) Joey Paquet, 2007-2019

  32. Friends (friend class) Joey Paquet, 2007-2019

  33. Friends (member function friend) Joey Paquet, 2007-2019

  34. Friends (member function friend) Joey Paquet, 2007-2019

  35. Constructors and destructors Joey Paquet, 2007-2019

  36. Constructors and destructors • C++ relies on constructors and destructors for the creation and destruction of objects. • A constructor is called any time an object is instantiated: • Using a variable declaration (allocated on the stack): DayOfYear birthday; • Using a pointer variable and calling the newoperator (allocated on the heap): DayOfYear *today = new DayOfYear(); • During an explicit call to a constructor: birthday = DayOfYear(); • The destructor is called any time an object is to be destroyed: • When going out of a function scope, all variables declared in that function scope are destroyed by automatically calling their destructor before the stack frame is popped from the function call stack. • When the delete operator is explicitly called on a pointer variable. Joey Paquet, 2007-2019

  37. Constructors and destructors • To sum-up, variables allocated on the stack are destroyed as we get out of the block`s scope, but objects allocated on the heap using new need to be manually destroyed using delete. • Rule of thumb: for every new, there should be a corresponding delete somewhere. Joey Paquet, 2007-2019

  38. Constructor declaration and implementation • A constructor is a member function that has the same name as its class. • Can have as many constructors as needed. • The “default constructor” is the constructor that does not take any parameter. It is the one called when no specific constructor is mentioned during instantiation. • If you include no constructors in your class, the compiler will provide an automatically generated default constructor for basic memory allocation of the non-pointer data members only. • If you include at least one constructor, it will not be generated, thus you may end up having no default constructor defined. • Rule of thumb: if you define your own constructor(s), make sure that you provide a default constructor, as the default constructor is likely to be called implicitly by the runtime system. Joey Paquet, 2007-2019

  39. Constructor declaration and implementation • Constructors are differentiated by their parameter lists. • Parameters are generally used to pass values for data members initialization. • The call to the default constructor should not use parentheses, as it would make it syntactically equivalent to a function header declaration: DayOfYear date3(); // function header declaration Joey Paquet, 2007-2019

  40. Constructor declaration and implementation • For the implementation, constructors are easily recognizable by being referred to as className::className: • C++ also provides a constructor initialization listfeature that allows one to declare how are the initialization mappings done between the parameters of the constructor and the data members of the class: • The preceding two examples are implementing two constructors that are equivalent. Joey Paquet, 2007-2019

  41. Constructor declaration and implementation • Constructors can also act as guards. • A guard verifies that certain preconditions are met before proceeding and may reject the operation if preconditions are not met. • The constructor may verify that the values passed as parameters to the constructors are valid according to the specifications of the variable they are to initialize. • If valid preconditions are not met, then the constructor may report an error or throw an exception. • Including guarding conditions in your constructors leads to more robust implementation. Joey Paquet, 2007-2019

  42. Constructor declaration and implementation • Constructors can also be explicitly called in the initialization list of the constructors of a class that contains a data member which is an object of another class: Joey Paquet, 2007-2019

  43. inheritance Joey Paquet, 2007-2019

  44. Inheritance: basics • Inheritance is a mechanism by which a class can be defined as a refinement of an existing class, adding supplemental data members and/or member functions to an already existing class, or redefining an inherited member function adapted to the intended behavior of the derived class. • Most object-oriented programming languages implement an inheritance mechanism, which may be implemented with different restrictions, possibilities, and run-time mechanisms. • Inheritance allows for more flexible and extensible design possibilities by allowing the definition of generic concepts to be refined into many sub-concepts that share common traits included in the generic concepts they inherit from. • Additional mechanisms can be added to simple inheritance, such as multiple inheritance or abstract classes, which C++ implements. Joey Paquet, 2007-2019

  45. Inheritance: terminology • A base class is a class from which other classes inherit from. • Often called a super-class, or a parent or ancestor class. • A derived class is a class that inherits from a base class. • Often called a sub-class, or a child or descendent class. • Multiple inheritance is when a derived class inherits from more than one base class. • An abstract class is a class that is meant to be derived from by containing abstract member functions that do not have a definition, assuming that they will find a concrete implementation in a derived class. Because of that, an abstract class cannot be instantiated. • Will come back to abstract classes later when we cover polymorphism. Joey Paquet, 2007-2019

  46. Inheritance: what is inherited? • A derived class inherits all the following members from its base class(es): • Data members • Member functions • The inheritance relationship is a transitive one, meaning that a derived class also inherits the members of all its ancestor classes. • A derived class does not inherit the following members of its base class(es): • Constructors, copy constructor • Assignment operator • Destructor • Upon use, all of these are automatically called in sequence by the run-time system, but in many cases careful explicit implementation is required. Joey Paquet, 2007-2019

  47. Inheritance: example Joey Paquet, 2007-2019

  48. Inheritance: overriding vs overloading • A derived class can explicitly add new behavior, or provide different behavior for the member functions that it inherited by overriding or overloading its inherited member functions. • Overriding is when a member function is included in a derived class, having exactly the same signature as an inherited member function. • A function`s "signature" is defined as: • Function’s name • Sequence of types in parameter list • Including order, number, and types of parameters • Signature does NOT include: • Return type • const keywords used for the function or the parameters • References specifiers (&) used for the parameters Joey Paquet, 2007-2019

  49. Inheritance: overriding vs overloading • Overloadingis when two or more functions with the same name are defined in the same scope, each having different parameter lists. • Thus, overloading applies to inherited member functions, but also applies to free functions or operators available within a certain scope. • In the previous example: • Constructors are overloaded • printcheck()function is overridden Joey Paquet, 2007-2019

  50. Inheritance: constructors and destructors • Base class constructors are NOT inherited in derived classes • The run-time system automatically calls the default constructor of the class after it transitively calls the default constructors of all its ancestor classes when and object is created. • If classes have constructors explicitly defined, they can explicitly call the appropriate base class constructor. • Explicitly calling a superclass constructor is done in the initialization list. • Note that Java’s “super()” does not make sense in C++, as we can have multiple inheritance. • Base class constructor should initialize its own member variables • They are inherited by the derived class, but the derived class constructors should not be responsible for them. The same rule applies for destructors. • So derived class constructor simply calls a base class constructor to take care of its inherited members. • This way, the inherited members are always initialized in the same manner across all classes derived by a particular base class. • As destructors cannot be overloaded, they cannot be explicitly called. They should only be called implicitly by the runtime system. • Very important part of object-oriented design, especially in C++. Joey Paquet, 2007-2019

More Related