C++ STREAMS; INHERITANCE AS

Similar documents
INHERITANCE: CONSTRUCTORS,

Understanding main() function Input/Output Streams

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

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

Object Oriented Programming COP3330 / CGS5409

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

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

CS 247: Software Engineering Principles. ADT Design

Fundamental of Programming (C)

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

Chapter 14 Sequential Access Files

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

CPSC 427a: Object-Oriented Programming

C++ Input/Output: Streams

G52CPP C++ Programming Lecture 17

I/O streams

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

Inheritance and aggregation

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

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

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Due Date: See Blackboard

Simple File I/O.

Introduction to C++ (Extensions to C)

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

A SHORT COURSE ON C++

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Expert Systems artificial intelligence

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Due Date: See Blackboard

BITG 1113: Files and Stream LECTURE 10

PIC 10A Objects/Classes

C++ Input/Output Chapter 4 Topics

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Circle all of the following which would make sense as the function prototype.

CPSC 427: Object-Oriented Programming

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Inheritance, and Polymorphism.

Chapter 3: Input/Output

C++_ MARKS 40 MIN

Unit-V File operations

IS 0020 Program Design and Software Tools

Constructor - example

Input and Output File (Files and Stream )

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

Tutorial letter 202/2/2018

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

pointers & references

A <Basic> C++ Course

การทดลองท 8_2 Editor Buffer Array Implementation

Lab 2: ADT Design & Implementation

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Objects and streams and files CS427: Elements of Software Engineering

What we will learn about this week:

CS2141 Software Development using C/C++ Stream I/O

A <Basic> C++ Course

2. It is possible for a structure variable to be a member of another structure variable.

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

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Overloading Operators in C++

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Implementing an ADT with a Class

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

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Due Date: See Blackboard

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

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

Object Oriented Design

COMP322 - Introduction to C++

CS304 Object Oriented Programming Final Term

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Summary of basic C++-commands

Intermediate Programming, Spring 2017*

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Tutorial letter 202/1/2015

Linked List using a Sentinel

Chapter 2. Procedural Programming

Appendix M: Introduction to Microsoft Visual C Express Edition

Object-Oriented Design (OOD) and C++

OBJECT ORIENTED PROGRAMMING USING C++

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Make Classes Useful Again

SFU CMPT Topic: Classes

Computer Programming Inheritance 10 th Lecture

Ch 2 ADTs and C++ Classes

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

Due Date: See Blackboard

UEE1302 (1102) F10: Introduction to Computers and Programming

Comp151. Inheritance: Initialization & Substitution Principle

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSCE 110 PROGRAMMING FUNDAMENTALS

Operator Overloading in C++ Systems Programming

Documentation. Programming / Documentation Slide 42

CSCI 1061U Programming Workshop 2. C++ Basics

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Transcription:

C++ STREAMS; INHERITANCE AS PUBLIC, PROTECTED, AND PRIVATE; AGGREGATION/COMPOSITION Pages 731 to 742 Anna Rakitianskaia, University of Pretoria

C++ STREAM CLASSES A stream is an abstraction that represents a device on which input and output operations are performed To output a variable to the screen, the cout stream object is used: cout << x << endl; To get input from keyboard, the cin stream object is used: cin >> x; To use cin and cout, <iostream> header has to be included, containing definitions of istream and ostream classes Similarly, ifstream and ofstream classes are used for file input/output, and are defined in the <fstream> header

C++ STREAM CLASSES: INHERITANCE IN PRACTICE istream is the parent of ifstream ostream is the parent of ofstream ios is the parent of istream and ostream

PROTECTED MEMBERS OF A CLASS public members are accessible outside of the class, and private members are not accessible outside of the class private members of the parent class are not directly accessible from the child class What if we want to keep certain parent class members inaccessible from the outside, but allow child class to access them directly? Use the protected access modifier! protected: double length; double width;

PROTECTED MEMBERS OF A CLASS Parent class access modifier Access from the outside Access from a child class public Accessible Accessible private Not accessible Not accessible protected Not accessible Accessible Protected members: public for child classes private for the rest of the world Inheritance can also be public, protected, and private (next slide)

INHERITANCE AS PUBLIC, PROTECTED, OR PRIVATE // Inherit from parent publicly class pubchild: public parentclass ; // Inherit from parent privately class prichild: private parentclass ; // Inherit from parent protectedly class prochild: protected parentclass ; // Default: private inheritance class defchild: parentclass ;

INHERITANCE AS PUBLIC, PROTECTED, OR PRIVATE Parent Access Modifier Public Inheritance Protected Inheritance Private Inheritance parent parent: public child parent: protected child parent: private child public: x public: x protected: x private: x protected: x protected: x protected: x private: x private: x Inaccessible Inaccessible Inaccessible

PUBLIC INHERITANCE class parent public: int x; protected: int y; private: int z; ; class child: public parent child() // inherit publicly x = 1; // ALLOWED: x is public y = 2; // ALLOWED: y is protected z = 3; // NO! COMPILE ERROR! z is private! ; int main() child obj; obj.x = 1; // ALLOWED: anybody can access public members obj.y = 2; // NOT OK: can not access protected members from outside obj.z = 3; // NOT OK: can not access private members from outside

PROTECTED INHERITANCE class parent public: int x; protected: int y; private: int z; ; class child: protected parent child() // inherit protectedly x = 1; // ALLOWED: x is protected y = 2; // ALLOWED: y is protected z = 3; // NO! COMPILE ERROR! z is private! ; int main() child obj; obj.x = 1; // NOT OK: can not access protected members from outside obj.y = 2; // NOT OK: can not access protected members from outside obj.z = 3; // NOT OK: can not access private members from outside

PRIVATE INHERITANCE class parent public: int x; protected: int y; private: int z; ; class child: private parent child() // inherit privately x = 1; // NO! COMPILE ERROR! x is private! y = 2; // NO! COMPILE ERROR! y is private! z = 3; // NO! COMPILE ERROR! z is private! ; int main() child obj; obj.x = 1; // NOT OK: can not access private members from outside obj.y = 2; // NOT OK: can not access private members from outside obj.z = 3; // NOT OK: can not access private members from outside

INHERITANCE VS COMPOSITION Inheritance: is-a relationship child class is derived from parent (base) class student is a person pigeon is a bird Aggregation (composition): has-a relationship one class contains an instance of another class person has a date of birth pigeon has a wing (two of them, in fact) Student Person Date of Birth

COMPOSITION/AGGREGATION Why is composition useful? Decouple for reusability Pigeon is not the only thing with wings Our models become more modular

CONSTRUCTOR INITIALIZATION LISTS What you re used to (explicit assignment): persontype::persontype(string first, string last) firstname = first; lastname = last; Another way to do it (implicit assignment): persontype::persontype(string first, string last) : firstname(first), lastname(last)

COMPOSITION: INITIALIZING INNER OBJECTS Many games and simulations have creatures or objects that move around a board, map, or screen The one thing that all of these creatures/objects have in common is a location (point in 2D space) Composition: creature has a location Creature -name: string -location: Point2D -Creature() +Creature(string n, int x, int y) +Creature(string n, Point2D p) +moveto(int x, int y): void +print() const: void -coordx: int -coordy: int Point2D +Point2D() +Point2D(int x, int y) +setpoint(int x, int y): void +getx() const: int +gety() const: int

COMPOSITION: POINT2D HEADER #ifndef POINT2D_H #define POINT2D_H class Point2D private: int coordx; int coordy; public: Point2D(int x = 0, int y = 0); void setpoint(int x, int y); int getx() const; int gety() const; ; #endif

COMPOSITION: POINT2D IMPLEMENTATION #include Point2D.h Point2D::Point2D(int x, int y) : coordx(x), coordy(y) // Constructor with default parameters // Notice the constructor initialisation list! void Point2D::setPoint(int x, int y) coordx = x; coordy = y; int Point2D::getX() const return coordx; int Point2D::getY() const return coordy;

COMPOSITION: CREATURE HEADER #ifndef CREATURE_H #define CREATURE_H #include <string> #include <iostream> #include "Point2D.h" // Include Point2D for composition class Creature private: string name; Point2D location; Creature(); public: ; #endif Creature(string n, int x, int y); void moveto(int x, int y); void print() const; // Composition: a Point2D object is used!

COMPOSITION: CREATURE IMPLEMENTATION #include Creature.h Creature::Creature() // Empty default constructor Creature::Creature(string n, int x, int y) : name(n), location(x, y) // Constructor with // initialisation list void Creature::moveTo(int x, int y) location.setpoint(x, y); void Creature::print() const cout << name << " is at " << location.getx() Passing parameters to the variable invokes Point2D constructor << "," << location.gety() << " location" << endl;

COMPOSITION: MAIN FUNCTION #include Creature.h using namespace std; int main() string cname; // declare input variables int x = 0, y = 0; cout << "Enter a name for your creature: "; cin >> cname; // get input from user cout << " Enter X position: "; cin >> x; cout << " Enter Y position: "; cin >> y; Creature beast(cname, x, y); // create the beast beast.print(); // print to screen return 0; // exit! Bobby is at 3,4 location

QUESTION TIME Next lecture: Object-Oriented Design Object-Oriented Programming A thorough example