מונחה עצמים- תרגול 2 - Classes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Class Exercise 2: Classes

Object-Oriented Programming (83-131), 5771

1) Simple class Employee.h


#ifndef EMPLOYEE_H #define EMPLOYEE_H #define MAX_NAME_SIZE 128 class Employee { // Accessible from any function public: // member attributes char m_szEmpName[MAX_NAME_SIZE]; int m_iSalary; // member methods void Construct( const char *pszEmpName, int iSalary); void }; #endif PrintAttributes();

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

Class Exercise 2: Classes

Object-Oriented Programming (83-131), 5771

2) Class permissions Employee.h


#ifndef EMPLOYEE_H #define EMPLOYEE_H #define MAX_NAME_SIZE #define MAX_SALARY_ALLOWED class Employee { 128 50000 #include #include #include #include ...

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

Class Exercise 2: Classes

Object-Oriented Programming (83-131), 5771

3) Composition (Class within class) point.h


#ifndef POINT_H #define POINT_H class Point { private: int m_x; int m_y; public: int& getX() {return m_x;} int getY() {return m_y;} void setX(int ax) {m_x = ax;} void setY(int ay) {m_y = ay;} void init() {m_x = 0; m_y = 0;} void set(int ax, int ay) {m_x = ax; m_y = ay;} void copy_to(Point& p) const; bool is_equal(int ax, int ay); bool is_equal(Point p); void print(); }; # endif }; #ifndef SET_H #define SET_H #include "Point.h" class Set { private:

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& get_point (int index)


{return vector[index];}

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() ...

Class Exercise 2: Classes

Object-Oriented Programming (83-131), 5771

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:

(1,4), (3,8), (2,2)

set1.get_point(0).setX(5); set1.print(); set1.empty_set(); set1.print(); return 0; }

// By reference (5,4), (3,8), (2,2)

// print:

// important!!! // print: empty

You might also like