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

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
Chapter 15: Operator Overloading
IT PUTS THE ++ IN C++ Object Oriented Programming.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Classes, Interfaces and Packages
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 13: Overloading and Templates
Chapter 5 Classes.
Lecture 9 Concepts of Programming Languages
Introduction to Classes
Classes and Objects.
Object-Oriented Programming
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

Accessors and Mutators … 2016/01/03Mr Kandjimi11

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

Value and Reference parameters /01/03Mr Kandjimi13

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

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

Time:+operator 2016/01/03Mr Kandjimi16

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

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