ממיבחנים c שאלות ++

22
םםםםםםםםC םםםםם++

Upload: arien

Post on 21-Mar-2016

93 views

Category:

Documents


2 download

DESCRIPTION

ממיבחנים C שאלות ++. תכנות מונחה עצמים ו C++ (35 נקודות). עליכם לממש מחלקות גנריות עבור " מערכים בטוחים ". מערך בטוח הוא מערך המכיל מידע על אורכו, המאפשר הגנה מפני גלישה בשימוש. הגנריות מתבטאת בעובדה שהמימוש מאפשר ליצור מערכים שונים עבור סוגי עצמים שונים. למשל, הפקודה - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ממיבחנים C  שאלות ++

+ שאלות Cממיבחנים +

Page 2: ממיבחנים C  שאלות ++

נקודותC++( 35)תכנות מונחה עצמים ו ". מערך בטוח הוא מערך המכיל מערכים בטוחיםמערכים בטוחיםעליכם לממש מחלקות גנריות עבור "

מידע על אורכו, המאפשר הגנה מפני גלישה בשימוש. הגנריות מתבטאת בעובדה שהמימוש מאפשר ליצור מערכים שונים עבור סוגי עצמים שונים. למשל, הפקודה

array<double> vec)12(array<double> vec)12( instance. כדי למנוע שכפול קוד ע"י הקומפיילר )לכל 12 בגודל doubleתיצור מערך של

class arrayBaseclass arrayBase(, יש לאסוף את החלקים המשותפים במחלקת בסיס templateשל ה- ואח"כ לבצע הורשה:

template <class T> class array: public arrayBasetemplate <class T> class array: public arrayBase}...{ }...{ יש לממש מחלקות כדי שהתוכנית למטה תתבצע כפי שנדרש. שימו לב: בראש הקוד הושמטו שמות המחלקות; עליכם להשלים את הקוד. מומלץ לקרוא את כל הקוד לפני

פתרון השאלה. נקודות(: הגדרת המחלקות15סעיף א )

עם מתודות סבירות לשימוש קל ונוח במערכים T4, ו-1T, 2T, 3Tהגדירו את המחלקות )כולל קלט/פלט(. שימו לב כי יש להגדיר את כל שדות הנתונים ולהצהיר על כל

הפונקציות הנדרשות. אין צורך לממש שום פונקציה. הגדירו גם את המחלקה לטיפול בחריגות. נקודות(: מימוש )חלק מהפונקציות של( המחלקות20סעיף ב )

אין - (constructorsממשו את הפונקציות הבאות בכל מחלקה בה הן מופיעות: בנאים )(, >>operator(, אופרטור פלט )destructorsצורך לאפס ערכים לא מאותחלים, מפרקים )

(. טפלו נכון בשגיאות.][operatorופעולת אינדקס )

Page 3: ממיבחנים C  שאלות ++

int main )( } try } T1 a1)12(, a11)10(; // 10 ו12בגודל double מערכים של2הגדרת T2 a2)10(; // 10 בגודל int הגדרת מערך של a2 = a11; // Syntax error a1 = a11; // O.K. a1[5] = a2[4]; // O.K. cout << a1; // הדפסת מערך שלם const T1 ca1)a11(; // הגדרת מערך קבוע עם אתחול ca1 = a11; // Syntax error ca1[2] = a11[3]; // Syntax error a11[3] = ca1[2]; // O.K. double c_array[] = }0.5, -7, 3.14, 0., 3{; // "C הגדרת "מערך T1 a12)c_array, 4(; // "C הגדרת מערך ואתחולו ע"י "מערך T3 a3; // 5בגודל double הגדרת מערך של T4 a4; // 8בגודל double הגדרת מערך של a3[1] = a4[2]; // O.K. a3 = a4; // Syntax error a4 = a3; // Syntax error a1 = a4; // O.K. return 0; { catch )Bad_Index exc( } cerr << exc; // Bad-Index value is ... : פלט {{

typedef .................. T1; typedef .................. T2; typedef .................. T3; typedef .................. T4;......... more code? .........

Page 4: ממיבחנים C  שאלות ++

typedef array<double< T1;typedef array<int< T2;typedef arraySize<double,5< T3;typedef arraySize<double,8< T4;

class Bad_Index { int index; public:

Bad_Index)int i(:index)i({}friend ostream& operator<<)ostream& co,const Bad_Index& b( {

return co << "Bad_Index value is " << b.index << endl; }};

Page 5: ממיבחנים C  שאלות ++

class arrayBase { int size_; protected: bool legal)int index( const{return index<=0 && index<size_;} void size)int sz( {

if )sz<0( size_=0; else size_=sz;}

public: arrayBase)int sz( {size)sz(;} int size)( const {return size_;}};

Page 6: ממיבחנים C  שאלות ++

template <class T< class array: public arrayBase { T* elem_array; void enter_array)T* ar,int sz({

elem_array = new T]sz[; size)sz(; for )int i=0;i<sz;i++(

elem_array]i[ = ar]i[;} public: array)int sz(:arrayBase)sz({elem_array=new T]sz[;}

array)const array& ar(:arrayBase)ar.size)(( { enter_array)ar.elem_array,size)((;}

array)T* ar, int sz(:arrayBase)sz( { enter_array)ar,size)((;}

Page 7: ממיבחנים C  שאלות ++

~array)({delete][ elem_array;}T& operator][)int i({

if )!legal)i(( throw Bad_Index)i(;return elem_array]i[;

}

const T& operator][)int i( const{if )!legal)i(( throw Bad_Index)i(;

return elem_array]i[;}array& operator=)const array& ar({

if )this == &ar( return *this; delete][ elem_array;

enter_array)ar.elem_array,ar.size)((;return *this;

}friend ostream& operator<< )ostream& co, const array<T<& ar(;

friend istream& operator<< )istream& ci, array<T<& ar(;};

Page 8: ממיבחנים C  שאלות ++

template <class T<ostream& operator<< )ostream& out, const array<T<& ar( { for )int i=0;i<ar.size)(;i++( out << ar.elem_array]i[ << ' '; return out << endl;}

template <class T<istream& operator<< )istream& in, array<T<& ar( { for )int i=0;i<ar.size)(;i++(

in << ar.elem_array]i[; return in;}

Page 9: ממיבחנים C  שאלות ++

template <class T,int SZ< class arraySize: public array<T<{

public: arraySize)(:array<T<)SZ({};

};

Page 10: ממיבחנים C  שאלות ++

נק'(15חלק א' )#include <iostream.h>template <class T>class A }public: A)( } cout << "A::A)(" << endl;{ A)const A& a( :i)a.i( } cout << "A::A)A&(" << endl;{private: T i;{;

template <class T>class B }public: B)A<T> aa(: a)aa( } cout << "B::B)A(" << endl;{ B)const B& b(: a)b.a( } cout << "B::B)B&(" << endl;{ A<T> a;{;

class C : public B<int> }public: C)A<int> aa(: B<int>)aa(,a)aa(

} cout << "C::C)A aa(" << endl;{ ~C)(} cout << "C::~C)(" << endl;{ A<int> a;{;

רשמו את הפלט של התוכנית :הבאה

void main)(} cout << "--1--" << endl; A<int> a; cout << "--2--" << endl; A<double> a1; cout << "--3--" << endl; B<int> b)a(; cout << "--4--" << endl; B<int> b1)b(; cout << "--5--" << endl; C c)a(; cout << "--6--" << endl; B<int>& b2 = c; cout << "--7--" << endl;{

Page 11: ממיבחנים C  שאלות ++

פתרון סעיף א':

--1--A::A)(

--5--A::A)A&(A::A)A&(A::A)A&(B::B)A(A::A)A&(C::C)A aa(

--2--A::A)(--3--A::A)A&(A::A)A&(B::B)A(

--4--A::A)A&(B::B)B&(

--7--C::~C)(

--6--

Page 12: ממיבחנים C  שאלות ++

נק'(15חלק ב' ) הגדר מחלקה/מחלקות הנדרשות בקובץ

Array.h על מנת שקטע הקוד הבא יעבור קומפילציה(. שים לב: רק הצהרת )הידור

מימוש הפונקציות. ללאהמחלקה/ות נדרשת - יש להניח שבמימוש המחלקה ישנם מצביעים.

Page 13: ממיבחנים C  שאלות ++

"include "Array.h# נק'(15חלק ב' )#include "iostream.h"class A } public: A)int aa = 0( : a)aa( }{private: int a;{; int main )( } Array<int> *a1 = new Array<int>)3(; // An array with 3 elements of type int Array<double> arr[20]; // An array of 20 Arrays, each one of them // is of 100 elements of type double Array<double> sum)100(; // An Array of 100 elements of type double Array<double> min)100(; // An Array of 100 elements of type double a1[0] = 10; a1[1] = 20; a1[2] = 30; int i; for )i = 0; i < 20; i ++( } cin >> arr[i]; sum += arr[i]; {

cout << “Sum is:” << sum << endl; min = arr[0]; for )i = 1; i < 20; i ++( if )arr[i] < min( min = arr[i]; cout << “Min is: ” << min << endl; if )min == arr[0]( cout <<“The first Array is the minimum” <<endl; const Array<double> c_arr = sum; for )i = 0; i < c_arr.size)( ; i ++( cout <<“Element #” << i << “: “ << c_arr[i] <<endl; delete a1; Array<A> arr_A)7(; Array<A> arr_A2 = arr_A; return )1(;{

Page 14: ממיבחנים C  שאלות ++

פתרון חלק ב'

#ifndef __ARRAY_H_#define __ARRAY_H_template <class T>class Array } public: Array)int size=100(; T operator[])int i( const; T& operator[])int i(; Array& operator+=)const T& elem(; int size)( const; Array)const Array& src(; Array& operator=)const Array& src(; ~Array)(; private: //...{;

Page 15: ממיבחנים C  שאלות ++

פתרון חלק ב'

template <class T>ostream& operator<<)ostream& out, const Array<T>& arr(;

template <class T>istream& operator>>)istream& inp, Array<T>& arr(;

//the next operators may be implemented as membertemplate <class T>bool operator<)const Array<T>& left, const Array<T>& right(;

template <class T>bool operator==)const Array<T>& left, const Array<T>& right(;

#endif //__ARRAY_H_

Page 16: ממיבחנים C  שאלות ++

2005-2006נקודות. חורף )C++ )40 תכנות מונחה עצמים ו-בשאלה זו נעסוק במטריצות בעלות מימדים כלשהם ובפרט במטריצות ריבועיות. בעוד

שפעולות שונותישנן פעולות מסוימות הניתנות ( transposeניתנות לביצוע עם מטריצות בגודל כללי )כמו

להפעלה אךלחישוב הדטרמיננטה שלה ורק על מטריצות ריבועיות. מבין פעולות אלו נתייחס בשאלה

(determinant .): סעיף זה מתייחס לקטע הקוד הבא נקודות(25סעיף א )

const double EPSILON = 0.000001;int main)(}//create two 3*1 matrices )3D vectors( //with entries of type double assigned //with zeros: Matrix<double> m1, m2)3,1(; const Matrix<double> m3=m1;//assign values to m1 from the file “matrix_1.dat”: ifstream inputFile)“matrix_1.dat”(; inputFile >> m1; inputFile.close)(; m2 = m1; m2.getEntry)0,0( = m3.getEntry)1,0(;//SYNTAX ERROR !!! m3.getEntry)1,0( = m2.getEntry)0,0(; Matrix<double> m4)10,3(; inputFile.open)“matrix_4.dat”(; inputFile >> m4; inputFile.close)(;

//create a 3*3 matrix with the multiplication //of m4’s transpose and m4: Matrix<double> m5) m4.transpose)( * m4 (;//return 1*3 matrix Matrix<double> secondRow = m5.getRow)1(; //return 3*1 matrix Matrix<double> firstColumn = m5.getColumn)0(; //create two 3*3 square-matrices. The first equals //to m5, and the second initielied with zeros: SquareMatrix<double> sm6)m5(, sm7)3(; sm7 = sm6.transpose)(; double det6 = sm6.determinant)(; double det7 = sm7.determinant)(; double diff = )det6 > det7(? det6-det7 : det7-det6 ; if)diff>EPSILON( cout << ”something very strange is going on...”;// save the transposed matrix to the specified file: ofstream outputFile)“transposedMatrix.dat”(; outputFile << sm7; outputFile.close)(; return 0;{

Page 17: ממיבחנים C  שאלות ++

באופן מינימאלי כך שהקוד הנ"ל יוכל SquareMatrix ו- Matrixהגדר\י את המחלקות אין להוסיף (. למעט השורה בה מצוין כי חבויה שגיאה ) לעבור קומפילציה

פונקציונאליות מעבר לנדרש עבור קוד זה ואין צורך לממש את המתודות השונות. של typeבכדי שהמחלקה תהיה ניתנת למימוש יש להניח הנחות מסוימות לגבי ה-

י מדוע הנחות אילו נחוצות.\ציין\י הנחות אלו ופרט(. templateהמוזן ל- )entriesה-

Page 18: ממיבחנים C  שאלות ++

פתרון סעיף א'template <class T< class Matrix {protected:

T** mat;int x_dim, y_dim;

public:Matrix)int x=3, int y=1(;Matrix)const Matrix<T<& matrix(;Matrix& operator=)const Matrix<T<& matrix(;const T& getEntry)int x, int y( const;T& getEntry)int x, int y(;Matrix transpose)( const;Matrix operator*)const Matrix<T<& matrix( const;Matrix getRow)int x( const; Matrix getColumn)int y( const; ~Matrix)(;friend operator << )ifstream& cin, Matrix<T<& matrix(;friend operator << )ofstream& cout, Matrix<T<& matrix(;

};template <class T< class SquareMatrix : public Matrix<T< {public:

SquareMatrix)int x=3(;SquareMatrix)const Matrix<T<& matrix(;T& determinant)( const;~SquareMatrix)(;

};

Page 19: ממיבחנים C  שאלות ++

פתרון סעיף א'

של typeבכדי שהמחלקה תהיה ניתנת למימוש יש להניח את ההנחות הבאות על ה-:entriesה-

copy constructor – -על מנת שנוכל לממש את הcopy constructor שלMatrix. על מנת שיהיה ניתן לקלוט >>אופרטור data-מ ifstream. אופרטור השמה – על מנת שיהיה אפשר לממש אופרטור השמה שלMatrix. של ו-+- על מנת שיהיה ניתן לממש את האופרטור * *אופרטורMatrix.-אופרטור +,-,* - לחישוב הdeterminant.default constructor – לאיתחול מערך שלentries-עבור ה costructors. על מנת שנוכל לממש את אופרטור ההדפסה של - >>אופרטורMatrix.

Page 20: ממיבחנים C  שאלות ++

בסעיף זה . של עצמהtransposeסימטרית הינה מטריצה ריבועית השווה ל- מטריצהמעבר למתודות . SymetricMatrixשל השאלה הנך נידרש לממש את המחלקה

הנורשות, למחלקה זו יהיה:המקבל את גודל המטריצה )פרמטר יחיד( ומאתחל אותה ( constructorבנאי )•

(Iכמטריצת היחידה )הבנאי , אינה סימטרית בהכרחM- כלשהי. היות וSquareMatrix – Mבנאי המקבל •

שמו לב לכך שביטוי זה הינו סימטרי בהכרח(. ) MTM יאתחל את ערכי המטריצה ע"י של ה- type במידה ויש צורך בהנחות נוספות לגבי ה- .הגדיר\י וממש\י מחלקה זו

entries.ציין\י הנחות אילו

נקודות(15סעיף ב )

Page 21: ממיבחנים C  שאלות ++

template <class T< class SymetricMatrix : public SquareMatrix<T< {public:

SymetricMatrix )int size(;SymetricMatrix )const SquareMatrix<T<& matrix(;~ SymetricMatrix )(;

};

template <class T<SymetricMatrix <T<:: SymetricMatrix )int size(: SquareMatrix<T<)size({

for )int i=0;i<size; i++({ for )int j=0; j<size;j++({ if)i!=j(getEntry)i,j( = 0; elsegetEntry)i,i( = 1;}

}}

template <class T<SymetricMatrix <T<:: SymetricMatrix )const SquareMatrix<T<& matrix(: SquareMatrix<T<)matrix.transpose)(* matrix({}

פתרון סעיף ב'

Page 22: ממיבחנים C  שאלות ++