classes and objects instructor: 小黑. files and streams 1 #include 2 3 4 int main( void ) { 5 char...
Post on 22-Dec-2015
220 views
TRANSCRIPT
Files and StreamsFiles and Streams
http://www.cplusplus.com/
More IOstream library
function you can use:getgetlinereadungetread/writeseekg/seekpflush
Structures V.S. ClassesStructures 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.
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 Classes and Objects
Classes and Objects 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 methodsDefine methodsFollowing 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;};
The constructor is a simple and automatic way to initialize the objects.
int main() {
Employee myemployee;
myemployee.init();
return 0;
}
Purpose of ConstructorsPurpose of Constructors
class Employee {public:
void init() {...}...
private:char _name[10];int _salary;float _height;
};
Use of ConstructorsUse 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;
};
DestructorsDestructors
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 practiceToday’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.)Today’s practice ( contd.)
Ann 50 Molly 46Tom 8 Max 7Sam 20 Rocky 13Ted 5 pepe 1
Today’s practice ( contd.)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.