close
OOP ( Object-Oriented Programming )
1. Encapsulation
2. Inheritance
3. Polymorphism
#1 : Encapsulation
a ) Without Encapsulation
#include "stdafx.h" #include <cmath> using namespace System; int main(array<System::String ^> ^args) { int iPointA_x = 3; int iPointA_y = 5; //------------------------------------------ int iPointB_x = 6; int iPointB_y = 10; //------------------------------------------ double dDistance = pow( (double) pow( (double) ( iPointA_x - iPointB_x ) , 2 ) + pow( (double) ( iPointA_y - iPointB_y ) , 2 ) , 0.5 ); //------------------------------------------ return 0; }
b ) With Encapsulation
#include "stdafx.h" #include <cmath> using namespace System; class CPoint { public: int x; int y; CPoint(int x, int y) { this->x = x; this->y = y; } ~CPoint() {} }; double computeDistance(CPoint* A, CPoint* B) { return pow( (double) pow( (double) ( A->x - B->x ) , 2 ) + pow( (double) ( A->y - B->y ) , 2 ) , 0.5 ); } int main(array<System::String ^> ^args) { CPoint* pA = new CPoint( 3 , 5 ); CPoint* pB = new CPoint( 6 , 10 ); //------------------------------------------ double dDistance = computeDistance( pA , pB ); //------------------------------------------ return 0; }
#2 : Inheritance
a ) Without Inheritance
#include "stdafx.h" #include <cmath> using namespace System; class CPoint { public: int x; int y; CPoint(int x, int y) { this->x = x; this->y = y; } ~CPoint() {} }; class CPoint3D { public: int x; int y; int z; CPoint3D(int x, int y, int z) { this->x = x; this->y = y; this->z = z; } ~CPoint3D() {} }; double computeDistance(CPoint* A, CPoint* B) { return pow( (double) pow( (double) ( A->x - B->x ) , 2 ) + pow( (double) ( A->y - B->y ) , 2 ) , 0.5 ); } double computeDistance3D(CPoint3D* A, CPoint3D* B) { return pow( (double) pow( (double) ( A->x - B->x ) , 2 ) + pow( (double) ( A->y - B->y ) , 2 ) + pow( (double) ( A->z - B->z ) , 2 ) , 0.5 ); } int main(array<System::String ^> ^args) { CPoint* pA = new CPoint( 3 , 5 ); CPoint* pB = new CPoint( 6 , 10 ); //------------------------------------------ double dDistance = computeDistance( pA , pB ); //------------------------------------------ CPoint3D* pA3D = new CPoint3D( 2 , 4 , 6 ); CPoint3D* pB3D = new CPoint3D( 3 , 6 , 9 ); double dDistance3D = computeDistance3D( pA3D , pB3D ); //------------------------------------------ return 0; }
b ) With Inheritance
#include "stdafx.h" #include <cmath> using namespace System; class CPoint { public: int x; int y; CPoint(int x, int y) { this->x = x; this->y = y; } ~CPoint() {} }; class CPoint3D : public CPoint { public: int z; CPoint3D(int x, int y, int z) : CPoint( x , y ) { this->z = z; } ~CPoint3D() {} }; double computeDistance(CPoint* A, CPoint* B) { return pow( (double) pow( (double) ( A->x - B->x ) , 2 ) + pow( (double) ( A->y - B->y ) , 2 ) , 0.5 ); } // OverLoading (Polymorphism) double computeDistance(CPoint3D* A, CPoint3D* B) { return pow( (double) pow( (double) ( A->x - B->x ) , 2 ) + pow( (double) ( A->y - B->y ) , 2 ) + pow( (double) ( A->z - B->z ) , 2 ) , 0.5 ); } int main(array<System::String ^> ^args) { CPoint* pA = new CPoint( 3 , 5 ); CPoint* pB = new CPoint( 6 , 10 ); //------------------------------------------ double dDistance = computeDistance( pA , pB ); //------------------------------------------ CPoint3D* pA3D = new CPoint3D( 2 , 4 , 6 ); CPoint3D* pB3D = new CPoint3D( 3 , 6 , 9 ); double dDistance3D = computeDistance( pA3D , pB3D ); //------------------------------------------ return 0; }
#3 : Polymorphism
a ) Without Polymorphism
#include "stdafx.h" #include <stdlib.h> #include <time.h> #include <cmath> using namespace System; class CPoint { public: int x; int y; CPoint() { this->x = rand()%10; this->y = rand()%10; } ~CPoint() {} }; class CPoint3D : public CPoint { public: int z; CPoint3D() : CPoint() { this->z = rand()%10; } ~CPoint3D() {} }; class CVector2D { public: CPoint* pA; CPoint* pB; double distance; CVector2D() { this->pA = new CPoint(); this->pB = new CPoint(); computeDistance(); } ~CVector2D() {} void computeDistance() { this->distance = pow( (double) pow( (double) ( this->pA->x - this->pB->x ) , 2 ) + pow( (double) ( this->pA->y - this->pB->y ) , 2 ) , 0.5 ); } }; class CVector3D { public: CPoint3D* pA; CPoint3D* pB; double distance; CVector3D() { this->pA = new CPoint3D(); this->pB = new CPoint3D(); computeDistance(); } ~CVector3D() {} void computeDistance() { this->distance = pow( (double) pow( (double) ( this->pA->x -this->pB->x ) , 2 ) + pow( (double) ( this->pA->y - this->pB->y ) , 2 ) + pow( (double) ( this->pA->z - this->pB->z ) , 2 ) , 0.5 ); } }; void swap2D(CVector2D* A, CVector2D* B) { CVector2D Temp = *A; *A = *B; *B = Temp; } // bubble sort void sort2D(CVector2D* arr[10]) { for(int i=10-1;i>=1;i--) for(int j=0;j<i;j++) if( arr[j]->distance > arr[j+1]->distance ) swap2D( arr[j] , arr[j+1] ); } void swap3D(CVector3D* A, CVector3D* B) { CVector3D Temp = *A; *A = *B; *B = Temp; } // bubble sort void sort3D(CVector3D* arr[10]) { for(int i=10-1;i>=1;i--) for(int j=0;j<i;j++) if( arr[j]->distance > arr[j+1]->distance ) swap3D( arr[j] , arr[j+1] ); } int main(array<System::String ^> ^args) { srand ( time(NULL) ); //------------------------------------------ CVector2D* vec2D[10]; CVector3D* vec3D[10]; //------------------------------------------ for(int i=0;i<10;i++) { vec2D[i] = new CVector2D(); vec3D[i] = new CVector3D(); } //------------------------------------------ sort2D( vec2D ); sort3D( vec3D ); //------------------------------------------ return 0; }
b ) With Polymorphism
#include "stdafx.h" #include <stdlib.h> #include <time.h> #include <cmath> using namespace System; class CPoint { public: int x; int y; CPoint() { this->x = rand()%10; this->y = rand()%10; } ~CPoint() {} }; class CPoint3D : public CPoint { public: int z; CPoint3D() : CPoint() { this->z = rand()%10; } ~CPoint3D() {} }; class CVector { public: int id; double distance; // virtual function virtual void computeDistance() {} }; class CVector2D : public CVector { public: CPoint* pA; CPoint* pB; CVector2D(int id) { this->id = id; this->pA = new CPoint(); this->pB = new CPoint(); computeDistance(); } ~CVector2D() {} // OverWriting void computeDistance() { this->distance = pow( (double) pow( (double) ( this->pA->x - this->pB->x ) , 2 ) + pow( (double) ( this->pA->y - this->pB->y ) , 2 ) , 0.5 ); } }; class CVector3D : public CVector { public: CPoint3D* pA; CPoint3D* pB; CVector3D(int id) { this->id = id; this->pA = new CPoint3D(); this->pB = new CPoint3D(); computeDistance(); } ~CVector3D() {} // OverWriting void computeDistance() { this->distance = pow( (double) pow( (double) ( this->pA->x -this->pB->x ) , 2 ) + pow( (double) ( this->pA->y - this->pB->y ) , 2 ) + pow( (double) ( this->pA->z - this->pB->z ) , 2 ) , 0.5 ); } }; void swap(CVector* A, CVector* B) { CVector Temp = *A; *A = *B; *B = Temp; } // bubble sort void sort(CVector* arr[10]) { for(int i=10-1;i>=1;i--) for(int j=0;j<i;j++) if( arr[j]->distance > arr[j+1]->distance ) swap( arr[j] , arr[j+1] ); } int main(array<System::String ^> ^args) { srand ( time(NULL) ); //------------------------------------------ CVector* vec2D[10]; CVector* vec3D[10]; //------------------------------------------ for(int i=0;i<10;i++) { vec2D[i] = new CVector2D( i ); vec3D[i] = new CVector3D( i ); } //------------------------------------------ sort( vec2D ); sort( vec3D ); //------------------------------------------ return 0; }
c ) With Polymorphism ( Template )
#include "stdafx.h" #include <stdlib.h> #include <time.h> #include <cmath> using namespace System; class CPoint { public: int x; int y; CPoint() { this->x = rand()%10; this->y = rand()%10; } ~CPoint() {} }; class CPoint3D : public CPoint { public: int z; CPoint3D() : CPoint() { this->z = rand()%10; } ~CPoint3D() {} }; // abstract class class CVector { public: double distance; // pure virtual function virtual void computeDistance() = 0; }; class CVector2D : public CVector { public: CPoint* pA; CPoint* pB; CVector2D() { this->pA = new CPoint(); this->pB = new CPoint(); computeDistance(); } ~CVector2D() {} // OverWriting void computeDistance() { this->distance = pow( (double) pow( (double) ( this->pA->x - this->pB->x ) , 2 ) + pow( (double) ( this->pA->y - this->pB->y ) , 2 ) , 0.5 ); } }; class CVector3D : public CVector { public: CPoint3D* pA; CPoint3D* pB; CVector3D() { this->pA = new CPoint3D(); this->pB = new CPoint3D(); computeDistance(); } ~CVector3D() {} // OverWriting void computeDistance() { this->distance = pow( (double) pow( (double) ( this->pA->x -this->pB->x ) , 2 ) + pow( (double) ( this->pA->y - this->pB->y ) , 2 ) + pow( (double) ( this->pA->z - this->pB->z ) , 2 ) , 0.5 ); } }; template <class T> void swap(T* A, T* B) { T Temp = *A; *A = *B; *B = Temp; } // bubble sort template <class T> void sort(T* arr[10]) { for(int i=10-1;i>=1;i--) for(int j=0;j<i;j++) if( arr[j]->distance > arr[j+1]->distance ) swap( arr[j] , arr[j+1] ); } int main(array<System::String ^> ^args) { srand ( time(NULL) ); //------------------------------------------ CVector2D* vec2D[10]; CVector3D* vec3D[10]; //------------------------------------------ for(int i=0;i<10;i++) { vec2D[i] = new CVector2D(); vec3D[i] = new CVector3D(); } //------------------------------------------ sort( vec2D ); sort( vec3D ); //------------------------------------------ return 0; }
文章標籤
全站熱搜