UEE1303(1070) S12: Object-Oriented Programming Template

Similar documents
UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

UEE1302(1066) F12: Introduction to Computers and Programming. Lab 12: Pointer & Dynamic Array (II)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Lab Instructor : Jean Lai

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

CMPS 221 Sample Final

Sample Code: OUTPUT Daily Highs & Lows

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Lecture on pointers, references, and arrays and vectors

Week 3: Pointers (Part 2)

Due Date: See Blackboard

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Linked List using a Sentinel

System Design and Programming II

Homework Assignment #2 (revised)

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Ch 6. Functions. Example: function calls function

Object oriented programming

1. Which of the following best describes the situation after Line 1 has been executed?

LAB 7.1 Working with One-Dimensional Arrays

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

Intermediate Programming, Spring 2017*

Programming in C++: Assignment Week 8

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

C++ STREAMS; INHERITANCE AS

CMSC 202 Midterm Exam 1 Fall 2015

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Due Date: See Blackboard

Introduction to Object-Oriented Programming with C++

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Getting started with C++ (Part 2)

Due Date: See Blackboard

The following is a typical execution run of this program:

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Module 7 b. -Namespaces -Exceptions handling

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Use the dot operator to access a member of a specific object.

Lecture 23: Pointer Arithmetic

CS24 Week 3 Lecture 1

Pointers. Variable Declaration. Chapter 10

Intermediate Programming, Spring 2017*

CSCE Practice Midterm. Data Types

Stack memory - "scratch pad" memory that is used by automatic variables.

Exceptions, Case Study-Exception handling in C++.

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Chapter 15 - C++ As A "Better C"

Due Date: See Blackboard

CS2141 Software Development using C/C++ C++ Basics

Bruce Merry. IOI Training Dec 2013

CSI33 Data Structures

CS3157: Advanced Programming. Outline

Due Date: See Blackboard

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

ET 181. Purpose: The purpose of this lab is to become familiar with using arrays in C++.

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

BITG 1113: Array (Part 1) LECTURE 8

I/O Streams and Standard I/O Devices (cont d.)

Introduction to C++ (Extensions to C)

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

1. In C++, reserved words are the same as predefined identifiers. a. True

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

W3101: Programming Languages C++ Ramana Isukapalli

Exercise 1.1 Hello world

CS 1337 Computer Science II Page 1

Introduction to C++ Systems Programming

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Definition Matching (10 Points)

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Problem Solving: Storyboards for User Interaction

FRIEND FUNCTIONS IN CPP

AN OVERVIEW OF C++ 1

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Computer Programming

TEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010

Understanding main() function Input/Output Streams

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Object Oriented Design

Local and Global Variables

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Programming I Lecture 7

Operator overloading

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Transcription:

UEE1303(1070) S12: Object-Oriented Programming Template What you will learn from Lab 12 In this laboratory, you will learn how to use function template and class template. TASK 12-1 FUNCTION TEMPLATE A function template defines a function that takes type parameters. Please execute lab12-1. Here is an example to maintain memory allocation for different types. // lab12-1.cpp #include <iostream> #include <cassert> #include <iomanip> using namespace std; T *new1d(int n, T k) T *vec = new T [n]; for (int i=0;i<n;i++) vec[i] = k; return vec; void delete1d(t *vec) assert(vec!= NULL); delete [] vec; void display1d(t *vec, int n) for (int i=0;i<n;i++) cout << vec[i] << " "; cout << endl; int *ivec = new1d<int>(10,1); display1d<int>(ivec,10); delete1d<int>(ivec); 1/6

double *dvec = new1d<double>(10,3.2); display1d<double>(dvec,10); delete1d<double>(dvec); TASK 12-2 FUNCTION TEMPLATE: SPECIALIZATION In program lab12-1, you can maintain a specific version of display1d() for double. Please add this specialization of display1d<t> to lab12-1 and execute the program again. template <> void display1d(double *vec, int n) cout << fixed << setprecision(2); for (int i=0;i<n;i++) cout << vec[i] << " "; cout << endl; TASK 12-3 CLASS TEMPLATE You can also define a class template by adding prefix template<class T>. // lab12-3.cpp #include <iostream> #include <iomanip> using namespace std; class Point2D T x; T y; Point2D():x(T(0)),y(T(0)) Point2D(T a, T b):x(a),y(b) void display() const; ; void Point2D<T>::display() const cout << x << " " << y << endl; Point2D<int> p1; 2/6

p1.display(); Point2D<double> p2(1.9,3.4); p2.display(); TASK 12-4 CLASS TEMPLATE: SPECIALIZATION Here define a specialization of the template class Point2D<T> when its elements are complex number. // lab12-4.cpp #include <iostream> using namespace std; class Complex double real; double image; Complex(const double a, const double b):real(a), image(b) Complex(const Complex &c):real(c.real), image(c.image) void display() const cout << real << << image << endl; ; // template Point2D defined in lab12-3 template <> class Point2D<Complex> Complex x; Complex y; Point2D(const Complex &a, const Complex &b):x(a),y(b) void display() const; ; void Point2D<Complex>::display() const x.display(); y.display(); 3/6

Complex c1(1.9,3.4); Complex c2(2.0,1.3); Point2D<Complex> pc(c1,c2); pc.display(); TASK 12-5 EXERCISE 1. STATISTICAL ANALYSIS Please finish the undefined function template in ex12-1. class Point2D int x; int y; // add any member if necessary ; template<class T> void analysis(int n, int k = 0) T *vec = new1d<t> (n, k); rand1d<t>(vec,n); // for int 1~10, for double 0.00~10.00, for char a~z, // for Point2D x: 0~9 y:0~9 display1d<t>(vec,n); sort1d<t>(vec,n); display<t>(vec,n); int n; cout << "Enter n: "; cin >> n; srand(1); analysis<int>(n); analysis<double>(n); analysis<char>(n); analysis<point2d>(n); 4/6

The output of the program should like as, >./ex12-1 Enter n: 8 2 8 5 1 10 5 9 9 1 2 5 5 8 9 9 10 9.62 4.64 7.05 1.45 2.81 8.27 9.61 4.91 1.45 2.81 4.64 4.91 7.05 8.27 9.61 9.62 f i r c v s c x c c f i r s v x (2, 2) (1, 6) (8, 5) (7, 6) (1, 8) (9, 2) (7, 9) (5, 4) (1, 6) (1, 8) (2, 2) (5, 4) (7, 6) (7, 9) (8, 5) (9, 2) Hint: use x coordinate first to compare Point2D and then compare y coordinate if x coordinates for two Point2Ds are the same. 2. VECTOR Please finish the undefined function template in ex12-2. The main function is like as follows. int n; cout << "Enter n: "; cin >> n; Vector<double> dvec(n,1); double *b = new double[n]; for (int i=0;i<n;i++) b[i] = i; Vector<double> dvec2(n,b); cout << "dvec = "; dvec.display(); cout << "dvec2 = "; dvec2.display(); dvec2 += dvec; cout << "new dvec = "; dvec2.display(); double c = dot(dvec, dvec2); cout << "dot(dvec, dvec2) = " << c << endl << endl; srand(1); Point2D *v = new Point2D[n]; rand1d<point2d>(v,n); //0~9 Vector<Point2D> vp1(n,1); Vector<Point2D> vp2(n,v); cout << "vp1 = "; vp1.display(); cout << "vp2 = "; vp2.display(); vp2 += vp1; 5/6

cout << "new vp2 = "; vp2.display(); Point2D d = dot(vp1, vp2); cout << "dot(vp1, vp2) = " << d << endl; The execution results are, Enter n: 3 dvec = 1 1 1 dvec2 = 0 1 2 new dec2 = 1 2 3 dot(dvec, dvec2) = 6 vp1 = (1,1) (1,1) (1,1) vp2 = (1,7) (4,0) (9,4) new vp2 = (2,8) (5,1) (10,5) dot(vp1, vp2) = (17,14) Hint: dot operation for two vector is defined as n 1 1 2 i= 0 v ( i) v ( i), and multiplication for two Point2Ds is written as Point2D(p1.x*p2.x, p1.y*p2.y); Please declare class Point2D in Point2D.h and define its functionality in Point2D.cpp. Please declare template class Vector in Vector.h and define its functionality in Vector.cpp. class Vector int len; T* vec; // add any member if necessary template<class S> friend S dot (const Vector<S> &, const Vector<S> &); ; 6/6