Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++

Similar presentations


Presentation on theme: "Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++"— Presentation transcript:

1 Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++

2 Modularity The ability to divide system into a set of functional units (named modules) that can be put together into a complex application. Modules are perceived to be independent from one another but their integrated functionalities acts like a single application. In this course we will cover the following: Header files, Classes, Member functions Access specifiers, Mutators, Value parameters Reference parameters, Returning of values Operator overloading, Templates and Copy constructors 2016/01/03Mr Kandjimi2

3 Header files Every.cpp file has an associated header file (named with.h extension) Contains a collection of function prototypes, constant and pre- processor definitions No actual implementation of the functions. Declared functions are easily shared between classes Two types exist: Programmer defined header Compiler predefined header Main purpose is for other modules/source files to access the functionality in any given header file simply by #including the “X.h” header file. Reduces the need to recompile source files when changes are made to the programs that use them. 2016/01/03Mr Kandjimi3

4 Classes Begins with the class keyword, followed by the class name. Class body enclosed in curly brackets {} defines a blueprint for a data type, no actual data provided Specifies what an object of that class can consist of and what operations can be done(methods). Must always contain a constructor and a destructor. Both must be public, same concept as in other OOP Languages. Please note that file name do not need to be the same as class name (*Java) 2016/01/03Mr Kandjimi4

5 Member functions Defined within a class like other class variables, except that its line ends with “(void)” or “(parameter list).” Note: the keyword “void” could be omitted. Define the operations an object of the class can carry out...can also operate on any objects of the class. Can access all the members of the class Implementation can be within the class definition or separately using scope resolution operator, :: (next slide) 2016/01/03Mr Kandjimi5

6 Member functions.. e.g.(1) 2016/01/03Mr Kandjimi6 This is referred to as inline definition. All implementation done within the class

7 Member functions.. e.g.(2) 2016/01/03Mr Kandjimi7 All implementation done outside the class, but referencing the class

8 Access specifiers (1) Access restriction to the class members is specified by: Public, Private and Protected In C++ the default specifier is private. Public members Can be accessed from anywhere outside the class without any member function. Private members cannot be accessed, or even viewed from outside the class (only with accessors and mutators) Protected members Same as private, but with the benefit that they can be accessed in child classes which are called derived classes. 2016/01/03Mr Kandjimi8

9 Access specifiers (2) One of important concepts of OOP is data hiding Such that non-member functions cannot have access to private or protected members Depending on the problem at hand, sometimes this restriction forces programmer to write long and complex codes To address this, C++ has a built-in mechanism to access private or protected members from non-member functions. This mechanism is a specifier called friend Friend This is used to specify within a class (say A) that an external class (say B) is a friend and can have access to the private and protected members of class A. Similarly, an external function could be declared as friend in a given class. Such function will have access to the private and protected members of the class 2016/01/03Mr Kandjimi9

10 Accessors and Mutators Also referred to as getters and Setters Naming convention should relate to the class member in question. Getters/Accessors name of the accessor must begin with the “get“ or “Get” prefix. No arguments required Must return a value for the given class member(variable) Setters/ Mutators name of the mutator must begin with the “set“ or “Set” prefix. Must take the value to be set to the class member as an argument Does not return anything 2016/01/03Mr Kandjimi10

11 Accessors and Mutators … 2016/01/03Mr Kandjimi11

12 Value and Reference parameters Also referred to as pass by value or by reference Place holders for a certain variable with a difference in that: Value parameters – means a copy of the value is passed to a functions and changes within the function are not retained after the functions Reference parameters – means a reference to the value is passed to a function and hence changes made will affect the original value See next slide for an example, try and understand what’s happening by running the code. For better understanding, please read further on the pro and cons of the parameter options and when or when not to use them 2016/01/03Mr Kandjimi12

13 Value and Reference parameters.. 2016/01/03Mr Kandjimi13

14 Returning of values return statement stops execution and returns to the calling function Hence always the last statement Returning in C++ can be done in three ways: By value, by reference and by address Return by value is the simplest and fastest way, since only a copy of the value is returned. Return by reference and address are quite similar, except when and how the are used. For better understanding please find out on when and when not to use each of the return options stated above 2016/01/03Mr Kandjimi14

15 Operator overloading C++ allows redefining most of the built-in operators, this is particularly useful where objects act as operands: Such as +,-,/,*,%, ^ …etc Hence if you have defined your own data type, these allows you to provide a custom operation for the objects of your class. Lets take time for example : the Time class could have variables such as hours, minutes and seconds. Overloading the operator means the function should take into consideration all the three aspects of Time. Next example illustrates the + operate for the Time class Consider overloading the minus(-) operator for the Time class 2016/01/03Mr Kandjimi15

16 Time:+operator 2016/01/03Mr Kandjimi16

17 Templates Foundation of generic programming: code is written in way that’s independent of any particular type blueprint for creating a generic class or a function. Makes classes more abstract by letting you define the behaviour of the class without actually knowing the datatype that are handled by the class. It being generic means code is full reusable. An instantiated object of a templated class is called a specialization: Now the template is restricted to that object, hence acts like any other normal object of that type. More then one specialization allowed 2016/01/03Mr Kandjimi17

18 Copy constructors Just like a normal constructor except it takes a parameter of the class type As the name suggests they are used to create new objects by make a copy of an existing object, mainly for the following: Initialize one object from another of the same type. Copy an object to pass it as an argument to a function. Copy an object to return it from a function. By default C++ defines a copy constructor for each class 2016/01/03Mr Kandjimi18


Download ppt "Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++"

Similar presentations


Ads by Google