User:Ateremetskyi
Name: Anton Teremetskyi
SID: 068937093
E-mail: ateremetskyi@learn.senecac.on.ca
Blog: Anton's Blog
Skype: Teremetskyi_Anton
IRC Nick: ateremetskyi
My Links:
Class notes : 09.02.2012
int(*fptr[3])(int, int){name1, name2, name3};
The better way to validate any attribute is to use functions .
ValidYear = getYear(Year); // Bool function
Check what do functions cin.clear()and others mean .
I like the way the teacher sends the output to the function
sendMsg("The number is", double number); // Something like that
bool validCCV(int ccv){ return ccv >= 100 && ccv <= 999; } // Better example of the function above .
HomeWork - I have to understand how the program the teacher used works. I can find that program by the date in SVN. Then, I have to finish the assignment 1 (I did not send it yet, some errors appear).
The class on Monday is going to be in T2107 lab.
Class notes : 23.02.2012
1. Virtual functions 2. Frames 3. CField
Exception
#include <iostream> using namespace std;
int main(void) { int x = 10;
try { while(x--) { if(x == 4) { throw x; }
cout << " pass" << x << endl ; } } catch(int x) { cout << "got it >> " << x << " << " << endl; }
return 0; }
---
Pretty TXT stream ( WRITE )
FILE Datafile.txt
Hello, I am jack and I am 35 years old.
==
#include <iostream> #include <fstream> using namespace std;
int main(){ ofstream fout("datafile.txt"); fout<<"Hello, I am jack and I am "<<endl<<35<<endl<<"years old.\n"; return 0; }
---------------
int main(){ ifstream fin("datafile.txt"); char str[100]; fin>>str; cout<<str<<endl; // Hello, fin.getline(str, 99, '\n'); cout<<str<<endl; // I am jack and I am str[0] = fin.get(); cout<<str[0]; // 3 cout<<"TheEnd"<<endl; return 0; }
---
Interesting Custom print statement
#include <cstdio> using namespace std;
class Output{ public: Output& print(const char* str){ printf(str); return *this; } Output& print(int a){ printf("%d", a); return *this; } };
int main(){ Output fout; fout.print("Hello, I am jack and I am ").print(35).print(" years old.\n"); return 0; }
OR
#include <cstdio> using namespace std;
class Output{ public: void print(const char* str){ printf(str); } void print(int a){ printf("%d", a); } };
int main(){ Output fout; fout.print("Hello, I am jack and I am "); fout.print(35); fout.print(" years old\n"); return 0; }
---
Read the written in the prev example file
#include <iostream> #include <fstream> #include <cstring> using namespace std;
class Employee{ char _name[15]; char _lastname[30]; int _empno; double _salary; public: Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){ set(name, lastname, empno, salary); } void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){ strcpy(_name, name); strcpy(_lastname, lastname); _empno = empno; _salary = salary; } ostream& print(ostream& OS)const{ return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno <<", Salary: "<<_salary; } virtual ~Employee(){ //print(cout)<<"is dead!!!!"<<endl; } }; ostream& operator<<(ostream& OS, const Employee& E){ return E.print(OS); }
int main(){ Employee E; fstream file("emp.bin",ios::in|ios::binary); for(int i=0;i<5;i++){ file.read((char*)&E, sizeof(Employee)); cout<<E<<endl<<endl; } return 0; }
OR
- include <iostream>
- include <fstream>
- include <cstring>
using namespace std;
class Employee{
char _name[15]; char _lastname[30]; int _empno; double _salary;
public:
Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){ set(name, lastname, empno, salary); } void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){ strcpy(_name, name); strcpy(_lastname, lastname); _empno = empno; _salary = salary; } ostream& print(ostream& OS)const{ return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno <<", Salary: "<<_salary; } virtual ~Employee(){ //print(cout)<<"is dead!!!!"<<endl; }
}; ostream& operator<<(ostream& OS, const Employee& E){
return E.print(OS);
}
int main(){
Employee E; fstream file("emp.bin",ios::in|ios::binary); file.seekg((ios::off_type)0, ios::end); int loc = file.tellg(); while(loc > 0 ){ loc -= sizeof(Employee); file.seekg((ios::pos_type)loc); file.read((char*)&E, sizeof(Employee)); cout<<E<<endl; } return 0;
}
---
Write classes to the file
#include <iostream> #include <fstream> #include <cstring> using namespace std;
class Employee{ char _name[15]; char _lastname[30]; int _empno; double _salary; public: Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){ set(name, lastname, empno, salary); } void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){ strcpy(_name, name); strcpy(_lastname, lastname); _empno = empno; _salary = salary; } ostream& print(ostream& OS)const{ return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno <<", Salary: "<<_salary; } virtual ~Employee(){ //print(cout)<<"is dead!!!!"<<endl; } }; ostream& operator<<(ostream& OS, const Employee& E){ return E.print(OS); }
int main(){ Employee E[5]={ Employee("John", "Doe", 1234,12345.67), Employee("Jack", "Brown", 22345,212345.67), Employee("Bill", "Red", 3234,32345.67), Employee("Louis", "Clark", 41234,42345.67), Employee("Homer", "Simpson", 5234,52345.67) }; fstream file("emp.bin",ios::out|ios::binary); for(int i=0;i<5;i++){ file.write((const char*)&E[i], sizeof(Employee)); } return 0; }