C++ Templates. David Camp
|
|
- Gavin Douglas
- 6 years ago
- Views:
Transcription
1 C++ Templates David Camp
2 C Marcos #define <identifier>(<parameter list>) <replacement token list> #define min(i, j) (((i) < (j))? (i) : (j)) #define max(i, j) (((i) > (j))? (i) : (j)) #define RADTODEG(x) (x * ) // What is the problem with using this macro RADTODEG(a + b) // = (a + b * )
3 C Marcos #define PRETTY_PRINT(msg) \ printf ("Message: '%s'\n", msg); if (n < 10) PRETTY_PRINT("n is less than 10"); else PRETTY_PRINT("n is at least 10"); error: expected expression before else gcc Why do we get this error message?
4 C Macro Problems There are many problems with C macros: There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking. Debugging problems. May only show the macro or may step in but the variables are wrong Cause wrong code to be generated. See more about C Macros at
5 Function Templates template<class T> const T& max( const T& i, const T& j ) return ((i < j)? i : j) ; // use in code cout << max( 3, 7 ); // outputs 7 // Compiler create the following function const int& max( const int& x, const int& y ) return((i < j)? i : j);
6 Function Templates int main() // calls max<int> (by argument deduction) std::cout << max( 3, 7 ) << std::endl; // calls max<double> (by argument deduction) std::cout << max( 3.0, 7.0 ) << std::endl; // The type is ambiguous, explicitly instantiate max<double> std::cout << max<double>( 3, 7.0 ) << std::endl; return 0;
7 Function Templates Perform identical operations for each type of data compactly and conveniently Based on the argument types provided in calls to the function, the compiler automatically instantiates separate object code functions to handle each type of call appropriately. STL algorithms are implemented as function templates
8 Class templates Class Templates Allow type-specific versions of generic classes Format: template <class T> class ClassName // Class Definition Need not use "T", any identifier will work To create an object of the class, type ClassName< type > myobject; Example: Stack< double > doublestack;
9 Class Templates Functions Template class functions Declared normally, but preceded by template<class T> Generic data in class listed as type T Template class function definition for class Constructor: template<class T> ClassName< T >::ClassName( int size ) // Creates an array of type T myarray = new T[size];
10 #define DEFAULT_STACK_SIZE 10 template < class T > class Stack public: Stack( int size = DEFAULT_STACK_SIZE ); ~Stack() delete [] stackptr; bool push( const T& pushvalue ); bool pop( T& popvalue ); private: int _size; int _top; T * _stackptr; // default constructor // destructor // push an element onto the stack // pop an element off the stack // # of elements in the stack // location of the top element // pointer to the stack ; bool isempty() const return( top == -1 ); // utility bool isfull() const return( top == size -1 ); // functions template < class T > Stack< T >::Stack( int size ) _size = size > 0? size : DEFAULT_STACK_SIZE; _top = -1; // Stack is initially empty _stackptr = new T[_ size ]; // allocate space for elements
11 // Push an element T onto the stack // return true if successful, false otherwise template< class T > bool Stack< T >::push( const T &pushvalue ) if(!isfull() ) stackptr[ ++top ] = pushvalue; // place item in Stack return( true ); // push successful return( false );// push unsuccessful // Pop an element off the stack template< class T > bool Stack< T >::pop( T &popvalue ) if(!isempty() ) popvalue = stackptr[ top-- ]; return( true ); return( false ); // remove item from Stack // pop successful // pop unsuccessful
12 int main() Stack< double > doublestack( 5 ); cout << "Pushing elements onto doublestack" << endl; for(double f = 1.1; doublestack.push( f ) ; f += 1.1 ) // if success true is returned cout << f << ' '; cout << "\nstack is full. Cannot push " << f << "\n\npopping elements from doublestack\n"; while( doublestack.pop( f ) ) // if success true is returned cout << f << ' '; cout << "\nstack is empty. Cannot pop\n"; Stack< int > intstack; cout << "\npushing elements onto intstack\n"; for(int i = 1; intstack.push( i ) ; ++i) // if success true is returned cout << i << ' '; cout << "\nstack is full. Cannot push " << i << "\n\npopping elements from intstack\n"; while( intstack.pop( i ) ) // if success true is returned cout << i << ' '; cout << "\nstack is empty. Cannot pop\n"; return( 0 );
13 Stack Sample Output Pushing elements onto doublestack Stack is full. Cannot push 6.6 Popping elements from doublestack Stack is empty. Cannot pop Pushing elements onto intstack Stack is full. Cannot push 11 Popping elements from intstack Stack is empty. Cannot pop
14 Class Templates and Non-type Parameters Can use non-type parameters in templates Default argument Treated as const Example: template< class T, int elements > Stack< double, 100 > mostrecentsalesfigures; Declares object of type Stack< double, 100> This may appear in the class definition: T stackholder[ elements ]; //array to hold stack Creates array at compile time, rather than dynamic allocation at execution time
15 Templates and Inheritance A class template can be derived from a template class A class template can be derived from a nontemplate class A template class can be derived from a class template A non-template class can be derived from a class template
16 Templates and friends Friendships allowed between a class template and Global function Member function of another class Entire class friend functions Inside definition of class template X: friend void f1(); f1() a friend function of all template classes friend void f2( X< T > & ); f2( X< int > & ) is a friend function of X< int > only. The same applies for float, double, etc. friend void A::f3(); Member function f3 of class A is a friend of all template classes
17 Templates and friends friend void C< T >::f4( X< T > & ); C<float>::f4( X< float > & ) is a friend function of class X< float > only friend classes friend class Y; Every member function of class Y is a friend with every template class made from X friend class Z< T >; Class Z< float > a friend of class X< float >, etc.
18 Template Parameters C++ templates allow one to implement a generic Stack<T> template that has a type parameter T. T can be replaced with actual types. Templates can have any number of parameters. C++ allows you to specify a default template parameter, so the definition could now look like: template Stack <class T = float, int elements = 100>
19 Template Specialization Template class specialization overrides the template-generated code by providing special definitions for specific types Template class partial specialization generate a specialization of the class for just one parameter
20 Template Specialization template<> const string& max<string>( const string& s1, const string& s2 ) if( s1.size() > s2.size() ) return( s1 ); return( s2 );
21 Partial Template Specialization template< class L, class R > L add( L left, R right ) return( left + right ); template< class R > string add( string left, R right ) ostringstream s; s << left << right; return( s.str() );
22 Template Metaprogramming template < int N > struct Factorial const int value = N * Factorial<N - 1>::value; ; template <> struct Factorial< 0 > const int value = 1; ; template <> struct Factorial< 1 > const int value = 1; ;
23 Benefits of Templates C++ promotes code reusability. Templates provide a way to re-use source code Templates are type-safe. Because the types that templates act upon are known at compile time, the compiler can perform type checking before errors occur Create a type-safe collection class (for example, a stack) that can operate on data of any type Templates avoid some of the common errors found in code that use macros like function
24 Drawbacks to Templates Compiler support Poor error messages Code bloat Hard to find errors in Templates Hard to read error messages during compiling
Chapter 12 - Templates
Chapter 12 - Templates O utline 12.1 Introd uction 12.2 Function Te m plate s 12.3 Ove rload ing Te m plate Functions 12.4 Class Te m p late s 12.5 Class Te m plate s and Non-type Param e te rs 12.6 Te
More informationTemplates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationLaboratorio di Tecnologie dell'informazione
Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Generic programming What is generic programming? Generic programming is a style of
More informationLaboratorio di Tecnologie dell'informazione. Ing. Marco Bertini
Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini bertini@dsi.unifi.it http://www.dsi.unifi.it/~bertini/ Generic programming What is generic programming? Generic programming is a style of
More informationCpt S 122 Data Structures. Templates
Cpt S 122 Data Structures Templates Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Function Template Function-template and function-template
More informationIS 0020 Program Design and Software Tools
1 IS 0020 Program Design and Software Tools Templates Lecture 10 March 23, 2004 Introduction 2 Templates Function templates Specify entire range of related (overloaded) functions Function-template specializations
More informationProgramming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor
Programming C++ Lecture 5 Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Templates S Function and class templates you specify with a single code segment an entire
More informationProgrammazione. Prof. Marco Bertini
Programmazione Prof. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Generic programming What is generic programming? Static type checking means that type checking occurs at compile
More informationSeptember 19,
September 19, 2013 1 Problems with previous examples Changes to the implementation will require recompilation & relinking of clients Extensions will require access to the source code Solutions Combine
More informationChapter 11. Abstract Data Types and Encapsulation Concepts
Chapter 11 Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept
More informationIntroduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1
Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:
More informationCpt S 122 Data Structures. Templatized Stack
Cpt S 122 Data Structures Templatized Stack Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Test # 2 Jia XU (Lab Section 3) Xiaoyu Zhang (Lab Section 3) Anh
More information! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:
Ch. 8: ADTs: Stacks and Queues Abstract Data Type A data type for which: CS 8 Fall Jill Seaman - only the properties of the data and the operations to be performed on the data are specific, - not concerned
More informationC and C++ 7. Exceptions Templates. Alan Mycroft
C and C++ 7. Exceptions Templates Alan Mycroft University of Cambridge (heavily based on previous years notes thanks to Alastair Beresford and Andrew Moore) Michaelmas Term 2013 2014 1 / 20 Exceptions
More information(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }
Exceptions Some code (e.g. a library module) may detect an error but not know what to do about it; other code (e.g. a user module) may know how to handle it C++ provides exceptions to allow an error to
More informationExceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information
Exceptions CandC++ 7. Exceptions Templates Stephen Clark University of Cambridge (heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford and Bjarne Stroustrup) Michaelmas
More informationCS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.
CS 247: Software Engineering Principles C++ Templates Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth U Waterloo CS247 (Spring 2017) p.1/16 Overloaded Functions Problem: Code in overloaded functions whose
More informationExercise 6.2 A generic container class
Exercise 6.2 A generic container class The goal of this exercise is to write a class Array that mimics the behavior of a C++ array, but provides more intelligent memory management a) Start with the input
More informationC++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
C++ TEMPLATES http://www.tutorialspoint.com/cplusplus/cpp_templates.htm Copyright tutorialspoint.com Templates are the foundation of generic programming, which involves writing code in a way that is independent
More informationShort 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 informationBilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü
Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü 1 C LECTURE 12 ++ Data Structures 2 Data Structures 17.1
More informationC++ Exception Handling 1
C++ Exception Handling 1 An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such
More informationCS201 - 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 informationAn introduction to. Templates. Generic Programming. Good old C. Metaprogramming 4/13/2017
An introduction to C++ Templates For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Templates Are C macros on Steroids Give you the power to parametrize
More informationStacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016
Stacks Gaddis 18.1, 18.3 Molly A. O'Neil CS 2308 :: Spring 2016 The Stack ADT A stack is an abstract data type that stores a collection of elements of the same type The elements of a stack are accessed
More informationCOMP6771 Advanced C++ Programming
1. COMP6771 Advanced C++ Programming Week 6 Part Three: 2016 www.cse.unsw.edu.au/ cs6771 2. Why? In C++, class names cannot be overloaded. Thus: 1 class IntStack { 2 public: 3 void push(int&); 4 void pop();
More informationOverview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43
Advanced C++ 1/43 Overview 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43 1. Expression Value Categories These are not the droids you re looking for ~Obi-wan
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationEL2310 Scientific Programming
(pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object
More informationeingebetteter Systeme
Praktikum: Entwicklung interaktiver eingebetteter Systeme C++-Tutorial (falk@cs.fau.de) 1 Agenda Classes Pointers and References Functions and Methods Function and Operator Overloading Template Classes
More information! A data type for which: ! An ADT may be implemented using various. ! Examples:
Stacks and Queues Unit 6 Chapter 19.1-2,4-5 CS 2308 Fall 2018 Jill Seaman 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are
More informationCMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC
CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible
More informationClasses. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).
UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members
More informationChapter 11. Abstract Data Types and Encapsulation Concepts ISBN
Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language
More informationC++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator
C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator What s s Not Inherited? The following methods are not inherited: Constructors
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 20 November 14, 2016 CPSC 427, Lecture 20 1/19 Templates Casts and Conversions CPSC 427, Lecture 20 2/19 Templates CPSC 427, Lecture 20
More informationBruce Merry. IOI Training Dec 2013
IOI Training Dec 2013 Outline 1 2 3 Outline 1 2 3 You can check that something is true using assert: #include int main() { assert(1 == 2); } Output: test_assert: test_assert.cpp:4: int main():
More informationIntroduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18
Stacks and Queues Week 9 Gaddis: Chapter 18 CS 5301 Fall 2015 Jill Seaman Introduction to the Stack Stack: a data structure that holds a collection of elements of the same type. - The elements are accessed
More informationCSE 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 informationObject-Oriented Programming for Scientific Computing
Object-Oriented Programming for Scientific Computing Templates and Static Polymorphism Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de
More informationCS250 Final Review Questions
CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand
More informationObject Oriented Programming COP3330 / CGS5409
Object Oriented Programming COP3330 / CGS5409 Intro to Data Structures Vectors Linked Lists Queues Stacks C++ has some built-in methods of storing compound data in useful ways, like arrays and structs.
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table
More informationImplementing Subprograms
Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing
More informationCh. 18: ADTs: Stacks and Queues. Abstract Data Type
Ch. 18: ADTs: Stacks and Queues CS 2308 Fall 2011 Jill Seaman Lecture 18 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are
More informationTemplates and Vectors
Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector
More informationPart X. Advanced C ++
Part X Advanced C ++ topics Philip Blakely (LSC) Advanced C++ 158 / 217 References The following are highly regarded books. They are fairly in-depth, and I haven t read them in their entirity. However,
More informationChapter 7: Stacks. Exercises 7.2
hapter 7: Stacks Exercises 7.2 1. mytop == 0; myrray contains 3 elements: 10, 22, 37,?,? but note that only element 10 is considered to be in the stack. 2. mytop == 1; myrray contains 3 elements: 10, 9,
More informationAnd Even More and More C++ Fundamentals of Computer Science
And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members
More informationEL2310 Scientific Programming
Lecture 14: Object Oriented Programming in C++ (ramviyas@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Classes (cont d) More on Classes and Members Group presentations Last time
More informationCSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010
CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)
More informationCMSC 4023 Chapter 11
11. Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized Abstract Data Types Encapsulation Constructs Naming Encapsulations
More informationIV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard
IV. Stacks 1 A. Introduction 1. Consider the problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker () Calculate and display base-two
More informationOBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe
OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++
More information! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:
Operator Overloading, Lists and Templates Week 6 Gaddis: 14.5, 16.2-16.4 CS 5301 Spring 2016 Jill Seaman Operator Overloading! Operators such as =, +,
More informationADTs: Stacks and Queues
Introduction to the Stack ADTs: Stack: a data structure that holds a collection of elements of the same type. - The elements are accessed according to LIFO order: last in, first out - No random access
More informationToday s lecture. CS 314 fall 01 C++ 1, page 1
Today s lecture Midterm Thursday, October 25, 6:10-7:30pm general information, conflicts Object oriented programming Abstract data types (ADT) Object oriented design C++ classes CS 314 fall 01 C++ 1, page
More information3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.
3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.
More informationCS 61B, Spring 1996 Midterm #1 Professor M. Clancy
CS 61B, Spring 1996 Midterm #1 Professor M. Clancy Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem
More informationException Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011
More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors
More informationComp151. Function Templates & Class Templates
Comp151 Function Templates & Class Templates Function Templates int max(const int& a, const int& b) string max(const string& a, const string& b) We often find a set of functions that look very much alike,
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationCOMP6771 Advanced C++ Programming
1 COMP6771 Advanced C++ Programming Week 11 Object Oriented Programming 2016 www.cse.unsw.edu.au/ cs6771 2 Covariants and Contravariants Let us assume that Class B is a subtype of class A. Covariants:
More informationWhat will happen if we try to compile, link and run this program? Do you have any comments to the code?
1 # include < iostream > 2 3 int main () 4 { 5 std :: cout
More informationTime(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }
Inheritance The following class implements time in hh:mm::ss format: class { public: () { = 0; = 0; = 0; (int h, int m, int s) { = h; = m; = s; void set(int h, int m, int s) { = h; = m; = s; void tick()
More informationCOMP 2355 Introduction to Systems Programming
COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract
More informationCOEN244: Class & function templates
COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library
More informationInterview Questions of C++
Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.
More informationAN OVERVIEW OF C++ 1
AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationCome and join us at WebLyceum
Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationReview: C++ Basic Concepts. Dr. Yingwu Zhu
Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to
More informationAbstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept of abstraction
More informationCS 162, Lecture 25: Exam II Review. 30 May 2018
CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you
More informationEL2310 Scientific Programming
Lecture 14: Object Oriented Programming in C++ (fpokorny@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Wrap Up Introduction to Object Oriented Paradigm Classes More on Classes
More informationCS93SI Handout 04 Spring 2006 Apr Review Answers
CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly
More informationDynamic Data Structures
Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded
More informationGenerics/Templates. Steven R. Bagley
Generics/Templates Steven R. Bagley Code reuse Often we implement a class Only to find that next week, we need to reuse it with just a slight tweak Collections, Containers are good examples class CIntStack
More informationIntroduction to C++ Systems Programming
Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions
More informationPOLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors
POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual
More informationPOLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE
Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public
More informationChapter 15 - C++ As A "Better C"
Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference
More informationG52CPP C++ Programming Lecture 14. Dr Jason Atkin
G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used
More information05-01 Discussion Notes
05-01 Discussion Notes PIC 10B Spring 2018 1 Exceptions 1.1 Introduction Exceptions are used to signify that a function is being used incorrectly. Once an exception is thrown, it is up to the programmer
More informationIntroduction to C++ Templates and Exceptions. C++ Function Templates C++ Class Templates Exception and Exception Handler
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler C++ Function Templates Approaches for functions that implement identical tasks for
More informationC++_ MARKS 40 MIN
C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float
More informationProgramming, numerics and optimization
Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428
More informationCSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism
CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism Review from Lecture 22 Added parent pointers to the TreeNode to implement increment and decrement operations on tree
More informationAssociate Professor Dr. Raed Ibraheem Hamed
Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 1 What this Lecture is about: Stack Structure Stack
More informationCSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many
More informationIncreases Program Structure which results in greater reliability. Polymorphism
UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the
More informationPart VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370
Part VII Object-Oriented Programming Philip Blakely (LSC) C++ Introduction 194 / 370 OOP Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip
More informationCS
CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and
More informationl Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:
Operator Overloading & Templates Week 6 Gaddis: 14.5, 16.2-16.4 CS 5301 Spring 2018 Jill Seaman Operator Overloading l Operators such as =, +,
More informationChapter 11. Abstract Data Types and Encapsulation Concepts
Chapter 11 Abstract Data Types and Encapsulation Concepts Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized
More informationCS 61B, Spring 1996 Midterm #1 Professor M. Clancy
CS 61B, Spring 1996 Midterm #1 Professor M. Clancy Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem
More information(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University
(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University Key Concepts 2 Object-Oriented Design Object-Oriented Programming
More informationContainers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science
Containers: Stack Jordi Cortadella and Jordi Petit Department of Computer Science The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list.
More information