CS计算机代考程序代写 compiler Java data structure c++ COMP345:

COMP345:
Advanced Program Design with C++
Lecture 4
Aggregate data types (part a)
Department of Computer Science and Software Engineering Concordia University

Contents
❑ struct
❑ class
❑ inline functions and methods ❑ const specifier

Aggregate data types
▪ An aggregate data type represents a type that contains other data elements.
▪ C++ aggregate data types: array, struct and class
▪ Array: collection of values of same type
▪ Struct: collection of values of different types
▪ Class: conceptually, 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 be declared prior to declaring any variables of this new type.
3
Joey Paquet, 2007-2020

Contents
❑ struct
❑ class
❑ inline functions and methods ❑ const specifier
❑ static specifier
❑ friend
❑ Constructors and destructors

COMP 345 – Advanced Program Design with C++ 5
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-2020

Using a POD
▪ Once the struct has been declared, it can be used as any other type ▪ The members of a struct can be referred to using the dot notation
6
Joey Paquet, 2007-2020

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

Using a POD
8
Joey Paquet, 2007-2020

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 members is managed correctly.
▪ We will see more about that when we talk about classes.
9
Joey Paquet, 2007-2020

Contents
❑ struct
❑ class
❑ inline functions and methods ❑ const specifier
❑ static specifier
❑ friend
❑ Constructors and destructors

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):
11
Joey Paquet, 2007-2020

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-2020
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).
13
Joey Paquet, 2007-2020

Header file, implementation file
▪ Good physical design of your program dictates that you should have:
▪ Class and member function 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 a 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-2020
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-2020
15

Contents
❑ struct
❑ class
❑ inline functions and methods ❑ const specifier
❑ static specifier
❑ friend
❑ Constructors and destructors

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 inline keyword as a prefix to the function definition. The function declaration does not need to have the inline prefix.
17
Joey Paquet, 2007-2020

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.
18
OR
Joey Paquet, 2007-2020

Inline functions/methods
▪ Technical explanation:
▪ In order to be able to make the code substitution, the compiler needs the definition of
the inline function.
▪ Thus, inline functions’ definitions must be found in all the compilation unit in which they are used.
▪ To achieve that, we may put their definition in the header file in which they are declared, either:
▪ In the class declaration.
▪ As a function definition after the class declaration.
▪ In other words, inline functions have internal linkage. If you want to make an inline function accessible to other compilation units, they must include the definition of the inline function.
19
Joey Paquet, 2007-2020

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.
▪ Rule of thumb: Inline your functions that have very short function definitions. Accessor methods are very good candidates for method inlining.
20
Joey Paquet, 2007-2020

Contents
❑ struct
❑ class
❑ inline functions and methods ❑ const specifier

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

The const specifier
▪ Pointers involve two different concepts ▪ the pointer itself
▪ the value pointed to
▪ These two different concepts can be declared separately as const
▪ When combined and repeated, this leads to mind-boggling concepts:
23
Joey Paquet, 2007-2020

End part a

Contents
❑ struct
❑ class
❑ inline functions and methods ❑ const specifier
❑ static specifier
❑ friend
❑ Constructors and destructors

Leave a Reply

Your email address will not be published. Required fields are marked *