1 / 64

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

cgee
Download Presentation

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

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

  3. struct Joey Paquet, 2007-2014

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

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

  6. Using a POD Joey Paquet, 2007-2014

  7. Using a POD Joey Paquet, 2007-2014

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

  9. class Joey Paquet, 2007-2014

  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 de 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: Joey Paquet, 2007-2014

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

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

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

  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. • Constructor: method whose goal is to manage the operations to be performed as an object is instantiated. • Destructor: method whose goal is to manage the operations to be performed as an object is destroyed. Joey Paquet, 2007-2014

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

  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. • 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-2014

  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. Joey Paquet, 2007-2014

  18. Inline functions/methods • Note that declaring a function 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-2014

  19. const specifier Joey Paquet, 2007-2014

  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. Very often used in conjunction with reference parameter passing. • 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-2014

  21. static specifier Joey Paquet, 2007-2014

  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 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. • Other languages allow static classes (e.g. C#), which C++ does not have. Joey Paquet, 2007-2014

  23. Static members: use and initialization Joey Paquet, 2007-2014

  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 it is declared in. • When you declare a variable as static, the variable has static duration, i.e. its value is kept even when the execution goes out of its scope. In other words, it is a “global variable that has local scope”. Joey Paquet, 2007-2014

  25. Static variables • All the static variables persist until program terminates. • Depending on where it is used, the static keyword has different meaning for non-member variables: • Variabled has local scope and no linkage. • Variable cretains 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 until the end of the file. But a can be used in other files because it has external linkage. Joey Paquet, 2007-2014

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

  27. nested and local classes Joey Paquet, 2007-2014

  28. Nested classes • A class can be declared inside another class, either as public or private. • If private, it can only be used inside of the outer class. • If public, it can be used outside of the outer class as: OuterClass::InnerClass • i.e. the inner class’ name is local to the outer class. Joey Paquet, 2007-2014

  29. Nested classes • The class FirstWithin is visible outside and inside Surround, as it is public. • FirstWithin's constructor and its member function getdvar are also globally visible. • The data member d_variable is only visible to the members of the class FirstWithin. • The class SecondWithin is only visible inside Surround. The public members of the class SecondWithin can also be used by the members of the class FirstWithin, as nested classes can be considered members of their surrounding class. • SecondWithin's constructor and its member function getdvar also can only be reached by the members of Surround(and by the members of its nested classes). • SecondWithin::d_variable is only visible to SecondWithin's members. Joey Paquet, 2007-2014

  30. Local classes • A class definition can also be defined inside a function definition, called a “local class”. Rules that apply to the use of local classes: • Global variables declared above the function can be used with the scope resolution operator "::". • Static variables declared inside the function can also be used. • Automatic local variables cannot be used. • It cannot have static data members. • Member functions must be defined inside the local classes. • Enclosing functions cannot access the private members of a local class. Joey Paquet, 2007-2014

  31. friends Joey Paquet, 2007-2014

  32. 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 or classes declared as such within a class. • Widely criticized for being a “rogue feature”: • Makes the language more complex to implement. • Breach to the information hiding principle. • If overused, creates a proliferation of exceptions to information hiding. Joey Paquet, 2007-2014

  33. Friends (free function friend) Joey Paquet, 2007-2014

  34. Friends (free function friend) Joey Paquet, 2007-2014

  35. Friends (friend class) Joey Paquet, 2007-2014

  36. Friends (friend class) Joey Paquet, 2007-2014

  37. Friends (member function friend) Joey Paquet, 2007-2014

  38. Friends (member function friend) Joey Paquet, 2007-2014

  39. Constructors and destructors Joey Paquet, 2007-2014

  40. 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 scope, all variables declared in that scope are destroyed by automatically calling their destructor before the stack frame is popped from the stack. • When the delete operator is explicitly called on a pointer variable. Joey Paquet, 2007-2014

  41. 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-2014

  42. 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 members. • 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. Joey Paquet, 2007-2014

  43. 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-2014

  44. Constructor declaration and implementation • For the implementation, constructors are easily recognizable by being referred to as className::className: • C++ also provides a constructor initialization list feature 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-2014

  45. 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-2014

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

  47. inheritance Joey Paquet, 2007-2014

  48. Inheritance: basics • Inheritance is a mechanism by which a class can be defined as a refinement of a 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-2014

  49. 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-2014

  50. 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 inherit the members of all its ancestor classes. • A derived class do not inherit the following members if 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-2014

More Related