Advertisement
wintest

ООП: Комплексни числа

Apr 9th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. //Header file
  2. #pragma once
  3. #ifndef _CCOMPLEX_H
  4. #define _CCOMPLEX_H
  5.  
  6. class CComplex {
  7. private :
  8.     double realPart;
  9.     double imgPart;
  10.  
  11. public:
  12.     CComplex();
  13.     CComplex(double, double);
  14.     //copy constructor
  15.     CComplex(const CComplex & );
  16.     //изивиква се винаги когато в един и същи ред се инициализира променлива И се задава стойност
  17.     //CComplex b=a //copy
  18.     // CComplex b;  b=a   викаме равно!!!
  19.  
  20.     ~CComplex();
  21.  
  22.     int setRealPart(double);
  23.     int setImgPart(double);
  24.  
  25.     double getRealPart()const;
  26.     double getImgPart() const;
  27.  
  28.     int print() const;
  29.     int read();
  30.  
  31.    
  32.     //подаваме само 1 параметър, защото имаме вече един имплицитен парметър - this.
  33.     CComplex multiply(const CComplex &) const;
  34.     CComplex divide(const CComplex &) const;
  35.  
  36.     friend CComplex add(const CComplex, const CComplex);//външна функция - затова подаваме 2 параметъра, нямам наличние на имплицитен параметър
  37.    
  38. };
  39.  
  40. //CComplex add(const CComplex, const CComplex);
  41. CComplex sub(const CComplex, const CComplex);
  42.  
  43. #endif
  44.  
  45.  
  46. //CComplex cpp
  47. #include "CComplex.h"
  48. #include <iostream>
  49.  
  50. CComplex::CComplex(): realPart(0.),imgPart(0.){
  51.     std::cout << "I am the default constructor of ("<<realPart
  52.         <<" + i "<<imgPart<<" )!" << std::endl;
  53. }
  54. //инициализиращ списък само за конструктор!!!
  55. CComplex::CComplex(double realPart, double imgPart)
  56.     : realPart(realPart), imgPart(imgPart) {
  57.     std::cout << "I am the constructor with parameters of (" << realPart
  58.         << " + i " << imgPart << " )!" << std::endl;
  59. }
  60. //copy constructor
  61. CComplex::CComplex(const CComplex & right){
  62.     realPart = right.realPart;
  63.     imgPart = right.imgPart;
  64.     std::cout << "I am the copy constructor of (" << realPart
  65.         << " + i " << imgPart << " )!" << std::endl;
  66. }
  67.  
  68.  
  69. CComplex::~CComplex(){
  70.     std::cout << "I am the destructor of (" << realPart
  71.         << " + i " << imgPart << " )!" << std::endl;
  72. }
  73.  
  74. int CComplex::setRealPart(double realPart){
  75.     this->realPart = realPart;
  76.     return 0;
  77. }
  78. int CComplex::setImgPart(double imgPart){
  79.     this->imgPart = imgPart;
  80.     return 0;
  81. }
  82.  
  83. double CComplex::getRealPart()const{
  84.     return realPart;
  85. }
  86. double CComplex::getImgPart() const{
  87.     return imgPart;
  88. }
  89.  
  90. int CComplex::print() const{
  91.     std::cout <<"( " <<realPart<<" + i"<<imgPart<<" ) "<<std::endl;
  92.     return 0;
  93. }
  94. int CComplex::read(){
  95.     std::cout << "Please enterthe real part!" << std::endl;
  96.     std::cin >> realPart;
  97.     std::cout << "Please enterthe imaginary part!" << std::endl;
  98.     std::cin >> imgPart;
  99.     return 0;
  100. }
  101.  
  102.  
  103. CComplex CComplex::multiply(const CComplex & right) const{
  104.     return CComplex(realPart*right.realPart - imgPart*right.imgPart,
  105.         imgPart*right.realPart + realPart*right.imgPart);
  106.     //създавам анонимен обект, когато няма да го използвам повече
  107. }
  108. CComplex CComplex::divide(const CComplex & right) const{
  109.     CComplex temp;
  110.     double denom = right.realPart*right.realPart + right.imgPart*right.imgPart;
  111.     temp = multiply(CComplex(right.realPart, -right.imgPart));
  112.  
  113.     return CComplex(temp.realPart / denom, temp.imgPart / denom);
  114. }
  115.  
  116. CComplex add(const CComplex left, const CComplex right){
  117.     return CComplex(left.realPart + right.realPart, left.imgPart + right.imgPart);
  118.  
  119. }
  120. CComplex sub(const CComplex left, const CComplex right){
  121.     return CComplex(left.getRealPart() - right.getRealPart(), left.getImgPart() - right.getImgPart());
  122. }
  123.  
  124. //Main.cpp
  125. #include<iostream>
  126. #include"CComplex.h"
  127. using namespace std;
  128.  
  129. int main() {
  130.  
  131.     CComplex a(1, 3), b(4, 3);
  132.    
  133.  
  134.     cout << "a + b = ";
  135.     add(a, b).print();
  136.     {
  137.         CComplex c = b;
  138.     cout << "a * c = ";
  139.     a.multiply(c).print();
  140.     }
  141.  
  142.     cout << "a - b = ";
  143.     a.divide(b).print();
  144.  
  145.     cout << "a / b = ";
  146.     sub(a, b).print();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement