C++ Objects Overloading Other C++ Peter Kristensen

Similar documents
3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to Programming Using Java (98-388)

COEN244: Class & function templates

CS

Inheritance. Transitivity

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

ECE 2400 Computer Systems Programming Fall 2017 Topic 15: C++ Templates

C++ Important Questions with Answers

Tokens, Expressions and Control Structures

Advanced Systems Programming

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Java Fundamentals (II)

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS201 Latest Solved MCQs

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Object Orientated Analysis and Design. Benjamin Kenwright

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

1. The term STL stands for?

Introduction to C++ with content from

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Topics. bool and string types input/output library functions comments memory allocation templates classes

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

1 Shyam sir JAVA Notes

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

COMP 2355 Introduction to Systems Programming

CE221 Programming in C++ Part 1 Introduction

G52CPP C++ Programming Lecture 13

Stream Computing using Brook+

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS304 Object Oriented Programming

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Instantiation of Template class

For Teacher's Use Only Q No Total Q No Q No

STRUCTURING OF PROGRAM

VARIABLES AND TYPES CITS1001

CS201 Some Important Definitions

Fast Introduction to Object Oriented Programming and C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Chapter 7 Constructors and Other Tools. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Lesson 13 - Vectors Dynamic Data Storage

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

An introduction to C++ template programming

Introduction to C++ Systems Programming

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Object-Oriented Programming

Class, Variable, Constructor, Object, Method Questions

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

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

III. Classes (Chap. 3)

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Data Abstraction. Hwansoo Han

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Introduction to C++11 and its use inside Qt

9 Working with the Java Class Library

Cpt S 122 Data Structures. Templates

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

September 10,

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS 162, Lecture 25: Exam II Review. 30 May 2018

Java: introduction to object-oriented features

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

10. Abstract Data Types

Properties of an identifier (and the object it represents) may be set at

CLASSES AND OBJECTS IN JAVA


Introduce C# as Object Oriented programming language. Explain, tokens,

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

PHY4321 Summary Notes

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS260 Intro to Java & Android 03.Java Language Basics

INHERITANCE. Spring 2019

Declarations and Access Control SCJP tips

CSE 303: Concepts and Tools for Software Development

G52CPP C++ Programming Lecture 18

CS 251 Intermediate Programming Inheritance

Interview Questions of C++

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

C++ Primer for CS175

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

I m sure you have been annoyed at least once by having to type out types like this:

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

And Even More and More C++ Fundamentals of Computer Science

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

CSI33 Data Structures

Transcription:

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 anyway 2 3 Functions Operators 4 Templates STL A better C 11 3 / 33

is... What s this thing anyway Almost a superset of C Object Oriented A better C than C 4 / 33

Some features in What s this thing anyway Namespaces Exceptions Function overloading Operator overloading Run-time type information (RTTI) Templates Standard Template Library 5 / 33

11 What s this thing anyway A major upgrade to Known as 0x because it was supposed to be finalized before 2010 Both clang and gcc supports more than 90% of the functinallity Lambdas For Each Auto types 6 / 33

Compiling What s this thing anyway Same as for C, just use g++ instead of gcc clang++ instead of clang 7 / 33

Overview 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 8 / 33

Like C functions, classes have a declaration, and a definition Syntax resembles C structs The usual suspects Visibility declaration private/public/protected Members variables Methods Static members and methods this keyword 9 / 33

Class sample Class declaration (.h,.hpp,.hh) class A { private: int member1; public: A(); int getmember(); void setmember(int m); ; Class definition (.cc,.cpp,.cxx) A::A() { member1 = 42; int A::getMember() { return member1; void A::setMember(int m member1 = m; //this.member1 = m; 10 / 33

Initializing Can be allocated on stack A obj; // will implicitly call the constructor int k = obj.getmember(); printf("%d\n",k);... or heap A* obj1 = new A(); int g = obj1->getmember(); printf("%d\n",g); delete obj; 11 / 33

Code in the header It s legal to put the implementation in the header file Needed for templates Makes compiling slower 12 / 33

can inherit other classes Call their methods Read members If public or protected Etc. 13 / 33

sample class B : public A { public: B() : A() { void dostuff() { int k = getmember(); // int k = A::getMember(); printf("%d\n",k); ; 14 / 33

Multiple inheritance A class can have multiple parents Need to specify the correct parent for ambiguities. Can be used as interfaces by inheriting from a abstract class 15 / 33

Multiple interfaces sample (1/2) class Human { public: bool gotmilk() { return false; int numberoflegs() { return 2; ; class Horse { public: int numberoflegs() { return 4; ; 16 / 33

Multiple interfaces sample (2/2) class Centaur : public Horse, public Human { public: void about() { bool m = gotmilk(); int n = Horse::numberOfLegs(); // ^ need to be explicit to avoid ambigu printf("milk? %d\n",m); printf("legs: %d\n",n); ; 17 / 33

Virtual inheritance When a method is overridden by a subclass The method is chosen from the static type of the class unless the method is virtual In java, every method is virtual 18 / 33

Virtual inheritance sample (1/2) class Parent { public: virtual int age() { return 42; ; class Child : public Parent { public: virtual int age() { return 12; ; 19 / 33

Virtual inheritance sample (2/2) Child* c = new Child(); Parent* p = c; printf("%d\n",p->age()); 20 / 33

Overview Functions Operators 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 21 / 33

Functions and methods Functions Operators The number, and type of arguments can be overloaded Not the return type int whatup() { return 4; int whatup(int k) { return k; 22 / 33

Default arguments Functions Operators Arguments can have default values Arguments following a argument with a default value, most also have a default value E.g. fun(int a=1, int b) is illegal int whatup2(int k=4) { return k; 23 / 33

Operator overloading Functions Operators Most operators can be overloaded using special method names E.g. operator+(int a) is called by writing x + 42 class Bing { public: int value; Bing() : value(4) { Bing operator+(int a) { Bing b = *this; b.value += a; return b; ; 24 / 33

Overview Templates STL A better C 11 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 25 / 33

Templates Templates STL A better C 11 Compile-time expansion of types and values. Used to create generic classes collections matrix template <typename T,int N> class Vector { T data[n]; ; Vector<float,3> v; T will be replaced by float, and N by 3 26 / 33

Standard template library Templates STL A better C 11 Large collection of reusable components Templates to fit any type Collections vector / list / queue map set Iterators Stream and strings files 27 / 33

Sample Templates STL A better C 11 file:stl.cpp 28 / 33

is a better C Templates STL A better C 11 Use new for allocation E.g. new float[42] creates a array of 42*sizeof(float) bytes Undeclared functions are errors Function style typecast e.g. int(42.0) instead of (int)42.0 Constant strings require space for \0 e.g. char str[5] = "abcde"; is legal C, but illegal in 29 / 33

Templates STL A better C 11 The new version of the standard Should have been finished before 2010 Type inference using auto keyword E.g. for (auto itr=list.begin(); itr!=list.end(); itr++) For Each E.g. for (int k : list)... Lambda functions Unicode strings Initializer list E.g. vector<int> v={1, 4, 2; Much more! 30 / 33

Lambda Templates STL A better C 11 Anonymous function. Can capture scope by reference or by value. auto fun = [](int k) { printf("hello :%d\n", k); fun(42); 31 / 33

Lambda scope capturing Templates STL A better C 11 int hest = 41; auto byval = [=](int k) { return hest+k; ; auto byref = [&](int k) { hest = hest+k; ; byref(1); printf("%d\n", hest); printf("%d\n", byval(1)); 32 / 33

For each Templates STL A better C 11 vector<int> vec = {1,1,2,3,5,8,13,21; for (int k : vec) { printf("%d\n", k); 33 / 33