Software Engineering /48

Size: px
Start display at page:

Download "Software Engineering /48"

Transcription

1 Software Engineering 1 /48

2 Topics 1. The Compilation Process and You 2. Polymorphism and Composition 3. Small Functions 4. Comments 2 /48

3 The Compilation Process and You 3 / 48

4 1. Intro - How do you turn code into a program? - When you hit build, a number of processes are invoked to do this job - Preprocessor - Compiler - Assembler - Linker - It is important to understand these steps 4 / 48

5 2. The Preprocessor - The preprocessor runs on your code before anything else - It prepares your code for compilation - It executes statements starting with the # symbol 5 / 48

6 2.a. #include - #include headerfile.h - This replaces the include statement with the contents of the specified file - Use mainly to include header files - You can include other things - Don t 6 / 48

7 2.b. #define - #define [macro] [replacement] - The define keyword replaces the first term with the second term - Example: #define MAX_THINGS Do not use this to conveniently define constants (like above) - Use C++ instead to get compile-time checks 7 / 48

8 2.b. #define (function macros) - You can also use #define to define function macros - You may have seen this in CS33 - That was C - This is C++ - Do not make function macros - Use inline C++ functions instead 8 / 48

9 2.b. #define (plain macros) - #define [macro] - You can also just define a special word - Can be done in code - Usually passed as an argument to the compiler - Specify these in Qt s.pro file - This should be your main use of #define - This becomes useful with the next directive 9 / 48

10 2.c. #ifdef, #ifndef, #endif - #ifdef defined_macro // if defined compile this code // compiles this #endif - #ifndef defined_macro // if NOT defined compile this code // compile this #endif - Using the previous #define macro, this does exactly what you think it does 10 / 48

11 2.c. #ifdef, #ifndef, #endif - The most common use of #ifdef, #ifndef, #endif is in header guards like this #ifndef HEADER_NAME_H #define HEADER_NAME_H // All code in your header #endif - This prevents headers from being included more than once any time you compile 11 / 48

12 2. Preprocessor cont. - The preprocessor does text replacement - It is not executed when your program runs - Its directives are not live code, so don t use it for actual program logic - Its logic is carried out and all macros are evaluated before the compiler touches the code 12 / 48

13 3. The Compiler - The compiler takes each source file generated by the preprocessor and compiles it into assembly language - From there, the assembler compiles the assembly into object code - The assembler is an intermediate step between the compiler and the linker - It is purely mechanical and not important for us 13 / 48

14 3. The Compiler - The major relevant parts of the compiler (for us) are the compiler flags - These specify certain behavior - Optimization - Warning and error generation - And more! - Different compilers exist; SunLab uses gcc - We ll go over just a few of gcc s options 14 / 48

15 3. The Compiler - -Wall: All warnings should be reported - Turn this on! - In.pro file, add QMAKE_CXXFLAGS += -Wall - You will learn lots of little bits from seeing everything - -Werror: All warnings become errors - Hardcore version of above: you can t compile if you have ANY warnings - Turn this on if you want to force yourself to code proper C++ 15 / 48

16 3. The Compiler - -O or -O1: Turn on level 1 optimizations - -O2: Optimize more - -O3: Optimize morer - -O0 (That s O and zero): Turn off optimizations - Will be easiest to use with the debugger - Look up specs for details, but they allow the compiler to make your code run faster - Hurray! 16 / 48

17 4. The Assembler - The assembler takes assembly code as written by the compiler - Assembly is written as a list of commands - Move this here, add this to that - The assembler translates each command into a numerical opcode (this is machine executable) - C++ supports compiling straight to machine code, to assembly code, to static or shared libraries, to whatever, really! 17 / 48

18 5. The Linker - Header files usually say that certain things (classes, functions, etc.) will exist (i.e. they ve been declared) - Implementations are defined in source files - The linker will pull in external libraries and source files to make an executable - E.G. Windows.dll files,.a libraries, etc. 18 / 48

19 6. Linker Errors - Oh I think I ll include this header file inside this other header file - Prefer forward declaration until you must include - Important exception: Always include Standard Library headers. Forward declaring is undefined behavior - *gasp* - The std namespace is reserved 19 / 48

20 6. Linker Errors - What is a forward declaration? - Rather than typing #include Foo.h in your.h file - Type class Foo; instead and put the include in the.cpp file - This signifies that the class Foo will exist and be defined later - This is all you need to say - Including the header file imports code and dependencies - This says what you need to say, but says too much more 20 / 48

21 6. Linker Errors - When can you forward declare Class Foo in header files? - When a Foo object is an argument or return type of a function - Pointer and reference versions of the above - This includes smart pointers* - When a member variable is a pointer or reference to Foo *The smart pointer headers themselves must be included 21 / 48

22 6. Linker Errors - When must you include Foo.h inside another header file? - When extending Foo - When an object is a direct (non-reference, non-pointer) member of Foo - When you want something from the Standard Library - Rule of thumb: Forward declare unless your compiler yells at you 22 / 48

23 6.a. Link times - If your project has lots of code to link, the Linker has more symbols to resolve and more places to look for them - Forward declaring is one statement for one class or function - Including shoves everything else about those functions and classes into the mix as well 23 / 48

24 6.b. Circular Dependencies A circular dependency occurs when two header files include one another - Header A includes header B - Header B includes header A - Header A includes itself - an infinite regression - Common example: Two classes contain instances of one another - Class A has member Class B - Class B has member Class A - In order to fully define class B for inclusion in Class A (and determine storage), class A must be fully defined, which requires that class B be fully defined 24 / 48

25 6.b. Circular Dependencies - Solution: Use pointers instead of direct members; forward declare instead of #include Foo.h Bar.h class Bar; // Forward Declaration class Foo; // Forward Declaration class Foo { class Bar { private: private: Bar *m_bar; // Member pointer Foo *m_bar; // Member pointer }; }; 25 / 48

26 7. Cleaning - The compiler and linker are smart and try to reduce their workload - They won t rebuild things unless they need to - Sometimes you ll change an aspect of your build process - Add compilation flag - Add #define macro - Have a weird time replacing a file - Can end up with mixed-build state 26 / 48

27 7. Cleaning - Solution: Clean your project - Qt->Build->Clean All - Removes all generated files - The next build will be forced to recompile everything - When in doubt, clean it out! - If you re getting strange errors that don t reflect the code, the code just may need to be cleaned 27 / 48

28 Polymorphism & Composition Prefer Composition! 28 /48

29 Classes are types - A class defines a type - The type represents data that has various functions defined for it - int is a type. So is a glm::vec3. So are Mesh and Shader. - We usually name functions like dothing() and length() - + and - are also functions - They take two arguments and return a result - Operators are functions - You should be able to use a class without knowing how it works - You know how to use int. You don t care how int is implemented. 29 /48

30 Polymorphism: How to do it - Liskov Substitution Principle: Derived types must be completely substitutable for their base types. - What this means: subclasses must work as intended if they are substituted for their superclasses - How not to do it: - Class Rectangle implements a bunch of methods, including setheight and setwidth (and getters) - Class Square subclasses Rectangle, but can t modify height alone - This is bad! Squares can t be treated as rectangles! 30 /48

31 Inheritance - A base class defines an interface (signatures of all its methods) - The base class only implements functionality common to every possible subclass - Correspondingly, all subclasses must obey this interface - What does that mean? - Base class does not do things conveniently for some subset of possible subclasses - Subclasses can be used as the base class without requiring the user to know which particular subclass it is - New subclasses do not require updates to base class - New subclasses do not need to circumvent/reverse-engineer parts of baseclass 31 /48

32 Filter example: How not to use the base class - Filter example: You notice that 2D convolution is shared by some classes, so you stick it in the base class. - But some subclasses can use 2 1D convolutions, so you put that in the base class. - And so on and so on until the base class implements everything - It has become a bucket 32 /48

33 Composition - Prefer composition over inheritance - By factoring shared functionality out into separate classes rather than up into the base class, you allow for unbridled customization without repeating code - By factoring up, you ve ensured that future subclasses must obey certain implementation details (in C++ especially if you didn t make functions virtual!) - This does not mean you should make everything virtual. That adds unnecessary overhead cost to your classes. - Composition can be used alongside inheritance - The distinction is more a question of where do you get your stuff? 33 /48

34 Inheritance example - Class Car defines functions startup, accelerate, brake, turn, and shutdown - Class Cars will have lots of subtypes - Subarus, Ferraris, and even Priuses - But a Ferrari engine is very different from a Subaru engine - Protected methods for subclasses to modify the engine - This quickly becomes tedious - all the Car subclasses are always out of date - And what happens when you need to make a Prius /48

35 Inheritance example - What if you make a separate engine class for your car - Now, additions to the Engine class will not require that Car update its interface - The Engine class is robust - it does everything you could ever possibly want, even stuff we don t need right now - The Engine class functionality is in the Car class via composition - Despite how robust the Engine class is - it does not provide Ferrari functionality - can t predict all future use cases 35 /48

36 Inheritance example - What if Car has a private variable Engine m_engine - Methods startup, shutdown, accelerate, etc. - Now we want a RaceCar that needs a souped up RaceEngine - But we don t have access to the base class s m_engine -> we make m_raceengine - All the base class methods that use the built-in m_engine now need to be overridden to use the new m_raceengine - Conclusion: none of the inherited code can be used - And you need to know how to reverse engineer the base class s methods that were using the built-in Engine, which, if they re private, is impossible - Solution: Don t have an Engine in the base class. Implement startup by referring to the sub-class s instance variable of an Engine 36 /48

37 Inheritance example - The new Car class only has one method: drive() - Any interface for acceleration + braking will be too simple for some Cars, but not robust enough for others. - Users now have to figure out all their own Car functionality - Car class doesn t actually do anything. - If you make no restrictions, you ve made no abstraction - The goal in creating an interface or a library is precisely to figure out the right abstractions. - And be flexible to make changes to account for constantly evolving contract requirements 37 /48

38 Inheritance example - One day, you may observe your code and exclaim, Oh look, this code is repeated in subclass X and subclass Y! I ll factor it out into the base class! - That is not how repeated code works - That is not how inheritance works - Repeated code is code that does the same thing for the same user in more than one location - The base class is not a receptacle for all potentially shared pieces of particular functionality for particular subclasses. 38 /48

39 Interface example - The base class lays out an interface! - Particular subclass implementation details are factored out into other classes - Subclasses X and Y that use M include it by composition - Subclass Z is now not forced to use M but may instead include N instead - Implementation details common to all subclass may be factored up into the base class - But even then it may make sense to factor it out into another class that B includes by composition 39 /48

40 Shapes Example - If Shape lays out particular OpenGL implementation details, it restricts what future subclasses can do - This has nothing to do with the way you actually use Shapes - i.e. the contract - Example: Shape lays out uv-tessellation implementation - How to implement a Mesh class? - A fractal class? - Example: Shape requires only GL_TRIANGLES - What about an efficient GL_TRIANGLE_STRIP class? - What about indexed buffers? - Solution: Direct OpenGL functionality (like raw VBOs) should not go in Shape base class - Shape subclasses should include OpenGL objects/functions by composition 40 /48

41 Inheritance Example - You know how the base class is supposed to be used - By definition, you do not know how a subclass will be implemented - If the base class provides an implementation of a certain piece of functionality, ask yourself this question: - Can I dream up any subclass that fits into the base class s interface but would not be possible with the base class s implementation of the functionality? - If no, then include the functionality in the base class - If yes, then remove the functionality from the base class 41 /48

42 Small Functions Keyword: small 42 /48

43 Large Functions - Hard to read - Need to be refactored more often - Large size means more places for bugs to hide - Usually break the Single Responsibility Principle - If it can be broken up into two functions, why isn t it two functions? 43 /48

44 Small Functions - Do one thing and one thing only - Say exactly what they do in the function name - There are no unintended consequences when they change - Because they only have one task, they are easier to debug 44 /48

45 Comments Yes, sometimes no 45 /48

46 What can go wrong with comments - Comments can go out of date if they re not updated when their code is - Comments can lie *gasp* - Comments can add clutter - Solution: - Take the time to descriptively name your variables and functions. If things are named obviously, there may not be a need to comment with an explanation - Use small functions - Good variable names alone will not make your code clear! You will need comments sometimes, and that is okay. 46 /48

47 When to use comments - To add new information - To explain how or why your code is working - Instead of saying: Step 4: Do X - Say: Step 4: Do X because reason - To explain how third parties use the public API you provide - Call this function with arguments that look like this:. - Returns an integer within this range:. - This throws an error when your arguments don t look like and what happens is. 47 /48

1: Introduction to Object (1)

1: Introduction to Object (1) 1: Introduction to Object (1) 김동원 2003.01.20 Overview (1) The progress of abstraction Smalltalk Class & Object Interface The hidden implementation Reusing the implementation Inheritance: Reusing the interface

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

CS354 gdb Tutorial Written by Chris Feilbach

CS354 gdb Tutorial Written by Chris Feilbach CS354 gdb Tutorial Written by Chris Feilbach Purpose This tutorial aims to show you the basics of using gdb to debug C programs. gdb is the GNU debugger, and is provided on systems that

More information

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 10: building large projects, beginning C++, C++ and structs CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

More information

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22 COSC 2P91 Bringing it all together... Week 4b Brock University Brock University (Week 4b) Bringing it all together... 1 / 22 A note on practicality and program design... Writing a single, monolithic source

More information

QUIZ. Source:

QUIZ. Source: QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That

More information

Starting to Program in C++ (Basics & I/O)

Starting to Program in C++ (Basics & I/O) Copyright by Bruce A. Draper. 2017, All Rights Reserved. Starting to Program in C++ (Basics & I/O) On Tuesday of this week, we started learning C++ by example. We gave you both the Complex class code and

More information

Figure 1 Common Sub Expression Optimization Example

Figure 1 Common Sub Expression Optimization Example General Code Optimization Techniques Wesley Myers wesley.y.myers@gmail.com Introduction General Code Optimization Techniques Normally, programmers do not always think of hand optimizing code. Most programmers

More information

Reviewing gcc, make, gdb, and Linux Editors 1

Reviewing gcc, make, gdb, and Linux Editors 1 Reviewing gcc, make, gdb, and Linux Editors 1 Colin Gordon csgordon@cs.washington.edu University of Washington CSE333 Section 1, 3/31/11 1 Lots of material borrowed from 351/303 slides Colin Gordon (University

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

Lecture 4 CSE July 1992

Lecture 4 CSE July 1992 Lecture 4 CSE 110 6 July 1992 1 More Operators C has many operators. Some of them, like +, are binary, which means that they require two operands, as in 4 + 5. Others are unary, which means they require

More information

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

the gamedesigninitiative at cornell university Lecture 7 C++ Overview Lecture 7 Lecture 7 So You Think You Know C++ Most of you are experienced Java programmers Both in 2110 and several upper-level courses If you saw C++, was likely in a systems course Java was based on

More information

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module Basic Class Design Goal of OOP: Reduce complexity of software development by keeping details, and especially changes to details, from spreading throughout the entire program. Actually, the same goal as

More information

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 What is gdb? GNU Debugger A debugger for several languages, including C and C++ It allows you to inspect what the program

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Polymorphism 1 / 19 Introduction to Object-Oriented Programming Today we ll learn how to combine all the elements of object-oriented programming in the design of a program that handles a company payroll.

More information

Lecture 14: more class, C++ streams

Lecture 14: more class, C++ streams CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L 2 0 1 5 Introduction At this point, you are ready to beginning programming at a lower level How do you actually write your

More information

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez! C++ Mini-Course Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion C Rulez! C++ Rulez! C++ Mini-Course Part 1: Mechanics C++ is a

More information

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE HEADER FILE A header (.h,.hpp,...) file contains Class definitions ( class X {... }; ) Inline function definitions ( inline int get_x() {... } ) Function declarations ( void help(); ) Object declarations

More information

Lecture 13: more class, C++ memory management

Lecture 13: more class, C++ memory management CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 13:

More information

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSci 4061 Introduction to Operating Systems. Programs in C/Unix CSci 4061 Introduction to Operating Systems Programs in C/Unix Today Basic C programming Follow on to recitation Structure of a C program A C program consists of a collection of C functions, structs, arrays,

More information

CS11 Introduction to C++ Fall Lecture 7

CS11 Introduction to C++ Fall Lecture 7 CS11 Introduction to C++ Fall 2012-2013 Lecture 7 Computer Strategy Game n Want to write a turn-based strategy game for the computer n Need different kinds of units for the game Different capabilities,

More information

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32

More information

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle Code Critique Identifying smells Refactoring Liskov Substitution Principle What goal are we designing to? What is the typical fix for code smells? What is a limitation of those fixes? How do we address

More information

Reliable programming

Reliable programming Reliable programming How to write programs that work Think about reliability during design and implementation Test systematically When things break, fix them correctly Make sure everything stays fixed

More information

Homework & Debugging Tips

Homework & Debugging Tips Homework & Debugging Tips Data Structures Spring 2018 http://www.cs.rpi.edu/academics/courses/spring18/csci1200/calendar.php Contents Order of Consultation 1 I Don t Know Where to Start 1 Have A C++ Question?................................

More information

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez! C++ Mini-Course Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion C Rulez! C++ Rulez! C++ Mini-Course Part 1: Mechanics C++ is a

More information

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14 COSC 2P91 Introduction Part Deux Week 1b Brock University Brock University (Week 1b) Introduction Part Deux 1 / 14 Source Files Like most other compiled languages, we ll be dealing with a few different

More information

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018 Contents 1 Mutation in the Doghouse 1 1.1 Aside: Access Modifiers..................................

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns Last Time: Object Design Comp435 Object-Oriented Design Week 7 Computer Science PSU HBG The main idea RDD: Responsibility-Driven Design Identify responsibilities Assign them to classes and objects Responsibilities

More information

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors QUIZ How could we disable the automatic creation of copyconstructors pre-c++11? What syntax feature did C++11 introduce to make the disabling clearer and more permanent? Give a code example. Ch. 14: Inheritance

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 13 C: The Rest of the Preprocessor 1 Administrivia Midterm exam Wednesday, here Topics: everything up to hw4 (including gdb concepts,

More information

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

Mastering Data Abstraction or Get nit-picky on design advantages

Mastering Data Abstraction or Get nit-picky on design advantages Arizona s First University. Mastering Data Abstraction or Get nit-picky on design advantages Luke: Your not my father! Vader: You mean, You re not my father Luke: What? Vader: You used the possessive,

More information

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors QUIZ How could we disable the automatic creation of copyconstructors pre-c++11? What syntax feature did C++11 introduce to make the disabling clearer and more permanent? Give a code example. QUIZ How

More information

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey. EXPLORATION 36 Virtual Functions Deriving classes is fun, but there s not a lot you can do with them at least, not yet. The next step is to see how C++ implements type polymorphism, and this Exploration

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/13 Scope Variables and functions are visible from the point they are defined until the end of the source

More information

Separate Compilation of Multi-File Programs

Separate Compilation of Multi-File Programs 1 About Compiling What most people mean by the phrase "compiling a program" is actually two separate steps in the creation of that program. The rst step is proper compilation. Compilation is the translation

More information

CS2141 Software Development using C/C++ Compiling a C++ Program

CS2141 Software Development using C/C++ Compiling a C++ Program CS2141 Software Development using C/C++ Compiling a C++ Program g++ g++ is the GNU C++ compiler. A program in a file called hello.cpp: #include using namespace std; int main( ) { cout

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 4: OO Principles - Polymorphism http://courses.cs.cornell.edu/cs2110/2018su Lecture 3 Recap 2 Good design principles.

More information

Computer Labs: Debugging

Computer Labs: Debugging Computer Labs: Debugging 2 o MIEIC Pedro F. Souto (pfs@fe.up.pt) October 29, 2012 Bugs and Debugging Problem To err is human This is specially true when the human is a programmer :( Solution There is none.

More information

PIC 10A. Lecture 17: Classes III, overloading

PIC 10A. Lecture 17: Classes III, overloading PIC 10A Lecture 17: Classes III, overloading Function overloading Having multiple constructors with same name is example of something called function overloading. You are allowed to have functions with

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

CS Software Engineering for Scientific Computing. Lecture 5: More C++, more tools.

CS Software Engineering for Scientific Computing. Lecture 5: More C++, more tools. CS 294-73 Software Engineering for Scientific Computing Lecture 5: More C++, more tools. Let s go back to our build of mdarraymain.cpp clang++ -DDIM=2 -std=c++11 -g mdarraymain.cpp DBox.cpp -o mdarraytest.exe

More information

Have examined process Creating program Have developed program Written in C Source code

Have examined process Creating program Have developed program Written in C Source code Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives

More information

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7 Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7 Ziad Matni Dept. of Computer Science, UCSB Programming in Multiple Files The Magic of Makefiles!

More information

y

y The Unfit tutorial By Dr Martin Buist Initial version: November 2013 Unfit version: 2 or later This tutorial will show you how to write your own cost function for Unfit using your own model and data. Here

More information

CS 247: Software Engineering Principles. Modules

CS 247: Software Engineering Principles. Modules CS 247: Software Engineering Principles Modules Readings: Eckel, Vol. 1 Ch. 2 Making and Using Objects: The Process of Language Translation Ch. 3 The C in C++: Make: Managing separate compilation Ch. 10

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #54 Organizing Code in multiple files (Refer Slide Time: 00:09) In this lecture, let us look at one particular

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

Computer Organization & Assembly Language Programming

Computer Organization & Assembly Language Programming Computer Organization & Assembly Language Programming CSE 2312 Lecture 11 Introduction of Assembly Language 1 Assembly Language Translation The Assembly Language layer is implemented by translation rather

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

Lab 7 Unit testing and debugging

Lab 7 Unit testing and debugging CMSC160 Intro to Algorithmic Design Blaheta Lab 7 Unit testing and debugging 13 March 2018 Below are the instructions for the drill. Pull out your hand traces, and in a few minutes we ll go over what you

More information

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance Handout written by Julie Zelenski, updated by Jerry. Inheritance is a language property most gracefully supported by the object-oriented

More information

Page 1. Last Time. Today. Embedded Compilers. Compiler Requirements. What We Get. What We Want

Page 1. Last Time. Today. Embedded Compilers. Compiler Requirements. What We Get. What We Want Last Time Today Low-level parts of the toolchain for embedded systems Linkers Programmers Booting an embedded CPU Debuggers JTAG Any weak link in the toolchain will hinder development Compilers: Expectations

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software. Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes

More information

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) CSE 374 Programming Concepts & Tools Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) Linked lists, trees, and friends Very, very common data structures

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

More information

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015 C++ Coding Standards and Practices Tim Beaudet (timbeaudet@yahoo.com) March 23rd 2015 Table of Contents Table of contents About these standards Project Source Control Build Automation Const Correctness

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 14 Makefiles and Compilation Management 1 Where we are Onto tools... Basics of make, particular the concepts Some fancier make features

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s Session 2 - Classes in C++ Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) A C++ source file may contain: include directives #include

More information

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

CS Lecture #14

CS Lecture #14 CS 213 -- Lecture #14 We re starting to catch up! Administrative... Late Night Guide to C++ Chapter 9 pg 222-239 MORE ABOUT CLASSES Part I Interfaces in C++ For those with Java experience, you know that

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

Racket Style Guide Fall 2017

Racket Style Guide Fall 2017 CS17 Integrated Introduction to Computer Science Hughes Racket Style Guide Fall 2017 Contents 1 Introduction 1 2 Naming 1 3 Formatting 1 4 Equality 3 5 Conditionals 4 5.1 Prefer Cond to If......................................

More information

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

#include <stdio.h> int main() { printf (hello class\n); return 0; } C #include int main() printf ("hello class\n"); return 0; Working environment Linux, gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

The Compilation Process

The Compilation Process The Compilation Process Olaf Lenz http://wwwicpuni-stuttgartde Institut für Computerphysik Universität Stuttgart March 17-21, 2014 Separate Compilation http://wwwicpuni-stuttgartde So far, all programs

More information

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

11.1 Modular Organization and makefiles.

11.1 Modular Organization and makefiles. Chapter 11: Modules and Makefiles 11.1 Modular Organization and makefiles. From the Fanny Farmer cookbook: Before beginning to mix, be sure that all ingredients and utensils are on hand. You can t bake

More information

Singleton, Factory Method, Abstract Factory, Named Constructor. Using one or more base classes to hide details from the client

Singleton, Factory Method, Abstract Factory, Named Constructor. Using one or more base classes to hide details from the client Idioms & Design Patterns Creational Introduction to Design Patterns Patterns and idioms can be grouped roughly into: Creational Patterns and idioms Singleton, Factory Method, Abstract Factory, Named Constructor

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

How to approach a computational problem

How to approach a computational problem How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Overview of C++ Support in TI Compiler Tools July 2008

Overview of C++ Support in TI Compiler Tools July 2008 Overview of C++ Support in TI Compiler Tools July 2008 1 Table of Contents 1 Table of Contents... 1 2 Introduction... 1 3 Support for the Language... 1 4 Embedded C++... 1 5 Some Comments on Efficiency...

More information

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding Conformance Conformance and Class Invariants Same or Better Principle Access Conformance Contract Conformance Signature Conformance Co-, Contra- and No-Variance Overloading and Overriding Inheritance as

More information

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above

More information