Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology
|
|
- Carol Davidson
- 5 years ago
- Views:
Transcription
1 Lesson 11: Generic Programming 1
2 Function templates 2
3 General Occasionally, we like to write once but have many functions/methods with arguments of different types, instead of writing multiple overloaded functions int max(int a, int b) { return a>b? a:b; } double max(double a, double b) { return a>b? a:b; } float max(float a, float b) { return a>b? a:b; } programming at a more generic level: consider type of variables as parameters (type parameterization) Function template: helps to define functions having arguments of different types (late type determination) Can be considered as writing multiple overloaded functions with common algorithm Types of function arguments are parameters of the template Function templates are not functions, but their instances are 3
4 Defining a function template Example 1: template <typename T> void swap(t& a, T& b) { T c = a; a = b; b = c; } T can be any type used of arguments a, b and c T is resolved at compile time for each function call T is parameter of template, while a, b are parameters of functions Example 2: template <typename Containter, typename Object> void push(containter& s, Object o) {} Possible to use class keyword instead of typename template <class T> void swap(t& a, T& b) {} 4
5 Calling a template function Function calls with explicit types: max<int>(a, b); max<double>(x, y); swap<string&>(s1, s2); swap<worker*&>(p1, p2); push<list&, Student>(l, st); with implicit types: int a, b; double x, y; max(a, b); // max<int>(a, b); max(x, y); // max<double>(x, y); max(a, x); // error (ambiguous) 5
6 Overloading function templates Function templates can also be overloaded (like normal functions) template <typename T> T max(t a, T b) { } template <typename T> T max(t a, T b, T c) { } template <typename T> T max(t* arr, int n) { } Calling overloaded function template: max<int>(10, 20); max('c', 'f'); max<double>(1.5, 2.1, 3.14); max("1un34k", 6); 6
7 Specialization of function templates Possibly define special versions of function templates for particular argument types: template<typename T> T max(t a, T b) { return a>b? a:b; } template<> const char* max(const char* a, const char* b) { return strcmp(a, b) == 1? a:b; } Partial specialization: template <typename Containter, typename Object> void push(containter& s, Object o) {} template <typename Object> void push(stack& s, Object o) { } 7
8 Not only argument types can be parameterized but also return type template <typename Worker, typename Product> Product makeprod(worker& w) { w.work(); return w.getresult(); } and local variable types template <typename List, typename Iterator> void foreach(const List& l) { Iterator i = l.first(); for (; i!=l.last(); i = i.next()) dosmth(i.get()); } 8
9 Template parameters are not only types but can also be values used as constants template<typename Object, int N> Object* makearray() { return new Object[N]; } string* p1 = makearray<string, 5>(); Student* p2 = makearray<student, 10>(); Both types and values as parameters template<typename T, T min, T max> T range(t t) { return t<min? min : (t>max? max : t); } 9 y = range<double, -1.5, 2.>(x); b = range<int, 10, 20>(a);
10 Remarks when using function templates The implementation of a function template is only compiled when argument types are resolved if a function template is defined in a library, then its prototype and definition (implementation) must be put in the.h file (it s possible to put the definition in a separated file then include this file in the.h file) Only when there are calls made with specific argument types, corresponding functions will be generated from the template int a = 10, b = 20; swap(a, b); // generates: void swap(int&, int&) {} swap<float>(x, y); // void swap(float&, float&) {} Probably get errors with only some particular argument types template <typename T> T divide(t a, T b) { return a/b; } 10 double z = divide(1.5, 0.5); // OK const char* c = divide("ssss", "dddd"); // error
11 Class templates 11
12 General Similar to functions, classes can also be generically defined by templates class templates Declaring types used within a class as parameters template<typename Object> class Array { private: int N; Object* p; public: void setat(int i, Object o) {} Object& operator[](int i) {} 12
13 Methods defined outside of class template<typename Object> class Array { private: int N; Object* p; public: Array(int N); ~Array(); void setat(int i, Object o); int length() const; Object& operator[](int i); template<typename Object> Array<Object>::Array(int N) { this->n = N; p = new Object[N]; } template<typename Object> Array<Object>::~Array() { delete[] p; } template<typename Object> void Array<Object>:: setat(int i, Object o) { p[i] = o; } template<typename Object> int Array<Object>::length() const { return N; } template<typename Object> Object& Array<Object>:: operator[](int i) { return p[i]; } These methods are defined similarly to function templates, except the class scope 13
14 Instantiating template classes Example: Array<double> a(10); for (int i=0; i<10; i++) a.setat(i, i*2); typedef Array<string> StrArray; StrArray s(2); s[0] = string("abcd"); s[1] = string("12345"); Using function and class templates together template<typename Object> void printarray(array<object> &a) { for (int i=0; i<a.length(); i++) cout << a[i]; } 14 printarray(a); printarray(s);
15 Friend functions and classes Friend of only the corresponding class version template<typename T> class Array { friend void sort(array<t>& a); friend class Serializer<T>; Common friend with all class versions template<typename T> class A { friend void func1(); template<typename X> friend int func2(); friend class B; 15
16 Specialization of class templates template<typename Key, typename Data> class Map { Data find(key k); // full specialization template<> class Map<int, int> { int find(int k); // partial specialization template<typename Data> class Map<int, Data> { Data find(int k); 16
17 Default values of class template parameters Class template parameters can have default values, similarly to function and method arguments template<typename Object = int, int N = 100> class Pool { Pool<> p1; // Pool<int, 100> p1; Pool<string> p2; // Pool<string, 100> p2 Pool<double, 20> p3; Attn: only class templates have, function templates don t (*) * Default values of function template parameters have been added in the new C++11 standard 17
18 Static members of template classes Each class derived from template has its own static variables template<typename Data> class smartptr { static smartptr<data> nullptr; template<typename Data> static smartptr<data> smartptr<data>::nullptr; smartptr<string> p1 = smartptr<string>::nullptr; smartptr<double> p2 = smartptr<double>::nullptr; Similarly for static methods 18
19 Related class templates template<typename T> class Iterator; template<typename T> class List { Iterator<T> begin() { } Iterator<T> end() { } template<t> class Iterator { T& getdata() { } Iterator<T> next() { } List<float> l; Iterator<float> i; for (i = l.begin(); i!= l.end(); i = i.next()) cout << i.getdata() << endl; 19
20 Problems 1. Write a function to read user input for an array of arbitrary type 2. Write a function to sort an array of arbitrary type. Do this in two ways: (C) using a comparison function to determine the order of elements while sorting, (C++) using templates void sortc(void* a, int n, int size, int (*compare)(void*, void*)); template<typename T> void sortcpp(t* a, int n); 3. Write class Stack that allows to store data of any type, without need to use void* as before 4. Modify class LinkedList to allow storing data of any type 5. Modify class Iterator corresponding to above class LinkedList 6. Modify class Vector to allow manipulating vector with components of both float and double 7. Modify class String to allow working with both ASCII (char) and Unicode (wchar_t) character types 20
Comp151. 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 informationCS 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 information1/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 informationSFU CMPT Topic: Class Templates
SFU CMPT-212 2008-1 1 Topic: Class Templates SFU CMPT-212 2008-1 Topic: Class Templates Ján Maňuch E-mail: jmanuch@sfu.ca Monday 3 rd March, 2008 SFU CMPT-212 2008-1 2 Topic: Class Templates Class templates
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 informationAdvanced C++ Topics. Alexander Warg, 2017
www.kernkonzept.com Advanced C++ Topics Alexander Warg, 2017 M I C R O K E R N E L M A D E I N G E R M A N Y Overview WHAT IS BEHIND C++ Language Magics Object Life Time Object Memory Layout INTRODUCTION
More informationImplementing Abstract Data Types (ADT) using Classes
Implementing Abstract Data Types (ADT) using Classes Class Definition class classname { public: //public member functions private: //private data members and member functions }; // Note the semicolon!
More informationMore Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference
Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange
More informationTemplating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading
s s in Templating functions Comp Sci 1570 Introduction to Outline s s in 1 2 3 s s in 4 Test 1 grade distribution grade on y, each student on x, sorted by y s s in 50 or below should talk to me. Outline
More informationIntroduction to Core C++
Introduction to Core C++ Lecture 04: Template Functions and Template Classes Massimiliano Culpo 1 1 CINECA - SuperComputing Applications and Innovation Department 26.02.2013 M.Culpo (CINECA) Introduction
More informationBefore we dive in. Preprocessing Compilation Linkage
Templates Before we dive in Preprocessing Compilation Linkage 2 Motivation A useful routine to have is void swap( int& a, int &b ) int tmp = a; a = b; b = tmp; 3 Example What happens if we want to swap
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 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 information7 TEMPLATES AND STL. 7.1 Function Templates
7 templates and STL:: Function Templates 7 TEMPLATES AND STL 7.1 Function Templates Support generic programming functions have parameterized types (can have other parameters as well) functions are instantiated
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 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 informationTemplate. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong
Template Prof. Jinkyu Jeong (Jinkyu@skku.edu) TA -- Minwoo Ahn (minwoo.ahn@csl.skku.edu) TA -- Donghyun Kim (donghyun.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu
More informationProgramming in C++: Assignment Week 8
Programming in C++: Assignment Week 8 Total Marks : 20 September 9, 2017 Question 1 Consider the following code segment. Mark 2 void myfunction(int test) { try { if (test) throw test; else throw "Value
More informationIntroduction to Standard C++
Introduction to Standard C++ Lecture 04: Template Functions and Template Classes Massimiliano Culpo 1 1 CINECA - SuperComputing Applications and Innovation Department 07.04.2014 M.Culpo (CINECA) Introduction
More informationCSCI 104 Templates. Mark Redekopp David Kempe
1 CSCI 104 Templates Mark Redekopp David Kempe 2 Overview C++ Templates allow alternate versions of the same code to be generated for various data types FUNCTION TEMPLATES 3 4 How To's Example reproduced
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationLecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli
Lecture-5 Miscellaneous topics Templates W3101: Programming Languages C++ Miscellaneous topics Miscellaneous topics const member functions, const arguments Function overloading and function overriding
More informationTemplates. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. June 8, 2009
Templates Giuseppe Lipari http://retis.sssup.it Scuola Superiore Sant Anna Pisa June 8, 2009 Reuse One of the most important keywords in OO programming is code reuse If you have a piece of code that works
More informationCSCI 104 Templates. Mark Redekopp David Kempe
CSCI 104 Templates Mark Redekopp David Kempe Overview C++ Templates allow alternate versions of the same code to be generated for various data types 2 FUNCTION TEMPLATES 3 How To's Example reproduced from:
More informationDarshan Institute of Engineering & Technology for Diploma Studies
1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value
More informationInheritance and Overloading. Week 11
Inheritance and Overloading Week 11 1 Inheritance Objects are often defined in terms of hierarchical classes with a base class and one or more levels of classes that inherit from the classes that are above
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 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 informationAppendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.
A Appendix Grammar There is no worse danger for a teacher than to teach words instead of things. Marc Block Introduction keywords lexical conventions programs expressions statements declarations declarators
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 informationCAAM 420 Fall 2012 Lecture 29. Duncan Eddy
CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationWhen we program, we have to deal with errors. Our most basic aim is correctness, but we must
Chapter 5 Errors When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with incomplete problem specifications, incomplete programs, and our own errors. When
More informationInstantiation of Template class
Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double
More informationME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types
ME240 Computation for Mechanical Engineering Lecture 4 C++ Data Types Introduction In this lecture we will learn some fundamental elements of C++: Introduction Data Types Identifiers Variables Constants
More informationtemplate<typename T> cout << "The value is << value << endl; } void printdata(t value){
C++ Templates Parametric Polymorphism void printdata(int value){ cout
More informationI BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++
No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)
More informationpublic : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }
. CONSTRUCTOR If the name of the member function of a class and the name of class are same, then the member function is called constructor. Constructors are used to initialize the object of that class
More informationNote 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...
CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?
More informationTEMPLATE IN C++ Function Templates
TEMPLATE IN C++ Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.
More informationCOMP 2355 Introduction to Systems Programming
COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Templates Operator Overloading 2 Templates Syntactically similar to Java generics
More informationObject-Oriented Principles and Practice / C++
Object-Oriented Principles and Practice / C++ Alice E. Fischer September 26, 2016 OOPP / C++ Lecture 4... 1/33 Global vs. Class Static Parameters Move Semantics OOPP / C++ Lecture 4... 2/33 Global Functions
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 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 informationComp151. Generic Programming: Container Classes
Comp151 Generic Programming: Container Classes Container Classes Container classes are a typical use for class templates, since we need container classes for objects of many different types, and the types
More informationObject Oriented Design
Object Oriented Design Chapter 6 Example Activity Diagram 1 Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationPassing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates
Lecture-4 Inheritance review. Polymorphism Virtual functions Abstract classes Passing arguments to functions by Value, pointers, refrence const member functions const arguments to a function Function overloading
More informationObject-Oriented Principles and Practice / C++
Object-Oriented Principles and Practice / C++ Alice E. Fischer June 3, 2013 OOPP / C++ Lecture 9... 1/40 Const Qualifiers Operator Extensions Polymorphism Abstract Classes Linear Data Structure Demo Ordered
More informationTokens, Expressions and Control Structures
3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type
More informationPaytm 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 informationElevate your Code to Modern C++ with Automated Tooling. Peter Sommerlad
Elevate your Code to Modern C++ with Automated Tooling Peter Sommerlad Simple C++ Less Code == More Software Know your language and its (modern) idioms Don t be afraid of STL or templates Start small.
More informationBasic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces
Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program
More informationCS201 Latest Solved MCQs
Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability
More informationclass Array; // Class template declaration class Array public: // T="type",e.g.,int or float Array(int n); // Create array of n elements Array(); // C
LECTURE 5 Templates We have written a simple array class of oat variables. But suppose we want to have arrays of integers, or doubles, or something else. It's a pain to write a separate array class for
More informationStream States. Formatted I/O
C++ Input and Output * the standard C++ library has a collection of classes that can be used for input and output * most of these classes are based on a stream abstraction, the input or output device is
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More information1. The term STL stands for?
1. The term STL stands for? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d 2. Which of the following statements regarding the
More informationVariables. Data Types.
Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting
More informationChapter 2. Procedural Programming
Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic
More informationAIMS Embedded Systems Programming MT 2017
AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors
More information1 Templates. 1 int sum(const int x, const int y) { 2 return x + y; 3 } For this function to work with doubles, it must be modified to the following:
1 Templates We have seen that functions can take arguments of specific types and have a specific return type. We now consider templates, which allow us to work with generic types. Through templates, rather
More informationLecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology
Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes
More informationCOM S 213 PRELIM EXAMINATION #2 April 26, 2001
COM S 213 PRELIM EXAMINATION #2 April 26, 2001 Name: Student ID: Please answer all questions in the space(s) provided. Each question is worth 4 points. You may leave when you are finished with the exam.
More informationUEE1303(1070) S 12 Object-Oriented Programming in C++
Computational Intelligence on Automation Lab @ NCTU UEE1303(1070) S 12 Object-Oriented Programming in C++ Lecture 10: Templates Function Templates And Class Templates Learning Objectives (1/2) Learn about
More informationNAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)
NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14) Some Material for your reference: Consider following C++ program. // A program
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 Summer 2005 3:00pm 4:15pm Tuesday, July 5 Name: NetID: Lab Section
More informationExample : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };
Constructors A Member function with the same name as its classis called Constructor and it is used to initilize the objects of that class type with a legal value. A Constructor is a special member function
More informationGlobal & Local Identifiers
Global & Local Identifiers the portions of a program where an identifier is defined (may be used). a variable declared inside a Block. from the Declaration statement to the end of the Block void fun()
More informationIntermediate 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 informationOOP THROUGH C++(R16) int *x; float *f; char *c;
What is pointer and how to declare it? Write the features of pointers? A pointer is a memory variable that stores the address of another variable. Pointer can have any name that is legal for other variables,
More informationAbstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles
Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can
More information8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );
- 50 - Control Structures: 8. Functions (II) Arguments passed by value and by reference. Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This
More informationGridKa School 2013: Effective Analysis C++ Templates
GridKa School 2013: Effective Analysis C++ Templates Introduction Jörg Meyer, Steinbuch Centre for Computing, Scientific Data Management KIT University of the State of Baden-Wuerttemberg and National Research
More informationProgramming in C++: Programming Test-2
Programming in C++: Programming Test-2 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 20,
More informationAbsolute C++ Walter Savitch
Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents
More informationFor Teacher's Use Only Q No Total Q No Q No
Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9
More informationIntroduction to Programming
Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines
More informationFinancial computing with C++
Financial Computing with C++, Lecture 6 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 6 - p2/24 Outline Linked lists Linked
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Winter 2011 Lecture 08 - Inheritance continued School of Computer Science McGill University March 8, 2011 Last Time Single Inheritance Polymorphism: Static Binding vs Dynamic
More informationMotivation for Templates
Motivation for You want both: a list of Location objects a list of MazeMonster objects 1 How can you accomplish this by writing one LinkedList class? state all the ways you can think of doing this state
More informationQuiz Start Time: 09:34 PM Time Left 82 sec(s)
Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability
More informationIntroduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts
Introduction to C++ 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 informationCS304 Object Oriented Programming
1 CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes?
More informationGeneric Programming: Overloading and Templates
H.O. #9 Fall 2015 Gary Chan Generic Programming: Overloading and Templates N:9; D:6,11,14 Outline Function overloading Operator overloading and copy constructor An example on string Function templates
More informationIntro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.
Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype
More informationBinding and Variables
Binding and Variables 1. DEFINITIONS... 2 2. VARIABLES... 3 3. TYPE... 4 4. SCOPE... 4 5. REFERENCES... 7 6. ROUTINES... 9 7. ALIASING AND OVERLOADING... 10 8. GENERICS AND TEMPLATES... 12 A. Bellaachia
More informationCS 247: Software Engineering Principles. ADT Design
CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data
More informationECE Fall 2014, Final Exam
ECE 30862 Fall 2014, Final Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. THE LAST TWO PAGES ARE THE ANSWER SHEETS. TEAR THEM OFF AND PUT ALL ANSWERS ON THEM. TURN IN BOTH
More informationChapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.
hapter 1 INTRODUTION SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. ++ Programming: Advanced Objectives You will learn: Anonymous class types. Nested class declarations. Incomplete declarations. Pointers to class
More informationSoftware Development with C++ Templates
Software Development with C++ Templates Lab Submission 1 Exercises should be solved in groups of two. However, with approval from the lecturer, exercises may also be solved alone or in groups of three.
More informationFORM 2 (Please put your name and form # on the scantron!!!!)
CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function
More informationC++ Important Questions with Answers
1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children
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 informationAbout Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals
More informationC++ Objects Overloading Other C++ Peter Kristensen
Peter Kristensen 2012-12-03 Peter Kristensen Outline 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 Peter Kristensen Overview What s this thing anyway 1 What s this thing
More informationArrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors
Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
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 informationcast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */
cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */ #include #include /* The above include is present so that the return type
More information