Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Header file
- #pragma once
- #ifndef _CCOMPLEX_H
- #define _CCOMPLEX_H
- class CComplex {
- private :
- double realPart;
- double imgPart;
- public:
- CComplex();
- CComplex(double, double);
- //copy constructor
- CComplex(const CComplex & );
- //изивиква се винаги когато в един и същи ред се инициализира променлива И се задава стойност
- //CComplex b=a //copy
- // CComplex b; b=a викаме равно!!!
- ~CComplex();
- int setRealPart(double);
- int setImgPart(double);
- double getRealPart()const;
- double getImgPart() const;
- int print() const;
- int read();
- //подаваме само 1 параметър, защото имаме вече един имплицитен парметър - this.
- CComplex multiply(const CComplex &) const;
- CComplex divide(const CComplex &) const;
- friend CComplex add(const CComplex, const CComplex);//външна функция - затова подаваме 2 параметъра, нямам наличние на имплицитен параметър
- };
- //CComplex add(const CComplex, const CComplex);
- CComplex sub(const CComplex, const CComplex);
- #endif
- //CComplex cpp
- #include "CComplex.h"
- #include <iostream>
- CComplex::CComplex(): realPart(0.),imgPart(0.){
- std::cout << "I am the default constructor of ("<<realPart
- <<" + i "<<imgPart<<" )!" << std::endl;
- }
- //инициализиращ списък само за конструктор!!!
- CComplex::CComplex(double realPart, double imgPart)
- : realPart(realPart), imgPart(imgPart) {
- std::cout << "I am the constructor with parameters of (" << realPart
- << " + i " << imgPart << " )!" << std::endl;
- }
- //copy constructor
- CComplex::CComplex(const CComplex & right){
- realPart = right.realPart;
- imgPart = right.imgPart;
- std::cout << "I am the copy constructor of (" << realPart
- << " + i " << imgPart << " )!" << std::endl;
- }
- CComplex::~CComplex(){
- std::cout << "I am the destructor of (" << realPart
- << " + i " << imgPart << " )!" << std::endl;
- }
- int CComplex::setRealPart(double realPart){
- this->realPart = realPart;
- return 0;
- }
- int CComplex::setImgPart(double imgPart){
- this->imgPart = imgPart;
- return 0;
- }
- double CComplex::getRealPart()const{
- return realPart;
- }
- double CComplex::getImgPart() const{
- return imgPart;
- }
- int CComplex::print() const{
- std::cout <<"( " <<realPart<<" + i"<<imgPart<<" ) "<<std::endl;
- return 0;
- }
- int CComplex::read(){
- std::cout << "Please enterthe real part!" << std::endl;
- std::cin >> realPart;
- std::cout << "Please enterthe imaginary part!" << std::endl;
- std::cin >> imgPart;
- return 0;
- }
- CComplex CComplex::multiply(const CComplex & right) const{
- return CComplex(realPart*right.realPart - imgPart*right.imgPart,
- imgPart*right.realPart + realPart*right.imgPart);
- //създавам анонимен обект, когато няма да го използвам повече
- }
- CComplex CComplex::divide(const CComplex & right) const{
- CComplex temp;
- double denom = right.realPart*right.realPart + right.imgPart*right.imgPart;
- temp = multiply(CComplex(right.realPart, -right.imgPart));
- return CComplex(temp.realPart / denom, temp.imgPart / denom);
- }
- CComplex add(const CComplex left, const CComplex right){
- return CComplex(left.realPart + right.realPart, left.imgPart + right.imgPart);
- }
- CComplex sub(const CComplex left, const CComplex right){
- return CComplex(left.getRealPart() - right.getRealPart(), left.getImgPart() - right.getImgPart());
- }
- //Main.cpp
- #include<iostream>
- #include"CComplex.h"
- using namespace std;
- int main() {
- CComplex a(1, 3), b(4, 3);
- cout << "a + b = ";
- add(a, b).print();
- {
- CComplex c = b;
- cout << "a * c = ";
- a.multiply(c).print();
- }
- cout << "a - b = ";
- a.divide(b).print();
- cout << "a / b = ";
- sub(a, b).print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement