C/c++ 4 Yeting Ge.

Slides:



Advertisements
Similar presentations
L7: Exceptions Problem Description Error Handling –terminate the program –return a value representing an error –return a legal value and leave the program.
Advertisements

Introduction to C++ An object-oriented language Unit - 01.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Exception Handling Handling an unexpected behaviour Unit - 08.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Definition Class In C++, the class is the construct primarily used to create objects.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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.
C++ Yeting Ge.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Java and C++, The Difference An introduction Unit - 00.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
1 CSC241: Object Oriented Programming Lecture No 27.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
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.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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.
1.Which of the following statements is correct?
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
 2006 Pearson Education, Inc. All rights reserved Templates.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Lesson 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C ++ MULTIPLE CHOICE QUESTION
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
Topics: Templates Exceptions
C++ INTERVIEW QUESTIONS
Object-Oriented Programming (OOP) Lecture No. 45
Finally! Discussing a language!
Templates.
Polymorphism Lec
Classes and Data Abstraction
Name: Rubaisha Rajpoot
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Overview of C++ Overloading
Exception Handling.
Exceptions, Templates, and the Standard Template Library (STL)
Overview of C++ Polymorphism
Functions Lecture 5.
C++ Programming CLASS This pointer Static Class Friend Class
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Presentation transcript:

c/c++ 4 Yeting Ge

Review Static variables/functions Friend functions/classes Overloading Operators Virtual functions/Overriding

Examples of virtual functions By pointers 42.cpp By reference 43.cpp By member function 44.cpp 45.cpp

Pure virtual functions & abstract class A function declared without definition virtual ret_type func_name(arg_list)= 0; Abstract class A class contains one or more pure functions Can not be instantiated Can be used to define pointers

Function Templates Generic function for different types. E.g. get the min value of three variables (int, float, char…) Define function templates 46.cpp Template<class S, class T…> func_name(…) Func_name<type name>(…) something like macro More powerful than macro

Using template functions Func_name<type_name list>(…) 47.cpp Generic quick sort 47-1.cpp

No automatic type conversion in templates int i; long j; template<class T> T func(T x, T y) {return x +y;} func(i,j)

Class templates Generic classes Define class templates 48.cpp Template<class T> Class {…} Template<class T> ret_type class_name<type_name> :: func_name ( … ){ } 48.cpp

Class template examples Class_name<type_name..> obj… Array class 49.cpp Linked list 50.cpp

overload template functions It is possible to overload a template function. 51.cpp

Template specialization An example from www.cplusplus.com 52.cpp template <> class class_name <type>

Template value parameters Template can have parameters other than type names 53.cpp

Default value for templates paramters template <class T = char> // With a default value. template <int Tfunc (int)> // A function as parameter. the implementation (definition) of a template class or function must be in the same file as the declaration.

Execptions try { // code to be tried throw exception; } catch (type  exception) { // code to be executed in case of exception }

Examples of exception 54.cpp throw out an object. 55.cpp

Uncaught exception Uncaught exceptions will be thrown into outer scope. If no catch, usually program will terminate. Void terminate();

I/O system iostream.h cin,cout,cerr,clog iostream,fstream,strstream Flags 57.cpp 58.cpp Overload “<<“ 59.cpp 60.cpp 61.cpp 62.cpp 63.cpp 64.cpp