49
Основы ООП

Основы ООП

  • Upload
    ban

  • View
    59

  • Download
    0

Embed Size (px)

DESCRIPTION

Основы ООП. Объектно-ориентированное программирование. Парадигма программирования, основанная на представлении предметной области в виде взаимосвязанных абстрактных объектов и их реализаций. Классы и объекты. - PowerPoint PPT Presentation

Citation preview

1

- , , 3 , ++class {// ( )}; ( , -)- (data members) , , .. , , , , ( -), 1: 2:

, - (, C++ Java) , , , : A B C class Point{public:double x, y;};

class Triangle{public:double GetArea();double GetPerimeter();Point GetCenter();

void Move(double dx, double dy);void Scale(double sx, double sy);void Rotate(Point center, double angle);Point p0, p1, p2;};

, - , , class IntStack{public:void Push(int value);int Pop();bool IsEmpty()const;private:// // };. () - , class Plane{public:void TakeOff();void Fly();void Land();private:double m_fuel;};

class MilitaryPlane : public Plane{public:void Attack();private:intm_ammo;}; , - -, - , , class Shape{public:virtual double GetArea()=0;};

class Rectangle : public Shape{public:virtual double GetArea(){return width * height;}private:doublewidth, height;};

class Circle : public Shape{public:virtual double GetArea(){return 3.1415927 * radius * radius;}private:double radius;}; C++ C++ classclass { // }; class Date{int year, month, day;void next();void print();};

// void Date::print(){printf(%d/%d/%d, day, month, year);}

void Date::next(){// ...}

.h, .cpp #include date.h

class Date{public:void Next();void Print();private:int m_day;int m_month;int m_year;};date.cpp

#include date.h

void Date::Next(){// ...}

void Date::Print(){// ...}main.cpp

#include date.h

int main(){Date date1;return 0;} public:private:protected: (public) Public- public- , () Private- , , Protected- , , class Date{public:void Next();void Print();private:int year, month, day;};

// void Date::Print(){printf(%d/%d/%d, day, month, year);}

void Date::Next(){// ...} , thisclass ListItem{public:void Append(ListItem *pItem){pItem->m_pNext = this;m_pPrevious = pItem;m_pNext = NULL;}private:ListItem *m_pNext;ListItem *m_pPrevious;intm_data;}; C++ , ( ) , , , const, ,

class IntArray{public:int GetSize()const{return m_numberOfItems;}void ClearElements(){delete [] m_pData;m_pData = NULL;m_numberOfItems = 0;}private:int *m_pData;int m_numberOfItems;};

void f(IntArray const& array){int i = array.GetSize();// array.ClearElements();// } (mutable) , - ++ mutable , , , class VeryComplexShape{public:VeryComplexShape(){m_areaInitialized = false;}double GetArea()const{if (!m_areaInitialized){// ( )m_areaInitialized = true;}return m_area;}void ModifyShape(...){m_areaInitialized = false;// ...}private:mutable bool m_areaInitialized;mutable double m_area;}; , ( void) ( new) , class Date{public:Date(int day, int month){m_day = day;m_month = month; m_year = GetCurrentYear();}Date(int day, int month, int year){m_day = day;m_month = month; m_year = year;}private:int m_day, m_month, m_year;}; , , , , , class Foo{public:Foo(int i, int j = 0):m_i(i),m_j(j){}private:int m_i, m_j;};

class Bar{public:Bar() :m_foo(3, 5){}Bar(int i, int j):m_foo(i, j){}private:Foom_foo;}; , , .. , ~ () : , delete delete []class MyFile{public:MyFile():m_pFile(NULL){}

~MyFile(){Close();}bool Open(const char *fileName){Close();m_pFile = fopen(fileName, r);return m_pFile != NULL;}

void Close(){if (m_pFile){fclose(m_pFile); m_pFile = NULL;}}private:FILE *m_pFile;}; ( ) C++ , Type(Type const& t); , , #include "stdio.h"

class Foo{public:Foo():m_moo(0){}Foo(Foo const& foo) :m_moo(foo.m_moo){printf("Creating copy of foo\n");}private:intm_moo;};class Bar{public:void Do(){printf("Do\n");}private:Foom_foo;};

void f(Bar b){printf("f()\n");b.Do();}Bar g(){printf("g()\n");Bar b;return b;}

int main(){Bar b0;printf("Call f()\n");f(b0);printf("Call g()\n");Bar b1 = (g());b1.Do();return 0;}OUTPUT:Call f()Creating copy of foof()DoCall g()g()Creating copy of fooDo , , , #include "stdio.h"#include "memory.h"

class IntArray{public:IntArray():m_pData(NULL), m_size(0){}

IntArray(IntArray const& arr):m_pData(new int [arr.m_size]),m_size(arr.m_size){if (m_size != 0){memcpy(m_pData, arr.m_pData, sizeof(int) * m_size);}}

private:int * m_pData;intm_size;}; , , , , , , (private) class CFile{public:// private:CFile(Cfile const&);// };