Documente Academic
Documente Profesional
Documente Cultură
1) Exceptions
Employee.h
class Employee { public: // default constructor Employee() : m_sName(NULL),m_id(0),m_salary(0) {} int GetSalary() { return m_salary; } long GetId() { return m_id; } char* GetName() { return m_sName; } void void void void SetSalary(int newSalary ); SetId(long newId) {m_id = newId;} SetName(char* newName); SetSalaryByYear(int yearSalary);
Employee.cpp
void Employee::SetSalary( int newSalary ) { if (newSalary <= 0) throw BaseException(1); m_salary = newSalary; } void Employee::SetName( char* newName ) { int len = strlen(newName); if ( len <= 0 || len > 10 ) throw BaseException(3); if (m_sName) delete m_sName; m_sName = new char[len+1]; strcpy(m_sName, newName); } void Employee::SetSalaryByYear( int yearSalary ) {
try
{ SetSalary(yearSalary / 12); }
catch(...)
{ throw BaseException(4); } }
Exceptions.h
class BaseException { int m_err; public: BaseException(int err) : m_err(err) {} void Print() { cout << "BaseException(" << m_err << ")" << endl; } }; void main() { Employee emp;
main.cpp
try
{ emp.SetId( 123456789 ); emp.SetName("Moshe"); emp.SetSalary(-3); }
catch(BaseException& e)
{ cout << "main function 1: "; e.Print(); }
try
{ emp.SetSalaryByYear(0); }
catch(BaseException& e)
{ cout << "main function 2: "; e.Print(); } }
main.cpp
void main() { //////////////////////// try { A a(-1); int n = a.find(); a.foo(); } catch (int e) { cout << "A: " << e << endl; } catch (...) { cout << "B: " << endl; } //////////////////////// try { A a(1); try { a.find(); } catch (...) { cout << "C: " << endl; throw; } } catch (int e) { cout << "D: " << e << endl; } catch (const char* e) { cout << "E: " << e << endl; } catch (...) { cout << "F: " << endl; } //////////////////////// try { A a(1); a.foo(); } catch (int e) { cout << "G: " << e << endl; } catch (...) { cout << "H: " << endl; } }
Output
A: 3 C: E: Cannot find value H:
String.cpp
const
while (pos<m_iLength && m_pStr[pos] != to_find) pos++; if (pos == m_iLength) return -1; else return pos;
const;
:
:
void PrintVarName( {
const
String& var_str)
int pos = var_str.Find('_'); cout << "The short var name is: "; if (pos==-1) cout << var_str.GetStr();
:
else
const -
cout << var_str.GetStr() + pos +1; } : #ifndef _STRING_H #define _STRING_H class String { private: char *m_pStr; int m_iLength; public: String(const char*);
const
{ }
char* String::GetStr()
const
return m_pStr;
main.cpp
void PrintVarName(const String& var_str) {
const
char* GetStr()
const;
int pos = var_str.Find('_'); cout << "The short var name is: "; if (pos==-1)
void Reverse(); // returns the position // of the char or -1 // if not found int Find(char) }; #endif
cout << var_str.GetStr(); else cout << var_str.GetStr() + pos +1; } void main() { String var_name("m_sName"); PrintVarName(var_name); }
const;
const
String *m_pBossName;
.
public: Employee(const String &name, const String *pBossname); void Func(); };
:
void PrintVarName(const String & var_str) { ... }
.String-
. :
void main() { PrintVarName("m_sName"); }
const
const
Const
.Const
.
" "
.Const
Const
3) Static members
// Point.h: interface for the Point class. // class Point { public: Point(); Point(int x,int y); Point(const Point&); ~Point(); void void int int void SetX(int x) {m_x=x;} SetY(int y) {m_y=y;} GetX() {return m_x;} GetY() {return m_y;} Show(); // main.cpp // #include "Point.h" #include <iostream.h> void ByValueFunction(Point p) { cout<<"(2) There are "; cout<<Point::GetInstancesCounter(); cout<<" instances of Point now"<<endl; cout<<"Press any key to continue\n"; cin.get(); } void ByReferenceFunction(Point& p) { cout<<"(4) There are "; cout<<Point::GetInstancesCounter(); cout<<" instances of Point now"<<endl; cout<<"Press any key to continue\n"; cin.get(); int ms_counter; } void main() { Point p1,p2; cout<<"(1) There are "; cout<<Point::GetInstancesCounter(); cout<<" instances of Point now"<<endl; cout<<"Press any key to continue\n"; cin.get(); ByValueFunction(p1); cout<<"(3) There are "; cout<<p2.GetInstancesCounter(); cout<<" instances of Point now"<<endl; cout<<"Press any key to continue\n"; cin.get(); ByReferenceFunction(p1); }
static
static
};
// Point.cpp: implementation of the Point class. // #include "Point.h" #include <iostream.h> int Point::ms_counter = 0; Point::Point() { ++ms_counter; } Point::Point(int x, int y) { m_x=x; m_y=y; ++ms_counter; } Point::Point(const Point& p) { m_x=p.m_x; m_y=p.m_y; ms_counter++; } Point::~Point() { --ms_counter; } void Point::Show() { cout<<"x value is: "<<m_x<<'\t'; cout<<"y value is: "<<m_y<<endl; }
Output:
(1) There are 2 instances of Point now Press any key to continue (2) There are 3 instances of Point now Press any key to continue (3) There are 2 instances of Point now Press any key to continue (4) There are 2 instances of Point now Press any key to continue