Title: - 1 OOP Object Oriented Programming
URL Source: blob://pdf/4dfac7e4-189f-4a8e-aca5-626779399050
Markdown Content:
- 1
##
## OOP
## OBJECT ORIENTED PROGRAMMING
## C++
> 1
!
' .
..
" "
. ,
.
.
,
"
.
( ... ")
>
>
>
,
>
, ,
> 2
# "
>
object orientated programming :
>
,
>
C++ .
>
C++
'.
>
'
.
> 3
#
> 4
>
>
>
:
>
>
...
> "
> 5
##
# int float
:
-attributes
-methods
INT :
:+, -,* '
> 6
# char
# long Student * s2;
s2= new Student;
s2->taz = 44 ;
//
delete s 2;
## -STRUCT
struct Student
{
int taz ;
float avg;
int year;
};
.
.
,
.
> 7
Student s 1;
s1.taz = 324 ;
//
*s2 = s1;
?
.
,
(
)
#
#
# CLASS AND OBJECT
> 8
## "
>
(struct) , -
class
>
(class) :
>
( ) .
>
.
>
:
>
/ (object)
> 9
class Rect
{
public:
int length;
int width;
void printArea ();
};
class ( )
Rect !
(objects)
## -CLASS
> 10
## (OBJECT)
class Rect
{
public:
int length;
int width;
void printArea ( );
};
!
printArea ?
?
> 11
int main()
{
Rect rec 1;
rec 1.length = 17 ;
rec 1.width = 19 ;
rec 1.printArea( );
Rect * rec 3;
rec 3 = new Rect ;
rec 3->length = 17 ;
rec 3->width = 19 ;
rec 3->printArea ( );
}
>
> ( )
>
>
class Rect
{
public:
int length;
int width;
void printArea ( )
{
cout << length * width << endl;
};
};
class Rect
{
public:
int length;
int width;
void printArea () ;
};
void Rect :: printArea ( )
{
cout << length * width << endl;
}
inline function
( , )
::
.
12
2
.
::
:
::
.
.
>
/class
>
/instance / /object
>
/ /attributes /fields / / /data
members
>
/ /methods / / /member functions
> 13
## ?
>
/ .
>
/
:
>
>
( )
>
>
> 14
## ,
>
( )
>
>
-length
>
-print
>
>
, ,
printArea
>
. .
>
printShetach printArea
> 15
##
class ClassName
{
public:
public data members
public member functions
protected:
protected data members
protected member functions
private:
private data members
private member functions
};
:
-public
.
.
:
. .
:
-private
.( ,
)
class private
> 16
17
##
class Rect
{
private:
int length ;
int width ;
public:
void printArea ( ) { cout << length * width << endl; };
int getLength () { return length ; };
void setLength ( int myLength )
{
if ( myLength > 0)
length = myLength ;
else
length = 5; //default value
};
int getWidth () { return width ; };
void setWidth ( int myWidth ) { width = myWidth ; };
};
length
.
private
,
public
length, width
int main()
{
Rect rect 1;
rect 1.length = -4; //compilation error. Length is private!!!
rect 1.setLength( -4);
cout << rect 1.getLength();
}
getter \setter
get set .
, .class Rect
{
int length;
int width;
public:
void printArea ( );
int getLength ();
void setLength (int myLength );
int getWidth ();
void setWidth (int myWidth );
};
class Rect
{
private:
int length;
int width;
public:
void printArea ( );
int getLength ();
void setLength (int myLength );
int getWidth ();
void setWidth (int myWidth );
};
## CLASS STRUCT
struct Rect
{
public:
int length;
int width;
public:
void printArea ( );
int getLength ();
void setLength (int myLength );
int getWidth ();
void setWidth (int myWidth );
};
++ C:
class struct
.
.
18
> class ,
> private
> struct ,
> public
struct Rect
{
int length;
int width;
public:
void printArea ( );
int getLength ();
void setLength (int myLength );
int getWidth ();
void setWidth (int myWidth );
};
#C:
class
struct
.
,
".
=
= , ,
## Rect.h
Class Rect
{
//
};
## Rect.cpp
## main.cpp
int main ()
{
}
#include "Rect.h "
19
#include "Rect.h "
" 3 : .1 ( )
.2
.3
, ,
> 20
## #include <iostream >#include "Rect.h "
(current directory )
(system directory)
## > < " "
> 21
## ?
>
:
>
>
, .
>
:
>
.
>
.
> 22
##
23
// Rational.h
#pragma once
#include <iostream >
using namespace std;
class Rational
{
private :
int numerator;
int denominator;
int gcd ();
public :
void setNumerator (int x);
void setDenominator (int y);
int getNumerator ();
int getDenominator ();
void print();
void multiply( Rational num );
void reduce();
};
//main.cpp
#include "Rational.h"
int main()
{
Rational num 1, num 2;
num 1.setNumerator( 4);
num 1.setDenominator( 8);
cout << "num 1 = " ;
num 1.print();
num 2.setNumerator( 3);
num 2.setDenominator( 4);
cout << "num 2 = " ;
num 2.print();
num 1.reduce();
cout << "After reduce: num 1 = " ;
num 1.print();
num 1.multiply(num 2);
cout << "After multiply num 1 = " ;
num 1.print();
return 0;
}
num 1 = 4/8
num 2 = 3/4
After reduce: num 1 = 1/2
After multiply num 1 = 3/8
24
//Rational.cpp
#include "Rational.h
void Rational :: setNumerator (int x)
{
numerator = x;
}
void Rational :: setDenominator (int y)
{
if (y)
denominator = y;
else
denominator = 1;
}
int Rational :: getNumerator ()
{
return numerator;
}
int Rational :: getDenominator ()
{
return denominator;
}
void Rational ::print()
{
cout << numerator << "/" << denominator << '\n' ;
}
void Rational ::multiply( Rational num )
{
numerator *= num .numerator ;
denominator *= num .denominator ;
}
void Rational ::reduce()
{
int d = gcd ();
numerator /= d;
denominator /= d;
}
int Rational :: gcd ()
{
int x = numerator < denominator ? numerator : denominator;
for (; x >= 1; x --)
if (numerator % x == 0 && denominator % x == 0)
return x;
}
>
, '
>
>
( :notepad )
>
( :visual studio )
>
>
" "build
>
run
> 25
##
- ,
.
( )"compiler
( ')"linker
source code
main. cpp
machine code
main. obj
project. exe
> Bootstarping
>
> main
>
> .
> math. lib
> string. lib 26
( )
"
LINKER
project. exe
file 2.cpp
file 1.cpp
main. cpp
file 3.cpp
file 4.cpp
COMPILER COMPILER COMPILER COMPILER COMPILER
file 2.obj
file 1.obj
main. obj
file 3.obj
file 4.obj
compilation
linking
27 (preprocessor )
( ), .
>
# ;
>
-
## preprocessor directives
##
#define #include #if
#elif #endif
#else
#ifndef
> 28
29
## -PREPROCESSOR
file. cpp
COMPILER
file. obj
PREPROCESSOR
temp. cpp
*. h
*. h
*. h
iostream
(*. obj)
(main. cpp) :
main. cpp
include Student. h
#include
<iostream. h>
?
## ?
project. exe
file 2.cpp
file 1.cpp
file 3.cpp
COMPILER COMPILER COMPILER
file 2.obj
file 1.obj
file 3.obj
LINKER
include header , (*. obj) .
-
.
main. cpp
COMPILER
main. obj
PREPROCESSOR
mainTemp. cpp
#include < iostream.h >
*. h
*. h
#include < iostream.h >
file 4.cpp
COMPILER
file 4.obj
PREPROCESSOR
temp 4.cpp
#include < iostream.h >
*. h
*. h
30
> us
> er
???
# ? ?
pre -processor :
>
1:
>
h,2 :
#ifndef _H
#define _H
>
:
#endif
>
2:
>
h, :
#pragma once
> 32
## Rational. h
#ifndef RATIONAL_H
#define RATIONAL_H
// Rational.h
#include <iostream >
using namespace std;
class Rational
{
//...
};
#endif
> 33
#pragma once
// Rational.h
#include <iostream >
using namespace std;
class Rational
{
//...
};
1 2 #DEFINE
#include <iostream>
#include "A.h "
#define NUM 5
const int COUNT = 10 ;
int main()
{
int arr [NUM ];
int arr 2[COUNT];
//...
int x = COUNT;
int y = NUM ;
}
NUM 5
#DEFINE CONST ?
,
.
DEFINE
.
, .
CONST
. .DATA ENCAPSULATION -
>
-
(hiding ) .
>
? .
DATA ABSTRACTION
>
>
" " ,
>
private
>
, .
>
" (setter ) (getter )
CONTROL ABSTRACTION
>
( ) .
>
( H CPP )
## :
> 35
## 1-
>
Student