C++ Functions, Classes, and Templates

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Vectors, lists and queues
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
CSE 332: C++ STL functors STL Functors: Functions vs. Function Objects STL Functors support function call syntax Algorithms invoke functions and operators.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers (long) or intervals (long, long)‏
Functional Programming Think Differently!! Functional languages are expressive, accomplishing tasks using short, succinct and readable code. Functional.
ITEC 320 C++ Examples.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Classes and Data Structures Jeffrey S. Childs
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C++ Functions A bit of review (things we’ve covered so far)
1 C++ Classes and Data Structures Course link…..
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Motivation for Generic Programming in C++
Overview 4 major memory segments Key differences from Java stack
C++ Templates.
Generic Algorithms (TIC++V2:C6)
Motivation and Overview
Command Line Arguments
Generic Programming Techniques in C++
The C++ Algorithm Libraries
Collections Intro What is the STL? Templates, collections, & iterators
Overview 4 major memory segments Key differences from Java stack
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Built-In (a.k.a. Native) Types in C++
Pointers & Functions.
Code::Block vs Visual C++
C++ File Structure and Intro to the STL
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Pass by Reference.
Namespaces How Shall I Name Thee?.
C++ Constructor Insanity CSE 333 Summer 2018
C++ File Structure and Intro to the STL
Pointers and dynamic objects
Pointers & Functions.
Lesson 25 Miscellaneous Topics
CS1201: Programming Language 2
Modifying Objects Assignment operation Assignment conversions
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Presentation transcript:

C++ Functions, Classes, and Templates C++ functions encapsulate behavior Data used/modified by a function must be passed in via parameters Data produced by a function must be passed out via return type Classes (and structs) encapsulate related data and behavior Member variables maintain each object’s state Member functions (methods) and operators have direct access to member variables Templates are used to make classes/structs/functions generic For example, vector class template can hold different data types For example, sort algorithm works with different containers as well

Declaring, Defining, Calling C++ Functions // function declarations in myfile.h void foo (); void baz (int j); // function definitions in myfile.cpp void baz (int j){ cout << j << endl; // prints passed value } void foo (){ int i = 7; // foo calls baz, passing variable i to it baz (i);

Declaring, Defining, Using C++ Structs/Classes // declaration (in Point2d.h) struct Point2D { Point2D (int x, int y); bool operator< (const Point2D &) const; int x_; int y_; }; // definitions (in Point2d.cpp) Point2D::Point2D (int x, int y) : x_(x), y_(y) {} bool Point2D::operator< (const Point2D & p2d) const { return (x_ < p2d.x_) || ((x_ == p2d.x_) && (y_ < p2d.y_)); } promises not to modify object on which it’s called base class/struct and member initialization list scoping operator

Using C++ Struct/Class Templates (with Structs) // using standard library code #include <vector> using namespace std; // using user-defined code #include “point2d.h” // main function definition int main (int, char *[]) { vector<Point2D> v; // must give a type here v.push_back(Point2D(2,3)); v.push_back(Point2D(1,4)); return 0; }

Using C++ Function Templates (STL Algorithms) // same as before, and add algorithm library #include <vector> #include <algorithm> using namespace std; #include “point2d.h” int main (int, char *[]) { vector<Point2D> v; v.push_back(Point2D(2,3)); v.push_back(Point2D(1,4)); // reorders the points in the vector // note that you don’t give a type here! sort (v.begin(), v.end()); return 0; }

Another Useful STL Algorithm #include <string> #include <iostream> #include <algorithm> using namespace std; int main (int argc, char * argv[]) { if (argc != 2) { cout << “usage: ” << argv[0] << “ <string>” << endl; return -1; } string s(argv[1]), t(argv[1]); do { next_permutation(s.begin(), s.end()); cout << s << endl; } while (s != t); return 0;