foo.h #ifndef _FOO #define _FOO template <class T> class foo{

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Vectors, lists and queues
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
EC-241 Object-Oriented Programming
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
ITEC 320 C++ Examples.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Templates.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 Generating Random Numbers Textbook ch.6, pg
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
C++ : Operator overloading example
#define #include<iostream> using namespace std; #define GO
Inheritance Concept of Inheritance . Types of Inheritance .
Template Classes and Functions
Pointers Psst… over there.
Pointers Psst… over there.
More on Your Project Program arguments:
תכנות מכוון עצמים ו- C++ יחידה 03 מחלקות: תכונות, שיטות, הרשאות, const
C++ Functions, Classes, and Templates
Random Number Generation
Separate Compilation.
Name: Rubaisha Rajpoot
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Creation and Use of Namespaces
Screen output // Definition and use of variables
תכנות מכוון עצמים ו- C++ יחידה 01 מ- C ל- C++
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
Dynamic Memory A whole heap of fun….
Classes.
C++ Compilation Model C++ is a compiled language
Today’s Topic Const Ref:
CS1201: Programming Language 2
Arrays of Two-Dimensions
C++ Templates CSE 333 Summer 2018
Statements and flow control
CMSC 341 C++ and OOP.
Pointers & Functions.
The Stack.
CMSC 341 C++ and OOP.
C++ Templates CSE 333 Winter 2019
Programming Strings.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
Introduction to Algorithms and Programming COMP151
CMSC 341 C++ and OOP.
Presentation transcript:

foo.h #ifndef _FOO #define _FOO template <class T> class foo{ private: T f; public: void setFoo(T val); T getFoo(); }; #endif

foo.cpp #include "foo.h" template <class T> void foo<T>::setFoo(T val){ f = val; } T foo<T>::getFoo(){ return f;

fooImpl.cpp #include "foo.cpp" template foo<int>;

goodtestFoo.cpp #include <iostream> using namespace std; #include "fooImpl.cpp" int main(){ foo<int> myFoo; myFoo.setFoo(42); cout << "we got " << myFoo.getFoo() << endl; return 0; }

badTestFoo.cpp #include <iostream> using namespace std; #include "fooImpl.cpp" int main(){ foo<int> myFoo; myFoo.setFoo(42); cout << "we got " << myFoo.getFoo() << endl; foo<double> anotherFoo; //OOPS! – not instantiated anotherFoo.set(64.5); cout << "we got " << anotherFoo.getFoo() << endl; return 0; }

compile, run goodtestfoo linux3[106] % g++ -o goodtestfoo goodtestfoo.cpp fooImpl.cpp linux3[107] % goodtestfoo we got 42

compiling badtestfoo linux3[104] % g++ -o badtestfoo badtestfoo.cpp fooImpl.cpp badtestfoo.cpp: In function `int main()': badtestfoo.cpp:10: no matching function for call to `foo<double>::set(double)'