----------------------------------------------- #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> using std::cout; using std::endl; class Complex{ public: Complex( double real, double imaginary ); void addition( const Complex & ); void subtraction( const Complex & ); void multiplication( const Complex & ); void printComplex(); void setComplexNumber( double real, double imaginary ); private: double realPart,imaginaryPart; }; #endif ----------------------------------------------- #include "complex.h" Complex::Complex( double real, double imaginary ){ setComplexNumber( real, imaginary );} void Complex::setComplexNumber( double real, double imaginary ){ realPart = real; imaginaryPart = imaginary;} void Complex::addition( const Complex &a ){ realPart+=a.realPart; imaginaryPart+=a.imaginaryPart;} void Complex::subtraction( const Complex &s ) { realPart-=s.realPart; imaginaryPart-=s.imaginaryPart;} void Complex::multiplication( const Complex &s ) { double temp; temp=realPart; realPart=realPart*s.realPart - imaginaryPart*s.imaginaryPart; imaginaryPart=temp*s.imaginaryPart + imaginaryPart*s.realPart;} void Complex::printComplex( ){ cout << '(' << realPart << ", " << imaginaryPart << ')';} ----------------------------------------------- #include "complex.h" int main() { Complex b( 1, 7 ), c( 9, 2 ); b.printComplex(); cout << " + "; c.printComplex(); cout << " = "; b.addition( c ); b.printComplex(); cout << '\n'; b.setComplexNumber( 10, 1 ); c.setComplexNumber( 11, 5 ); b.printComplex(); cout << " - "; c.printComplex(); cout << " = "; b.subtraction( c ); b.printComplex(); cout << endl; b.setComplexNumber( 19, 15 ); c.setComplexNumber( 13, 8 ); b.printComplex(); cout << " * "; c.printComplex(); cout << " = "; b.multiplication( c ); b.printComplex(); cout << endl; return 0; }2 (25 Pts) Create a class Circle, which has attribute radius with a default value 1, a const attribute PI. It has one constructor and two member functions that calculate perimeter and area of the circle. It has set and get functions for radius. The set function should verify that radius is greater than 0 and less than 50.0.
----------------------------------------------- #ifndef HEADER_H #define HEADER_H class SavingsAccount { public: SavingsAccount( double b ) { savingsBalance = b >= 0 ? b : 0; } void calculateMonthlyInterest(); static void modifyInterestRate( double ); void printBalance() const; private: double savingsBalance; static double annualInterestRate; }; #endif ----------------------------------------------- #include "header.h" #include <iostream> using std::cout; using std::fixed; #include <iomanip> using std::setprecision; // initialize static data member double SavingsAccount::annualInterestRate = 0.0; // calculate monthly interest for this savings account void SavingsAccount::calculateMonthlyInterest() { savingsBalance += savingsBalance * ( annualInterestRate / 12.0 ); } // method for modifying static member variable annualInterestRate void SavingsAccount::modifyInterestRate( double i ) { annualInterestRate = ( i >= 0 && i <= 1.0 ) ? i : 0.03; } // prints balance of the savings account void SavingsAccount::printBalance() const { cout << fixed << '$' << setprecision( 2 ) << savingsBalance << fixed; } ----------------------------------------------- #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setw; #include "header.h" int main() { SavingsAccount saver1( 2000.0 ), saver2( 3000.0 ); SavingsAccount::modifyInterestRate( .03 ); cout << "\nOutput monthly balances for one year at .03" << "\nBalances: Saver 1 "; saver1.printBalance(); cout << "\tSaver 2 "; saver2.printBalance(); for ( int month = 1; month <= 12; ++month ) { saver1.calculateMonthlyInterest(); saver2.calculateMonthlyInterest(); cout << "\nMonth" << setw( 3 ) << month << ": Saver 1 "; saver1.printBalance(); cout << "\tSaver 2 "; saver2.printBalance(); } SavingsAccount::modifyInterestRate( .04 ); saver1.calculateMonthlyInterest(); saver2.calculateMonthlyInterest(); cout << "\nAfter setting interest rate to .04" << "\nBalances: Saver 1 "; saver1.printBalance(); cout << "\tSaver 2 "; saver2.printBalance(); cout << endl; return 0; }4 (25 Pts) Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form
---------------------------------------------- #ifndef HEADER_H #define HEADER_H #include <iostream> using std::ostream; using std::istream; class Complex { friend ostream &operator<<( ostream &, const Complex & ); friend istream &operator>>( istream &, Complex & ); public: Complex( double = 0.0, double = 0.0 ); // constructor Complex operator*( const Complex& ) const; // multiplication private: double real; // real part double imaginary; // imaginary part }; #endif ---------------------------------------------- #include "header.h" #include <iostream> using std::ostream; using std::istream; // Constructor Complex::Complex( double r, double i ) { real = r; imaginary = i; } // end Complex constructor // Overloaded multiplication operator Complex Complex::operator*( const Complex &operand2 ) const { Complex times; times.real = real * operand2.real + imaginary * operand2.imaginary; times.imaginary = real * operand2.imaginary + imaginary * operand2.real; return times; } // end function operator* ostream& operator<<( ostream &output, const Complex &complex ) { output << complex.real << " + " << complex.imaginary << 'i'; return output; } // end function operator<< istream& operator>>( istream &input, Complex &complex ) { input >> complex.real; input.ignore( 3 ); // skip spaces and + input >> complex.imaginary; input.ignore( 2 ); return input; } // end function operator>> ---------------------------------------------- #include <iostream> using std::cout; using std::cin; #include "header.h" int main() { Complex x, y( 4.3, 8.2 ), z( 3.3, 1.1 ), k; cout << "Enter a complex number in the form: a + bi\n? "; cin >> k; cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\nk: " << k << '\n'; x = y * z; cout << "\nx = y * z:\n" << x << " = " << y << " * " << z << "\n\n"; return 0; }