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

Similar documents
Introduction to C++ (Extensions to C)

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

Exercise 1.1 Hello world

File I/O Christian Schumacher, Info1 D-MAVT 2013

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

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

G52CPP C++ Programming Lecture 18

Due Date: See Blackboard

Lab Instructor : Jean Lai

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Understanding main() function Input/Output Streams

Intermediate Programming, Spring 2017*

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

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

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

CSCE Practice Midterm. Data Types

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

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

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

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

CMPS 221 Sample Final

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Classes in C++98 and C++11

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS3157: Advanced Programming. Outline

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

CSE 303: Concepts and Tools for Software Development

EL2310 Scientific Programming

CS 247: Software Engineering Principles. ADT Design

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Set Implementation Version 1

Pointers, Dynamic Data, and Reference Types

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Linked List using a Sentinel

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Strings and Stream I/O

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

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

Streams. Rupesh Nasre.

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.

Week 3: Pointers (Part 2)

CS24 Week 3 Lecture 1

Lambda functions. Zoltán Porkoláb: C++11/14 1

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

Streams. Ali Malik

CSE 333 Lecture 9 - intro to C++

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

Object oriented programming

CS242 COMPUTER PROGRAMMING

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

A Tour of the C++ Programming Language

Due Date: See Blackboard

CSCE Practice Midterm. Data Types

Arrays. Week 4. Assylbek Jumagaliyev

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

C C C C++ 2 ( ) C C++ 4 C C

Lecture on pointers, references, and arrays and vectors

More Advanced Class Concepts

A Tour of the C++ Programming Language

GCC : From 2.95 to 3.2

Due Date: See Blackboard

IS 0020 Program Design and Software Tools

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CMSC 202 Midterm Exam 1 Fall 2015

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

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

Increment and the While. Class 15

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

CS302 - Data Structures using C++

Computer Programming

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

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

Module 9. Templates & STL

Lecture 5 Files and Streams

G52CPP C++ Programming Lecture 17

CS 376b Computer Vision

A <Basic> C++ Course

C and C++ 7. Exceptions Templates. Alan Mycroft

Apllications. February 23, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications. pointers and const

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

pointers & references

Jan 27, C++ STL Streams. Daniel Maleike

Programmazione. Prof. Marco Bertini

Your First C++ Program. September 1, 2010

A <Basic> C++ Course

The Memory Manager Project

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

CS

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Why C++? C vs. C Design goals of C++ C vs. C++ - 2

C++ Exception Handling 1

Comp151. Generic Programming: Container Classes

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

Transcription:

struct Buffer { Buffer(int s) { buf = new char[s]; ~Buffer() { delete [] buf; char *buf; ; struct FBuffer : public Buffer { FBuffer(int s) : Buffer(s) { f = fopen("file", "w"); ~FBuffer() { fclose(f); void write() { fwrite(buf, 1, 40, f); FILE *f; ;

struct Buffer { Buffer(int s); ~Buffer(); char *buf; ; struct FBuffer : public Buffer { FBuffer(int s); ~FBuffer(); void write(); FILE *f; ; Buffer *buf = new Buffer(128); delete buf; //

struct Buffer { Buffer(int s); ~Buffer(); char *buf; ; struct FBuffer : public Buffer { FBuffer(int s); ~FBuffer(); void write(); FILE *f; ; FBuffer *fbuf = new FBuffer(128); delete fbuf; //

struct Buffer { Buffer(int s); ~Buffer(); char *buf; ; struct FBuffer : public Buffer { FBuffer(int s); ~FBuffer(); void write(); FILE *f; ; Buffer *fbuf = new FBuffer(128); delete fbuf; // only ~Buffer is called

struct Buffer { Buffer(); virtual ~Buffer(); char *buf; ; struct FBuffer : public Buffer { FBuffer(); virtual ~FBuffer(); void write(); FILE *f; ; Buffer *fbuf = new FBuffer; delete fbuf; //

// C cast char *buf = (char *)malloc(128); // C-style cast float b = 98.6; int a = int(b);

// C-style cat casts class Cat { ; class Tiger : public Cat { ; class Persian : public Cat { ; Cat *c = new Persian; Tiger *t = (Tiger *)c; // whoops!

// valid up-cast Tiger *t = new Tiger; Cat *c1 = (Cat *)t; Cat *c2 = static_cast<cat *>(t); Cat *c3 = dynamic_cast<cat *>(t);

// almost valid down-cast Cat *c = new Tiger; Tiger *t1 = (Tiger *)c; Tiger *t2 = static_cast<tiger *>(c); Tiger *t3 = dynamic_cast<tiger *>(c); // compile error

// valid down-cast class Cat { virtual void purr() { ; class Tiger : public Cat { ; class Persian : public Cat { ; Cat *c = new Tiger; Tiger *t1 = (Tiger *)c; Tiger *t2 = static_cast<tiger *>(c); Tiger *t3 = dynamic_cast<tiger *>(c);

// invalid down-cast Cat *c = new Persian; Tiger *t1 = (Tiger *)c; Tiger *t2 = static_cast<tiger *>(c); Tiger *t3 = dynamic_cast<tiger *>(c); // t1 & t2 are invalid pointers // t3 is NULL

void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main() { int x = 2, y = 3; swap(&x, &y);

void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; int main() { int x = 2, y = 3; swap(x, y);

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0;

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0;

SNDFILE *open(const char *); count_t seek(sndfile *, count_t); int error(sndfile *);

SNDFILE *sf_open(const char *); count_t sf_seek(sndfile *, count_t); int sf_error(sndfile *);

namespace sf { SNDFILE *open(const char *); count_t seek(sndfile *, count_t); int error(sndfile *);

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0;

std::cout << "Hello, World!" << std::endl; using namespace std; cout << "Hello, World!" << endl; using std::cout; using std::endl; cout << "Hello, World!" << endl;

g++ -E hello.cpp namespace std { extern istream cin; extern ostream cout; extern ostream cerr;

namespace std { extern istream cin; extern ostream cout; extern ostream cerr; class ActionLawsuit { ;

extern istream cin; extern ostream cout; extern ostream cerr; class ActionLawsuit { ;

namespace super { namespace std { extern istream cin; extern ostream cout; extern ostream cerr; class ActionLawsuit { ; super::std::actionlawsuit;

extern

namespace std { extern istream cin; extern ostream cout; extern ostream cerr;

ostream cout; #include <iostream> #include <iostream> int main() { cout << "i"; foo(); int foo() { cout << "Phone";

ostream cout; ostream cout; int main() { cout << "i"; foo(); int foo() { cout << "Phone"; ld: 1 duplicate symbol for architecture x86_64

extern ostream cout; extern ostream cout; int main() { cout << "i"; foo(); int foo() { cout << "Phone";

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0;

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; int a = 2 << 1; return 0;

struct vec2 { vec2(float x, float y) : x(x), y(y) { float x, y; ; int main() { vec2 a(1, 0); vec2 b(1, 3); vec2 c = a + b; // compile error vec.cpp: In function int main() : vec.cpp:12: error: no match for operator+ in a + b

vec2 vec2::add(const vec2 &o) { return vec2(x + o.x, y + o.y); int main() { vec2 a(1, 0), b(1, 3); vec2 c = a.add(b);

vec2 vec2::operator +(const vec2 &o) { return vec2(x + o.x, y + o.y); int main() { vec2 a(1, 0), b(1, 3); vec2 c = a + b; vec2 d = a.operator+(b);

vec2 operator +(vec2 &v, const vec2 &o) { return vec2(x + o.x, y + o.y); int main() { vec2 a(1, 0), b(1, 3); vec2 c = a + b;

a + b a - b a * b a / b a % b a < b a <= b a == b a >= b a > b a!= b a && b a b a & b a b a ^ b a << b a >> b a, b a[b] vec2 operator+(const vec2 &o);

+a -a ++a a++ --a a--!a ~a *a &a vec2 operator+();

a = b a += b a -= b a *= b a /= b a %= b a &= b a = b a ^= b a <<= b a >>= b a = (b += c) vec2 &vec2::operator+=(const vec2 &o) { x += o.x; y += o.y; return *this; this

struct Foo { char *str() const { return "Foo!"; ; ostream & operator<<(ostream &os, const Foo &f) { return os << f.str(); int main() { Foo f; std::cout << f << std::endl;