Professional Documents
Culture Documents
Venezia Palazzo Ducale
Venezia Palazzo Ducale
Venezia
Palazzo Ducale Об'єктно-орієнтоване
програмування
Частина 1. Об'єктне програмування.
• class Date {
• public:
• Date (unsigned int d =0, int m=0, int y=0);
• Date (const string& USdate); // “yyyy/mm/dd”
• Date (const Date&);
• ~Date();
• private:
• unsigned int _day, _month, _year;
• void fillDate
(unsigned int d, unsigned int m, unsigned int y);
• };
• class Date
• {
• public:
• //Відкрита частина видна усім
• Date (unsigned int d=0, unsigned int m=0, unsigned int y=0);
• Date (string USdate); // “yyyy/mm/dd”
• Date (const Date&);
• ~Date();
• private:
• //Закрита частина, недоступна зовні
• unsigned int _day, _month, _year;
• void fillDate (unsigned int d, unsigned int m, unsigned int y);
• };
• #include “Date.h”
• int main()
• {
• Date today(22,1,2007);
• // Як виділити пам’ять під today, не знаючи
• // типів кожного з атрибутів?
• cout<<today<<endl;
• return 0;
• }
• #include "Person.h"
• class Card {
• private:
• Person _person;
• public:
• Card(const Person& person) :
• _person(person) {}
• };
• #include "PimplCard.h"
• int main()
• {
• PimplCard pimplcard;
• cout << sizeof(pimplcard) << endl;
• return 0;
• }
public:
• unsigned int _day, _month, _year;
стануть відкритими.
• myDay._day = 32;
• myDay._month = 13;
• myDay._year = 3333;
Метод, наприклад,
• fillDate; setDay(5); month();
не можна викликати без об'єкту.
Метод завжди знає свій об'єкт, до якого його
застосовано. Він звертається до свого об'єкту неявно
(_day, _month, _year ) або через указник
this (this->_day, this->fillDate(d, m, y); )
• void Date::setDefault ()
• {
• struct tm * today =new tm;
• time_t timer;
• time( &timer );
• today = gmtime(&timer);
• defaultDate._day = today->tm_mday;
• defaultDate._month = ++(today->tm_mon);
• defaultDate._year = today->tm_year+=1900;
• }
• _day = d? d: defaultDate._day;
• _month=m? m: defaultDate._month;
• _year= y? y: defaultDate._year;
Платон і Арістотель
© 2006-2021 Бублик В.В. ООП-1. Об'єктне програмування. Права 26
доступу (58)
Порушення прав доступу
ant
• class Employee
• {
• friend class Employer;
• private:
• const Person & _who;
• const Position & _what;
• Employee(const Person & who, const Position & what);
• ~Employee();
• public:
• const Person& who() const;
• const Position& what() const;
• };
© 2006-2021 Бублик В.В. ООП-1. Об'єктне програмування. Права 30
доступу (58)
Працедавець.1
• class Employer
• {
• friend void Accountant::payroll();
• private:
• Accountant * _accountant;
• unsigned int _volume;
• struct Staff; //Hidden implementation
• Staff * _office; // *** pimpl
• public:
• Employer(unsigned int volume);
• ~Employer();
• void hire
• (const Person &, const Position &);
• void fire(unsigned int);
• void pay();
• };
• class Position
• {
• friend void Accountant::payroll(const Employee&);
• private:
• const unsigned int _len;
• string _name;
• unsigned int _salary;
• unsigned int getSalary() const;
• public:
• string getPositionName() const;
• Position(unsigned int, char [], unsigned int);
• ~Position();
• };
• class Person
• {
• private:
• const unsigned int _len;
• string _name;
• public:
• string getName() const;
• Person(unsigned int, char []);
• ~Person();
• };
• class Accountant
• {
• private:
• void payroll (const Employee &);
• const Employer & _myEmployer;
• public:
• Accountant (Employer & employer):
_myEmployer(employer){};
• ~Accountant ()
• void payroll();
• };
• class Point
• {
• friend Point operator+(const Point&, const Point&);
• double _x;
• double _y;
• public:
• Point (double x=0, double y=0);
• Point (const Point &);
• ~Point();
• };
• class Point
• {
• friend Point operator+(const Point&, const Point&);
• double _x;
• double _y;
• public:
• Point (double x=0, double y=0);
• Point (const Point &);
• ~Point();
• };
• const Point operator+(const Point&, const Point&);
© 2006-2021 Бублик В.В. ООП-1. Об'єктне програмування. Права 39
доступу (58)
Можливість вибору: метод vs утиліта
• Перша крайність
• class Date {
• friend Date& operator+=(Date& date, unsigned int dn);
• public:
• Date(unsigned int d =0,unsigned int m=0,
unsigned int y=0);
• Date (const string& USdate); // “yyyy/mm/dd”
• Date (const Date&);
• ~Date();
• private:
• unsigned int _day, _month, _year;
• void fillDate(unsigned int d,unsigned int m,
unsigned int y);
• };
• І ти, Брут!
• // Файл math.cppm
• namespace math
• {
• // Файл math.cpp
• #include <cmath>
• using namespace std;
• module math;
• namespace math {
• long max_check(long i) { return std::sqrt(i)
+1; }
• bool is_prime(long i){
• if (i == 1) return false;
• if (i % 2 == 0) return i == 2;
• for (long j = 3; j < max_check(i); j += 2)
• if (i % j == 0) return false;
• return true;}
• } // namespace math
© 2006-2021 Бублик В.В. ООП-1. Об'єктне програмування. Права 56
доступу (58)
Використання модуля
• // Файл main.cpp
• #include <iostream>
• using namespace std;
• import math;
• int main(){
• using namespace math;
• cout << "982'451'653 is " <<
• (is_prime(982'451'653) ? "" : "not ") <<
• "a prime number.\n";
• // max_check not exported
• return 0;
• }