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(); };