מונחה עצמים- תרגול 2 - Classes
מונחה עצמים- תרגול 2 - Classes
מונחה עצמים- תרגול 2 - Classes
Employee.cpp
#include "Employee.h" #include <iostream> void Employee::Construct( const char* pszEmpName, int iSalary) { strncpy(m_szEmpName, pszEmpName, MAX_NAME_SIZE); m_iSalary = iSalary; } void Employee::PrintAttributes() { cout << "Employee Name : " << m_szEmpName << endl; cout << "Employee Salary : " << m_iSalary << endl << endl; }
main.cpp
#include <iostream> #include "Employee.h" int main() { //----------------1 ----------------Employee Object1, Object2; Object1.Construct("Bill Clinton", 34000); Object2.Construct("Bill Gates", 1); cout << "Employee objects data:" << endl; Object1.PrintAttributes(); Object2.PrintAttributes(); //----------------2 ----------------// If we would like to change Gates salary ? (He wants a raise!) // Since we don't have function we could change it directly cout << "Change bill gates salary:" << endl; Object2.m_iSalary = 30000; Object2.PrintAttributes(); // And what if we would like the max. to be 50K ? // but we could do : cout << "Change bill gates salary again:" << endl; Object2.m_iSalary = 60000; Object2.PrintAttributes(); return 0; }
Employee.cpp
Employee.h <assert.h> <iostream.h> <string.h>
public:
// Accessible from any function // members methods void Construct(const char* pszEmpName, int iSalary); void PrintAttributes(); void SetName(const char* pszEmpName); void SetSalary(int iSalary); const char* GetName(); int GetSalary();
void Employee::SetName(const char*szEmpName) { if (strlen(szEmpName) > MAX_NAME_SIZE) cout << "ERROR" << endl; else strcpy(m_szEmpName, szEmpName); } // We limit the max salary void Employee::SetSalary(int iSalary) { if (iSalary > MAX_SALARY_ALLOWED) cout << "ERROR" << endl; else m_iSalary = iSalary; } const char* Employee::GetName() { return m_szEmpName; } int Employee::GetSalary() { return m_iSalary; }
private:
// Accessible only from local members char m_szEmpName[MAX_NAME_SIZE+1]; int m_iSalary; }; #endif
#include "Employee.h" #include <iostream> int main() { //----------------- 1 ----------------//... //----------------- 2 ----------------// Object2.m_iSalary = 60000; <--- Compiler error
cout << "Trying to change salary to 60000:" << endl; Object2.SetSalary(60000); // Print: "ERROR" Object2.PrintAttributes(); // Print: "1" return 0; }
set.h
Point* vector;
int num_points; public: void init() {vector=NULL; num_points=0;} int get_num_points () {return num_points;} void add_point (const Point& p); bool is_empty() {return !(bool)num_points;} void empty_set(); void print();
point.cpp
#include <iostream> #include "Point.h" void Point::copy_to(Point& p) const { p.m_x = m_x; p.m_y = m_y; } bool Point::is_equal(int ax, int ay) { if ((ax == m_x) && (ay == m_y)) return true; return false; } bool Point::is_equal(Point p2) { if ((p2.m_x==m_x) && (p2.m_y==m_y)) return true; return false; } void Point::print() { cout<<"("<<m_x<<","<<m_y<<")"<<endl; }
#endif
set.cpp
#include <iostream> #include "Set.h" void Set::add_point (Point p) { Point* help = new Point[num_points+1]; for (i=0; i<num_points; i++) vector[i].copy_to(help[i]); delete[] vector; vector = help; p.copy_to(vector[num_points]); num_points++; } void Set::empty_set () { delete[] vector; vector = NULL; num_points = 0; } void Set::print() ...
main.cpp
#include <iostream> #include "Point.h" #include "Set.h" int main() { Point p1, p2; p1.set(1,4); p2.set(3,8); Set set1; set1.init(); set1.add_point(p1); set1.add_point(p2); set1.print(); p2.set(2,2); set1.add_point(p2); set1.print();
// important!!!
// print:
(1,4), (3,8)
// print:
// print: