Makefile Separate Compilation

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Polymorphism CS351 – Programming Paradigms.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
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.
Chapter -6 Polymorphism
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
A First Book of C++ Chapter 12 Extending Your Classes.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Polymorphism
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Learning Objectives Inheritance Virtual Function.
Makefiles and the make utility
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Java Inheritance.
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
Inheritance and Polymorphism
COP 3330 Object-oriented Programming in C++
Makefiles and the make utility
COP 3330 Object-oriented Programming in C++
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Makefile Separate Compilation What does the compiler do when it sees #include of a .hpp file in a .cpp file? When you have many .cpp files, you will end up creating a corresponding .o file. In UNIX C++ compilers this is done with the -c option. Any implementations of classes that are NOT part of the .cpp file, but appear in the #include files are not compiled Makefiles : not necessary to recompile all the files when you make changes. You only need to recompile a small subset of the files. https://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html

Makefile Each entry has: Example: a target (usually a file) the dependencies (files which the target depends on) and commands to run, based on the target and dependencies Example: Movie.o: Movie.cpp Movie.h Vector.h g++ -Wall -c Movie.cpp The basic syntax of an entry looks like: <target>: [ <dependency > ]* [ <TAB> <command> <endl> ]+ The entry tells how to construct a target, and more importantly, when to construct the target

Exectuables as targets Makefile Exectuables as targets Usually, the purpose of the makefile is to create an executable. That target is often the first target in the makefile. Example: p1 : MovieList.o Movie.o NameList.o Name.o Iterator.o g++ -Wall MoveList.o Movie.o NameList.o Name.o Iterator.o -o p1 If p1 is the first target in your makefile, then when you type "make", it will run the commands for p1. “ Notice that the command to compile will create an executable called p1. It uses the -o option to create an executable with a name other than a.out.

<macro_name> = <macro_string> Makefile Macro definitions OBJS = MovieList.o Movie.o NameList.o Name.o Iterator.o CC = g++ DEBUG = -g CFLAGS = -c -Wall -Werror -std=c++11 –pedantic LFLAGS = -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system p1 : $(OBJS) $(CC) $(LFLAGS) $(OBJS) -o p1 Macros are usually put at the beginning of a makefile. A macro has the following syntax: <macro_name> = <macro_string>

Makefile make clean clean: \rm *.o *~ p1 The backslash prevents "rm" from complaining Normally, you remove all .o files, all files ending in ~ (which are emacs backup files), and the name of the executable

Makefile make all all: p1 p2 p3 p1: Foo.o main1.o   p1: Foo.o main1.o g++ -Wall Foo.o main1.o -o p1 p2: Bar.o main2.o g++ -Wall Bar.o main2.o -o p2 p3: Baz.o main3.o g++ -Wall Baz.o main3.o -o p3

Class Inheritance Deriving one class from another class   Deriving one class from another class Polymorphism allows redefining how member functions of the same name operate Base class (parent class, or superclass): initial class used as basis for derived class Derived class (child class, or subclass): new class that incorporates all of the data and member functions of its base class Usually adds new data and function members Can override any base class function

Polymorphism Permits same function name to invoke one response in objects of base class and another response in objects of derived class Example of polymorphism: overriding of base member function using an overloaded derived member function Function binding: determines whether base class or derived class version of function will be used Static binding: determination of which function to be called is made at compile time Used in normal function calls Dynamic binding: determination of which function to be called is made at runtime (via virtual function)

Polymorphism Virtual function : creates pointer to function to be used Value of pointer variable is not established until function is actually called At runtime, and on the basis of the object making the call, the appropriate function address is used

Circle copy(Circle&) const; Circle copy(const Circle &); const member function Circle copy(Circle&) const; A const member function is indicated by a const suffix just after the member function’s parameter list It cannot call any non-const member functions, nor can it change any member variables The function can only be called via a const object of the class. Must be member function of the class 'Circle'. Circle copy(const Circle &); This one means that the parameter passed cannot be changed within the function. This one may or may not be a member function

Close-up of a Romanesco broccoli Fractal A fractal is a natural phenomenon or a mathematical set that exhibits a repeating pattern that displays at every scale. It is also known as expanding symmetry or evolving symmetry. If the replication is exactly the same at every scale, it is called a self-similar Close-up of a Romanesco broccoli An image of a fern https://commons.wikimedia.org/w/index.php?curid=22398720

Sierpinski triangle   The Sierpinski triangle fractal was first introduced in 1915 by Wacław Sierpiński (1882 –1969). But similar patterns already appeared in the 13th-century in some cathedrals. He was known for outstanding contributions to set theory (research on the axiom of choice and the continuum hypothesis), number theory, theory of functions and topology. He published over 700 papers and 50 books.

Sierpinski triangle

Recursion   Recursion is perhaps the most powerful programming technique, but unfortunately it also commonly referred as the most difficult to grasp. To put it simply recursion is a programming technique which let us to solve problems by decomposing the problem into smaller instances of the same problem. This technique is so powerful that it lets us to even emulate the different kinds of loops with recursion.

Sierpinski triangle

Sierpinski triangle Start with an equilateral triangle Height ℎ= 3 2 𝑎 𝑘=1 𝑛 𝑎 𝑟 𝑘−1 =𝑎 𝑟 0 +𝑎 𝑟 𝑛 +…+𝑎 𝑟 𝑛−1 = 𝑎(1− 𝑟 𝑛 ) 1−𝑟

#include guard #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion File "grandfather.h“ File "father.h“ File "child.c" struct foo { int member; }; #include "grandfather.h" #include "grandfather.h" #include "father.h" https://en.wikipedia.org/wiki/Include_guard

#include guard #ifndef GRANDFATHER_H #define GRANDFATHER_H struct foo File "grandfather.h“ File "father.h“ File "child.c" #ifndef GRANDFATHER_H #define GRANDFATHER_H struct foo { int member; }; #endif /* GRANDFATHER_H */ #include "grandfather.h" #include "grandfather.h" #include "father.h" https://en.wikipedia.org/wiki/Include_guard

Multiple inheritance Inheritance describes a relationship between two classes in which one class (the child class) subclasses the parent class. The child inherits methods and attributes of the parent, allowing for shared functionality Multiple inheritance allows programmers to use more than one totally orthogonal hierarchy simultaneously https://en.wikipedia.org/wiki/Multiple_inheritance

Multiple inheritance 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner: public PoweredDevice class Printer: public PoweredDevice class Copier: public Scanner, public Printer

Multiple inheritance

Multiple inheritance

Multiple inheritance 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class PoweredDevice { }; class Scanner: virtual public PoweredDevice class Printer: virtual public PoweredDevice class Copier: public Scanner, public Printer

Multiple inheritance Virtual base classes are created before non-virtual base classes, which ensures all bases get created before their derived classes The Scanner and Printer constructors still have calls to the PoweredDevice constructor Creating an instance of Copier, these constructor calls are simply ignored because Copier is responsible for creating the PoweredDevice , not Scanner or Printer Creating an instance of Scanner or Printer , the virtual keyword is ignored, those constructor calls would be used, and normal inheritance rules apply

Multiple inheritance If a class inherits one or more classes that have virtual parents, the most derived class is responsible for constructing the virtual base class In this case, Copier inherits Printer and Scanner , both of which have a PoweredDevice virtual base class Copier , the most derived class, is responsible for creation of PoweredDevice Note that this is true even in a single inheritance case: if Copier was singly inherited from Printer , and Printer was virtually inherited from PoweredDevice, Copier is still responsible for creating PoweredDevice http://www.learncpp.com/cpp-tutorial/118-virtual-base-classes/

Abstract Class   An abstract class is a class that is designed to be specifically used as a base class An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class

Abstract Class class AbstractClass { public:   class AbstractClass { public: virtual void AbstractMemberFunction() = 0; // Pure virtual function makes // this class Abstract class. virtual void NonAbstractMemberFunction1(); // Virtual function. void NonAbstractMemberFunction2(); };