COMP 345 Advanced Program Design with C 1

  • Slides: 43
Download presentation
COMP 345 - Advanced Program Design with C++ 1 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 1 Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Aggregate data types: struct class Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 2 Aggregate data types • Aggregate

COMP 345 - Advanced Program Design with C++ 2 Aggregate data types • Aggregate data type: array, struct and class • Array: collection of values of same type • Structure: 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 struct or a class is a user-defined data type, so it must first declared prior to declaring any variables of this new type. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 3 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 3 Click to edit Master title style struct Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 4 Structure vs. class • C++

COMP 345 - Advanced Program Design with C++ 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”. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 5 Using a POD • Once

COMP 345 - Advanced Program Design with C++ 5 Using a POD • Once the struct has been declared, it can be used as any other type • The member of a struct can be referred to using the dot notation Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 6 Using a POD Concordia University

COMP 345 - Advanced Program Design with C++ 6 Using a POD Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 7 Using a POD Concordia University

COMP 345 - Advanced Program Design with C++ 7 Using a POD Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 8 Using a POD • Structures

COMP 345 - Advanced Program Design with C++ 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 9 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 9 Click to edit Master title style class Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 10 Class declaration • A class

COMP 345 - Advanced Program Design with C++ 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: Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 11 Declaring and using objects •

COMP 345 - Advanced Program Design with C++ 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. Day. Of. Year today; Day. Of. Year *birthday = new Day. Of. Year(); • 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 day 1 = today. get. Day(); int day 2 = *birthday. get. Day(); // equivalent int day 3 = birthday->get. Day(); // equivalent Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 12 Class implementation • Use the

COMP 345 - Advanced Program Design with C++ 12 Class implementation • Use the scope resolution operator to specify to which class belong the member functions that you define. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 13 Kinds of methods • Member

COMP 345 - Advanced Program Design with C++ 13 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 14 Inline functions/methods • A function

COMP 345 - Advanced Program Design with C++ 14 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 inline keyword as a prefix to the function definition. The function declaration does not need to have the inline prefix. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 15 Inline functions/methods • If the

COMP 345 - Advanced Program Design with C++ 15 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 declaration with the inline prefix. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 16 Inline functions/methods • Note that

COMP 345 - Advanced Program Design with C++ 16 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 not that the compiler can only inline a function of 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 17 The const specifier • Specifies

COMP 345 - Advanced Program Design with C++ 17 The const specifier • 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 bay 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 const means 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 18 Static members • A member

COMP 345 - Advanced Program Design with C++ 18 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 19 Static members: use and initialization

COMP 345 - Advanced Program Design with C++ 19 Static members: use and initialization Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 20 Static variables • Non-member variables

COMP 345 - Advanced Program Design with C++ 20 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”. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 21 Static variables • All the

COMP 345 - Advanced Program Design with C++ 21 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: • Variable d has local scope and no linkage. • Variable c retains 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 22 Static: sum-up • To sum-up:

COMP 345 - Advanced Program Design with C++ 22 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 at file scope 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 module 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 23 Header file, implementation file •

COMP 345 - Advanced Program Design with C++ 23 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 24 Nested classes • A class

COMP 345 - Advanced Program Design with C++ 24 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 Outer. Class: : Inner. Class • The inner class’ name is local to the outer class Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 25 Nested classes • The class

COMP 345 - Advanced Program Design with C++ 25 Nested classes • The class First. Within is visible outside and • • • inside Surround, as it is public. First. Within'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 First. Within. The class Second. Within is only visible inside Surround. The public members of the class Second. Within can also be used by the members of the class First. Within, as nested classes can be considered members of their surrounding class. Second. Within'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). Second. Within: : d_variable is only visible to Second. Within's members. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 26 Local classes • A class

COMP 345 - Advanced Program Design with C++ 26 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 member objects. Member functions must be defined inside the local classes. Enclosing functions cannot access the private member objects of a local class. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 27 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 27 Click to edit Master title style friends Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 28 Friends • In principle, private

COMP 345 - Advanced Program Design with C++ 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 or classes declared as such within a class. • Widely criticized for being a "rogue feature" • Makes the language more complex to implement • Contradicts the information hiding principle • If overused, creates a proliferation of exceptions to information hiding. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 29 Friends (free function friend) Concordia

COMP 345 - Advanced Program Design with C++ 29 Friends (free function friend) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 30 Friends (free function friend) Concordia

COMP 345 - Advanced Program Design with C++ 30 Friends (free function friend) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 31 Friends (friend class) Concordia University

COMP 345 - Advanced Program Design with C++ 31 Friends (friend class) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 32 Friends (friend class) Concordia University

COMP 345 - Advanced Program Design with C++ 32 Friends (friend class) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 33 Friends (member function friend) Concordia

COMP 345 - Advanced Program Design with C++ 33 Friends (member function friend) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 34 Friends (member function friend) Concordia

COMP 345 - Advanced Program Design with C++ 34 Friends (member function friend) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 35 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 35 Click to edit Master title style Constructors and destructors Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 36 Constructors and destructors • C++

COMP 345 - Advanced Program Design with C++ 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): Day. Of. Year birthday; • Using a pointer variable and calling the new operator (allocated on the heap): Day. Of. Year today = new Day. Of. Year(); • During an explicit call to a constructor: birthday = Day. Of. Year(); • 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 37 Constructors and destructors • To

COMP 345 - Advanced Program Design with C++ 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 38 Constructor declaration and implementation •

COMP 345 - Advanced Program Design with C++ 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 compiler 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 39 Constructor declaration and implementation •

COMP 345 - Advanced Program Design with C++ 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: Day. Of. Year date 3(); // function header declaration Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 40 Constructor declaration and implementation •

COMP 345 - Advanced Program Design with C++ 40 Constructor declaration and implementation • For the implementation, constructors are easily recognizable by being referred to as class. Name: : class. Name : • 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 41 Constructor declaration and implementation •

COMP 345 - Advanced Program Design with C++ 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. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 42 Design principles enabled by class

COMP 345 - Advanced Program Design with C++ 42 Design principles enabled by class as implemented by C++ • Abstraction • Details of how data is manipulated within a class is not known to client classes. Only what is necessary to know is available externally. • Information Hiding • Some details of how an entity is internally representing or acting are hidden to the client class. Abstraction is enforced through information hiding. • Encapsulation • Bring together data and operations in a same entity, possibly hiding some. • Classes encapsulate multiple data elements together with their behavior to form a cohesive whole representing the data and behavior of a real-world concept or entity. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014

COMP 345 - Advanced Program Design with C++ 43 References • Y. Daniel Liang,

COMP 345 - Advanced Program Design with C++ 43 References • Y. Daniel Liang, Introduction to Programming with C++ (Chapter 6, 9, 10, 14), Peason, 2014. • Bjarne Stroustrup, The C++ Programming Language (Chapter 8, 12, 15, 16, 17, 18, 19), Addison-Wesley, 2013. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2014