Computer Programming Lab 14 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Computer Programming Lab 14

Description:

Subtraction of two Complex numbers ... Write statement to subtract the realPart of s. from the class realPart ... subtract // print complex numbers. void ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 10
Provided by: shyhka
Category:

less

Transcript and Presenter's Notes

Title: Computer Programming Lab 14


1
Computer ProgrammingLab 14
  • Shyh-Kang Jeng
  • Department of Electrical Engineering/
  • Graduate Institute of Communication Engineering
  • National Taiwan University

2
Lab Exercise 13 Complex Numbers (1/2)
  • Create a class called Complex for performing
    arithmetic with complex numbers. Write a driver
    program to test your class. Complex numbers have
    the form
  • where
  • Use float-point variables to represent the
    private data of the class.

3
Lab Exercise 13 Complex Numbers (2/2)
  • Provide a constructor function that enables an
    object of this class to be initialized when it is
    declared. The constructor should contain default
    values in case no initializers are provided.
    Provide public member functions for each of the
    following
  • Addition of two Complex numbers
  • Subtraction of two Complex numbers
  • Printing Complex numbers in the form (a, b) where
    a is the real part and b is the imaginary part

4
Sample Output
(1, 7) (9, 2) (10, 9) (10, 1) (11, 5)
(-1, -4)
5
Template Main.cpp (1/2)
  • // CPLab14\Main.cpp
  • include ltiostreamgt
  • using stdcout
  • using stdendl
  • include "Complex.h"
  • int main()
  • Complex b( 1, 7 ), c( 9, 2 )
  • b.printComplex()
  • cout ltlt " "
  • c.printComplex()
  • cout ltlt " "
  • b.add( c )
  • b.printComplex()
  • cout ltlt '\n'

6
Template Main.cpp (2/2)
  • b.setComplexNumber( 10, 1 )
  • c.setComplexNumber( 11, 5 )
  • b.printComplex()
  • cout ltlt " - "
  • c.printComplex()
  • cout ltlt " "
  • b.subtract( c )
  • b.printComplex()
  • cout ltlt endl
  • return 0
  • // main

7
Template Complex.h
  • // Complex.h
  • ifndef COMPLEX_H
  • define COMPLEX_H
  • / Write class declaration for Complex /
  • endif

8
Template Complex.cpp (1/2)
  • // Complex.cpp
  • include ltiostreamgt
  • using stdcout
  • include "Complex.h"
  • // Constructor
  • ComplexComplex( double real, double imaginary )
  • setComplexNumber( real, imaginary )
  • // constructor
  • // add complex numbers
  • void Complexadd( const Complex a )
  • / Write statement to add the realPart of a to
  • the class realPart /
  • / Write statement to add the imaginaryPart of
  • a to the class imaginaryPart /
  • // add

9
Template Complex.cpp (2/2)
  • // subtract complex numbers
  • void Complexsubtract( const Complex s )
  • / Write statement to subtract the realPart of s
  • from the class realPart /
  • / Write statement to subtract the imaginaryPart
  • of s from the class imaginaryPart /
  • // subtract
  • // print complex numbers
  • void ComplexprintComplex()
  • cout ltlt '(' ltlt realPart ltlt ", " ltlt imaginaryPart
  • ltlt ')'
  • // printComplex
  • // set complex number
  • void ComplexsetComplexNumber( double real,
  • double imaginary )
  • realPart real
  • imaginaryPart imaginary
  • // setComplexNumber
Write a Comment
User Comments (0)
About PowerShow.com