Sunteți pe pagina 1din 6

Class Exercise 5: Exceptions, Const, Static

Object-Oriented Programming (83-131), 5771

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 ) {

void ShowEmployee(); private: char* m_sName; long m_id; int m_salary; };

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(); } }

Class Exercise 5: Exceptions, Const, Static

Object-Oriented Programming (83-131), 5771

Another exceptions example


A.h
class A { public: A(int n) { if (n <= 0) throw (3); } int& find() { throw ("Cannot find value"); } void foo() { throw (2.5); } };

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:

Class Exercise 5: Exceptions, Const, Static

Object-Oriented Programming (83-131), 5771

2) Const Member Variables and Functions


String.h
#ifndef _STRING_H #define _STRING_H class String { private: char* m_pStr; int m_iLength; public: String(const char*); char* GetStr(); void Reverse(); // returns the position // of the char or -1 // if not found int Find(char) }; } #endif char* String::GetStr() { return m_pStr; } void String::Reverse() { char temp; for (int i=0; i<m_iLength/2; i++) { // Exchange the i and length-i characters temp = m_pStr[i]; m_pStr[i] = m_pStr[m_iLength-i-1]; m_pStr[m_iLength-i-1] = temp; } } #include "String.h" #include <iostream.h> String::String(const char* str) { m_iLength = strlen(str); m_pStr = new char[m_iLength+1]; strcpy(m_pStr,str); } int String::Find(char to_find) { int pos=0;

String.cpp

const

while (pos<m_iLength && m_pStr[pos] != to_find) pos++; if (pos == m_iLength) return -1; else return pos;

const;

main.cpp output: The short var name is: sName


void PrintVarName(String & var_str) { int pos = var_str.Find('_'); cout << "The short var name is: "; if (pos==-1) cout << var_str.GetStr(); else cout << var_str.GetStr() + pos +1; } void main() { String var_name("m_sName"); PrintVarName(var_name); }

Class Exercise 5: Exceptions, Const, Static

Object-Oriented Programming (83-131), 5771

:
:
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;

output: The short var name is: sName

Class Exercise 5: Exceptions, Const, Static

Object-Oriented Programming (83-131), 5771

(1) Const Object Pointer


class Employee { private: String m_sName;

(2) Temporary Objects :


void PrintVarName(String & var_str) { ... }

const

String *m_pBossName;

.
public: Employee(const String &name, const String *pBossname); void Func(); };

.String& void main() { PrintVarName("m_sName"); }

Employee::Employee(const String &name, const String* pBossName) : m_sName(name) { m_pBossName = pBossName; }

:
void PrintVarName(const String & var_str) { ... }

void Employee::Func() { // ... m_pBossName->Reverse(); }

.String-

. :
void main() { PrintVarName("m_sName"); }

void main() { String boss("Parho");

const

const

Employee moshe("Moshe",&boss); moshe.Func(); }

Const

.Const
.

" "

.Const

Const

Class Exercise 5: Exceptions, Const, Static

Object-Oriented Programming (83-131), 5771

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

int GetInstancesCounter() {return ms_counter;} private: int m_x; int m_y;

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

S-ar putea să vă placă și