Download presentation
Presentation is loading. Please wait.
1
Classes and Objects Instructor: 小黑
2
Files and Streams 1 #include 2 3 4 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 } 15 16 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 } 15 16 return 0; 17 }
3
Files and Streams in C : in C++, we can do like this: 1 #include 2 3 4 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 } 12 13 fprintf( cfPtr, "%s\n", input); 14 15 16 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 } 12 13 fout << input << endl; 14 15 16 return 0; 17 }
4
Files and Streams http://www.cplusplus.com/ More IOstream library function you can use: get getline read unget read/write seekg/seekp flush
6
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; };
7
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
8
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; };
9
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; }
10
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; };
11
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; };
12
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; }
14
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.
15
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
16
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.