You are on page 1of 5

Class Exercise 6: Static Example, Friend, Inner class Object-Oriented Programming (83-131), 5771

1) Summary for const, static members


Exam.h
class Exam { int grade; MyString name; static int pass; public: Exam(int i_grade, const char* name1) : grade(i_grade), name(name1) {} Exam(const char* name1) : grade(0), name(name1) {} Exam(const Exam& other) : grade(other.grade), name(other.name) {} explicit Exam (int i_grade) : grade(i_grade), name() {} int get_grade() const { return grade; } void set_grade(int i_grade) { grade = i_grade; } const char* get_name () const { return name.str(); } void set_name(const char* i_name) { name.copyFrom(i_name); } void print() const { cout<<"name="<<name.str()<< " grade="<<grade<<" pass="<<pass<<endl; } bool is_fail();

static int get_pass() { return pass; } static bool set_pass(int i_pass);


};

Exam.cpp
int Exam::pass = 55;
bool Exam::is_fail () { if (grade < pass) return true; else return false; } bool Exam::set_pass(int i_pass) { if ((i_pass<0) || (i_pass>100)) return false; pass=i_pass; return true; }

Class Exercise 6: Static Example, Friend, Inner class Object-Oriented Programming (83-131), 5771

MathGrades.h
class MathGrades { int size; Exam** exam_array; public: MathGrades(int pass) :size(0), exam_array(NULL) { Exam::set_pass(pass); } MathGrades(const MathGrades& other); ~MathGrades(); bool add_grade(int i_grade, const char* i_name); int number_failed(); const Exam& get_exam(int i) const {return *exam_array[i];}; void print() const; };

MathGrades.cpp
MathGrades::MathGrades(const MathGrades& other) { size = other.size; if (other.exam_array == NULL) exam_array = NULL; else { exam_array = new Exam* [size]; for (int i = 0; i < size; i++) exam_array[i] = new Exam(*(other.exam_array[i])); } } MathGrades::~MathGrades() { if (size != 0) { for (int i = 0; i < size; i++) delete exam_array[i]; delete [] exam_array; } } bool MathGrades::add_grade(int i_grade, const char* i_name) { if (i_name == NULL) return false; Exam** help = new Exam* [size+1]; // create new array

for (int i = 0; i < size; i++) help[i] = exam_array[i]; // copy old array to new help[size] = new Exam(i_grade, i_name); // create new Exam delete [] exam_array; exam_array = help; size++; return true; } int MathGrades::number_failed() { int i, sum = 0; for (i=0; i<size; i++) if (exam_array[i]->is_fail()) sum++; return sum; }

Class Exercise 6: Static Example, Friend, Inner class Object-Oriented Programming (83-131), 5771

main.cpp
int main () { /*************** casting by constructor ****************************/ Exam e1(32, "dudi"); e1.print(); // e1 = 54; // error casting without the "explicit" // e1 = (Exam)54; // runtime error e1.print();

/*************** const ***************************************/ Exam e2(70,"yossi"), e3(50,"shula"); e2.print(); // print: name=yossi grade=50 pass=55 e3.print(); // print: name=shula grade=70 pass=55 //e2.get_name()[0] = 'g'; cout<<e2.get_name()<<endl; cout<<e2.is_fail()<<endl; // error. try to change const // yossi // 0 (=false)

const Exam e4 (55, "moshe"); e4.print(); // e4.set_grade(7); // error. try to change const object

/*************** static *************************************/ MathGrades math(60); math.add_grade(87, "dudi"); math.add_grade(43, "rina"); math.add_grade(76, "zohar"); math.print(); cout<<math.number_failed()<<endl; // 1 // 3 // 2

e3.set_pass(90);
cout<<math.number_failed()<<endl;

Exam::set_pass(80);
cout<<math.number_failed()<<endl; // math.get_exam(2).set_grade(57); // error. try to change const math.get_exam(2).print(); // print: name=zohar ... math.print(); return 1; }

Class Exercise 6: Static Example, Friend, Inner class Object-Oriented Programming (83-131), 5771

2) Friend functions
main.cpp
class myclass { int num; public: myclass(int x) { num = x; }

friend int isneg(myclass ob);


};

// This is the function declaration, // but not of member-function

int isneg(myclass ob) { return (ob.num < 0) ? 1 : 0; }

int main() { myclass a(-1), b(2); cout << isneg(a) << ' ' << isneg(b); cout << endl; return 0; }

Class Exercise 6: Static Example, Friend, Inner class Object-Oriented Programming (83-131), 5771

3) Inner classes
Outer.h
class Outer { private: int outer_priv; class Inner1 { int inner1_num; public: Inner1() : inner1_num(1) {} int run_inner1(Outer& out) { inner1_num = out.outer_priv; return inner1_num; } }; public: Outer() : outer_priv(0) {}; class Inner2; int run_outer(Inner1& in) { // outer_priv = in.inner1_num; return outer_priv; } int run_outer(Inner2& in) { // outer_priv = Inner2.inner2_num; outer_priv = in.inner2_num; return outer_priv; } class Inner2 { int inner2_num; public: Inner2() : inner2_num(2) {} int run_inner2(Outer& out) { inner2_num = out.outer_priv; return inner2_num; }

// inner class can access outer privates

// outer class cannot access inner privates

// there is no object Inner2 // access by friendship

friend class Outer;


}; };

main.cpp
int main() { Outer out; //Outer::Inner1 in1; Outer::Inner2 in2; //out.Inner2.get_inner2_num(); in2.run_inner2(out); out.run_outer(in2); return 0; }

// cannot access private class // create object of the inner class // there is no object Inner2