Classes and Objects Instructor: 小黑. Files and Streams 1 #include 2 3 4 int main( void ) { 5 char input[ 5 ]; 6 7 FILE *cfPtr; 8 9 if (( cfPtr = fopen(

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C++ Classes & Data Abstraction
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
C++ data types. Structs vs. Classes C++ Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
1 Object-Oriented Programming Using C++ CLASS 27.
Object-Oriented Programming in C++
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Inheritance One of the most powerful features of C++
Object Oriented Programming (OOP) Lecture No. 10.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
11 Introduction to Object Oriented Programming (Continued) Cats.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Introduction to C++ (Extensions to C)
MT262A Review.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Introduction to C++ Systems Programming.
Command Line Arguments
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Chapter 5 Classes.
Lab 7: File Input/Output Graham Northup
COMP 2710 Software Construction File I/O
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
group work #hifiTeam
Files I/O, Streams, I/O Redirection, Reading with fscanf
Basic File I/O and Stream Objects
Today’s Lecture I/O Streams Tools for File I/O
Constructors and Destructors
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 22
CHAPTER 4 File Processing.
CS 144 Advanced C++ Programming February 21 Class Meeting
C++ data types.
Presentation transcript:

Classes and Objects Instructor: 小黑

Files and Streams 1 #include int main( void ) { 5 char input[ 5 ]; 6 7 FILE *cfPtr; 8 9 if (( cfPtr = fopen( “a.txt", “r“ )) == NULL ) { 10 printf( "File could not be opened\n" ); 11 } 12 while ( !feof( stdin ) ) { 13 fscanf( cfPtr, "%s“, input); 14 } return 0; 17 } in C++, we can do like this: in C : 1 #include 2 using namespace std; 3 4 int main( void ) { 5 char input[ 5 ]; 6 7 fstream fin ( "a.txt”, fstream::in); 8 9 if ( fin.is_open() == false ) { 10 printf( "File could not be opened\n" ); 11 } 12 while ( fin.good() ) { 13 fin >> input; 14 } return 0; 17 }

Files and Streams in C : in C++, we can do like this: 1 #include int main( void ) { 5 char input[5] = {"test"}; 6 7 FILE *cfPtr; 8 9 if (( cfPtr = fopen( "a.txt", "w" )) == NULL ) { 10 printf( "File could not be opened\n" ); 11 } fprintf( cfPtr, "%s\n", input); return 0; 17 } 1 #include 2 using namespace std; 3 4 int main( void ) { 5 char input[5] = {"test"}; 6 7 fstream fout ( "a.txt", fstream::out); 8 9 if ( fout.is_open() == false ) { 10 printf( "File could not be opened\n" ); 11 } fout << input << endl; return 0; 17 }

Files and Streams More IOstream library function you can use: get getline read unget read/write seekg/seekp flush

Structures V.S. Classes The class is a foundation of OOP. It is very similar to the expanded structures that group related data and functions together. Struct Employee { char name[10]; int salary; float height; }; class Employee { char name[10]; int salary; float height; };

C++ provides three ways of accessing class members : Public: can be accessed by members of its class as well as members of any other class and non-member functions, including main() Private: can only be accessed by other members of the same class. Protected: when dealing with inheritance class Employee { public: Employee() {} Employee(char *name) {} ~Employee() {} char * getName() {}... private: char _name[10]; int _salary; float _height; }; Classes and Objects

When an object of the class is declared, the constructor is automatically called. Employee em1; Employee em2(“John”); class Employee { public: Employee() {} Employee(char *name) {} ~Employee() {} char * getName() {}... private: char _name[10]; int _salary; float _height; };

Define methods Following is a sample class: class XYZCoord { public: XYZCoord( int x, int y, int z) { _x = x, _y=y, _z=z; } int getX() { return _x; } void output() { cout << “( ” << _x << “,” << _y << “,” << _z << “ )”<< endl; } private: int _x, _y, _z; }; Define with a header file: class XYZCoord { public: XYZCoord( int x, int y, int z); int getX(); void output(); private: int _x, _y, _z; }; XYZCoord::XYZCoord(int x, int y, int z) { _x = x, _y=y, _z=z; } int XYZCoord::getX() { return _x; } void XYZCoord::output() { cout << “( ” << _x << “,” << _y << “,” << _z << “ )”<< endl; }

The constructor is a simple and automatic way to initialize the objects. int main() { Employee myemployee; myemployee.init(); return 0; } Purpose of Constructors class Employee { public: void init() {...}... private: char _name[10]; int _salary; float _height; };

Use of Constructors We can define a default constructor separately or by providing default arguments for all parameters. class Employee { public: Employee(); Employee(char *name = “Blackie”, int salary = 100, float height = 170.5); … private: char _name[10]; int _salary; float _height; };

Destructors There can be only have one Destructor. Destructor has no parameter and no return type. It’s usually used to release resource. class Player { public: Player( ) { char *name = new char[strlen(name)+1]; strcpy ( _name, name); } ~Player() { delete [] _name; } private: char *_name; }

Today’s practice  Define a class Pet  Data member: _name, _level;  Default values of _name and _level are “Lucky” and 1, respectively.  User can pass name and level when constructing a Pet object.  Define a class Player  Data member: _name, _level, _pet;  Default values of _name and _level are “player” and 1, respectively.  User can pass player’s name, player’s level, pet’s name and pet’s level when constructing a Player object.

 Create an input.txt file that as follow:  Each line contains four attributes: player’s name, player’s level, pet’s name, pet’s level.  Create an output.txt file that save your data before exit. Today’s practice ( contd.) Ann 50 Molly 46 Tom 8 Max 7 Sam 20 Rocky 13 Ted 5 pepe 1

Today’s practice ( contd.)  Provide a simple UI as follow:  1. Add new player  Input player’s name. ( No need to input level, pet’s name and pet’s level )  2. Show all players  Print all player’s name, level and their pet.  3. Save & Leave  Save all information in output.txt file like input.txt’s format.