lecture3 охп удамшил

28
1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Удамшлын хэрэгцээ Тэгш өнцөгт Гурвалжин Тахир шугам class Polygon{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); }; class Triangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); };

Upload: mijiddorj-renchin-ochir

Post on 01-Jul-2015

942 views

Category:

Education


6 download

DESCRIPTION

ОХП классын удамшил

TRANSCRIPT

Page 1: Lecture3 охп удамшил

1

class Rectangle{

private:

int numVertices;

float *xCoord, *yCoord;

public:

void set(float *x, float *y, int nV);

float area();

};

Удамшлын хэрэгцээ

Тэгш өнцөгт Гурвалжин

Тахир шугам

class Polygon{

private:

int numVertices;

float *xCoord, *yCoord;

public:

void set(float *x, float *y, int nV);

};

class Triangle{

private:

int numVertices;

float *xCoord, *yCoord;

public:

void set(float *x, float *y, int nV);

float area();

};

Page 2: Lecture3 охп удамшил

2

Тэгш өнцөгт Гурвалжин

Тахир шугамclass Polygon{

protected:

int numVertices;

float *xCoord, float *yCoord;

public:

void set(float *x, float *y, int nV);

};

class Rectangle : public Polygon{

public:

float area();

};

class Rectangle{

protected:

int numVertices;

float *xCoord, float *yCoord;

public:

void set(float *x, float *y, int nV);

float area();

};

Удамшлын хэрэгцээ

Page 3: Lecture3 охп удамшил

3

Тэгш өнцөгт Гурвалжин

Тахир шугам

class Polygon{

protected:

int numVertices;

float *xCoord, float *yCoord;

public:

void set(float *x, float *y, int nV);

};

class Triangle : public Polygon{

public:

float area();

};

class Triangle{

protected:

int numVertices;

float *xCoord, float *yCoord;

public:

void set(float *x, float *y, int nV);

float area();

};

Удамшлын хэрэгцээ

Page 4: Lecture3 охп удамшил

4

Удамшлын хэрэгцээ

Цэг

Тойрог 3D-цэг

class Point{

protected:

int x, y;

public:

void set (int a, int b);

};

class Circle : public Point{

private:

double r;

};

class 3D-Point: public Point{

private:

int z;

};

xy

xyr

xyz

Page 5: Lecture3 охп удамшил

5

• Анхны классыг нэмэгдүүлэх

• Анхны классыг нарийвчлах

Удамшлын хэрэгцээ

RealNumber

ComplexNumber

ImaginaryNumber

Тэгш өнцөгт Гурвалжин

Тахир шугам Цэг

Тойрог

realimag

real imag

3D-цэг

Page 6: Lecture3 охп удамшил

6

Удамшил?

Удамшил нь өмнө нь тодорхойлогдсон классаас шинэ класс үүсгэх арга

• Классыг өргөтгөх ( шинж чанарууд нэмэх)

• Классыг дахин ашиглах( өөр классд хэрэглэх)

• Классыг засварлах( гишүүн функцийг дахин тодорхойлж )

Үр дүнд нь: Санах ой ба хугацаа хэмнэх, код зохицол сайжирч хөгжүүлэлт хялбарчлагддаг

Page 7: Lecture3 охп удамшил

7

Удамшсан классыг тодорхойлох

• Syntax:

class DerivedClassName : access-level BaseClassName

үүнд– access-level удамшлын хандалтын түвшинг

тодорхойлно• private //default-р• public

• Ямар ч класс эх класс байж болно– Иймд удамшсан класс өөр классын эх байж болно

Page 8: Lecture3 охп удамшил

8

Классын удамшилЦэг

3D-цэг

class Point{

protected:

int x, y;

public:

void set (int a, int b);

};

class 3D-Point : public Point{

private:

double z;

… …

};

class Sphere : public 3D-цэг{

private:

double r;

… …

};

Бөмбөлөг

Цэг нь 3D-цэг-ийн эх класс харин 3D-цэг нь бөмбөлөгийн(Sphere) эх

Page 9: Lecture3 охп удамшил

9

Юу удамших вэ?

• Зарчмын хувьд эх классын бүх гишүүд удамшиж болох хэдий ч– удамших зөвшөөрлөөс хамаарна

Page 10: Lecture3 охп удамшил

10

Удамших гиш нийг хянахүү

• Удамших гишүүнийг хянах – класс зарлах үеийн– удамшлын төрөл тодорхойлох

class Point{

protected: int x, y;

public: void set(int a, int b);

};

class Circle : public Point{

… …

};

Page 11: Lecture3 охп удамшил

11

• Эх классын protected, public гишүүд удамшина, • удамших төрлөөс хамааран гишүүдийн хандалт

өөрчлөгдөнө

Удамших эрх

private protected public

private - - -

protected private protected protected

public private protected public

Удамшлын төрөл

Ги

шүүди

йн

хандал

т

Page 12: Lecture3 охп удамшил

12

class daughter : --------- mother{

private: double dPriv;

public: void mFoo ( );

};

Классын удамшилclass mother{

protected: int mProc;

public: int mPubl;

private: int mPriv;

};

class daughter : --------- mother{

private: double dPriv;

public: void dFoo ( );

};

void daughter :: dFoo ( ){

mPriv = 10; //error

mProc = 20;

};

private/protected/publicint main() {

/*….*/

}

class grandDaughter : public daughter {

private: double gPriv;

public: void gFoo ( );

};

Page 13: Lecture3 охп удамшил

13

Юу удамших вэ?

• Зарчмын хувьд эх классын бүх гишүүд удамшиж болох хэдий ч– удамших зөвшөөрлөөс хамаарна

• Гэхдээ дараах функцууд удамшдаггүй– Байгуулагч ба устгагч– operator=() оператор гишүүн

– найз функц

эдгээр функцууд зөвхөн энэ класст хамаарах тул

Page 14: Lecture3 охп удамшил

14

Удамшлын байгуулагчийн дүрэм

эх классын байгуулагч, устгагч удамшсан классын объект үүсэх бүрт дуудагдана (ажиллана).

class A {

public:

A ( )

{cout<< “A:default”<<endl;}

A (int a)

{cout<<“A:parameter”<<endl;}

};

class B : public A

{

public:

B (int a)

{cout<<“B”<<endl;}

};

B test(1);A:defaultB

output:

Page 15: Lecture3 охп удамшил

15

Удамшлын байгуулагчийн дүрэм Аль аль байгуулагчийг дахин тодорхойлж

болно

class A {

public:

A ( )

{cout<< “A:default”<<endl;}

A (int a)

{cout<<“A:parameter”<<endl;}

};

class C : public A {

public:

C (int a) : A(a)

{cout<<“C”<<endl;}

};

C test(1);A:parameterC

output:

DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass args )

{ DerivedClass constructor body }

Page 16: Lecture3 охп удамшил

16

рийн гиш нийг утгатай Өө үүболгох

Point

Circle

class Point{

protected:

int x, y;

public:

void set(int a, int b);

};

class Circle : public Point{

private:

double r;

public:

void set_r(double c);

};

xy

xyr

class Circle{

protected:

int x, y;

private:

double r;

public:

void set(int a, int b);

void set_r(double c);

};

Удамшсан классын байгуулагч рийн гиш нийг утгатай өө үү

болгохоос гадна эхээс ирсэн гиш нийг утгатай болгоно.үү

Page 17: Lecture3 охп удамшил

17

…• Эх классын гишүүн функцийг хүү классад даран

тодорхойлж болно– Эхтэйгээ адил гишүүн функцтэй байж болно.

– Хүү класс өөрийн хувилбараар ажиллана.

class A {

protected:

int x, y;

public:

void print ()

{cout<<“From A”<<endl;}

};

class B : public A {

public:

void print ()

{cout<<“From B”<<endl;}

};

Page 18: Lecture3 охп удамшил

18

class Point{

protected:

int x, y;

public:

void set(int a, int b)

{x=a; y=b;}

void foo ();

void print();

};

class Circle : public Point{

private: double r;

public:

void set (int a, int b, double c) {

Point :: set(a, b); //same name function call

r = c;

}

void print(); };

Хандах арга

Circle C;

C.set(10,10,100); // from class Circle

C.foo (); // from base class Point

C.print(); // from class Circle

Point A;

A.set(30,50); // from base class Point

A.print(); // from base class Point

Page 19: Lecture3 охп удамшил

19

Дээрх б гдийг жишээ үклассад нэгтгэвэл

• Time нь эх класс• ExtTime нь public-р удамшсан

хүү. Хүү класс нь– Эхийн байгуулагчаас бусдыг

удамшуулж чаддаг– Эхийн public ба protected

гишүүдэд хадаж чаддаг– Өөрийн гишүүн өгөгдөлтэй– Өөрийн байгуулагчтай– Гишүүн функцтэй– Эхийн зарим функцийг даран

тодорхойлдогБайхаар зохион байгуул

ExtTime

Time

Page 20: Lecture3 охп удамшил

20

класс Time –г тодорхойлох

class Time{

public :

void Set ( int h, int m, int s ) ;void Increment ( ) ;void Write ( ) const ;Time ( int initH, int initM, int initS ) ; // constructor Time ( ) ; // default constructor

protected :

int hrs ; int mins ; int secs ;

} ;

// SPECIFICATION FILE ( time.h)

Page 21: Lecture3 охп удамшил

21

Классын интерфэйс диаграм

Protected data:

hrs

mins

secs

Set

Increment

Write

Time

Time

Time класс

Page 22: Lecture3 охп удамшил

22

Удамшсан класс ExtTime // SPECIFICATION FILE ( exttime.h)

#include “time.h”

enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ;

class ExtTime : public Time // Time is the base class and use public inheritance

{ public :

void Set ( int h, int m, int s, ZoneType timeZone ) ;void Write ( ) const; //overridden

ExtTime (int initH, int initM, int initS, ZoneType initZone ) ; ExtTime (); // default constructor

private :ZoneType zone ; // added data member

} ;

Page 23: Lecture3 охп удамшил

23

Классын интерфэйс диаграм

Protected data:

hrs

mins

secs

ExtTime класс

Set

Increment

Write

Time

Time

Set

Increment

Write

ExtTime

ExtTime

Private data:zone

Page 24: Lecture3 охп удамшил

24

ExtTime-г зохион байгуулах

Default Constructor

ExtTime :: ExtTime ( ){

zone = EST ;}

ExtTime –н объект үүсэхэд эх классын default байгуулагч Time() автоматаар дуудагдана.

ExtTime et1;

hrs = 0mins = 0secs = 0zone = EST

et1

Page 25: Lecture3 охп удамшил

25

ExtTime-г зохион байгуулах

Өөр нэг байгуулагч

ExtTime :: ExtTime (int initH, int initM, int initS, ZoneType initZone)

: Time (initH, initM, initS) // constructor initializer

{ zone = initZone ;}

ExtTime *et2 =

new ExtTime(8,30,0,EST);hrs = 8mins = 30secs = 0zone = EST

et2

5000

???

6000

5000

Page 26: Lecture3 охп удамшил

26

ExtTime-г зохион байгуулах

void ExtTime :: Set (int h, int m, int s, ZoneType timeZone)

{

Time :: Set (hours, minutes, seconds); // same name function call

zone = timeZone ;

}

void ExtTime :: Write ( ) const // function overriding

{

string zoneString[8] =

{“EST”, “CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”} ;

Time :: Write ( ) ;

cout <<‘ ‘<<zoneString[zone]<<endl;

}

Page 27: Lecture3 охп удамшил

27

ExtTime-тай ажиллах

#include “exttime.h”… …

int main() {

ExtTime thisTime ( 8, 35, 0, PST ) ; ExtTime thatTime ; // default constructor called

thatTime.Write( ) ; // outputs 00:00:00 EST

thatTime.Set (16, 49, 23, CDT) ; thatTime.Write( ) ; // outputs 16:49:23 CDT

thisTime.Increment ( ) ;thisTime.Increment ( ) ;thisTime.Write ( ) ; // outputs 08:35:02 PST

}

Page 28: Lecture3 охп удамшил

28

Нэгдсэн д гнэлтү

• Удамшил нь байгаа төрлийг нарийвчлан эсвэл өргөтгөн шинэ төрөл тодорхойлох механизм.

• Зарчмын хувьд байгуулагчаас бусад эх классын гишүүдийг хандах эрхийг нь өөрчлөн удамшуулж болно.