Docsity

Data Abstraction and Complex Number ADT in C and C++, Slides of Computer Engineering and Programming

Partial preview of the text

Download Data Abstraction and Complex Number ADT in C and C++ and more Slides Computer Engineering and Programming in PDF only on Docsity!

Computer Programming Techniques

Overview:

1. What is Data Abstraction?

2. Clients and Manufacturers

3. Complex Number ADT Example

4. How Well are ADTs Supported in C?

5. C++

2. Clients and Manufacturers

implementation interface

ADT

manufacturer’s responsibility client client client use

Benefits

 Manufacturer Benefits:

  • easy to modify, maintain
  • profitable
  • reusable

 Client Benefits:

  • simple to use, understand
  • familiar
  • cheap
  • component-based

Declare a complex number

 Interface:

Complex c1, c2, c3;

 Possible Implementation:

struct complex { double real, imag; } typedef struct complex Complex;

Create a complex number

 Interface:

c1 = create_complex(2, 3); /* conceptually, c1 = 2+3i */

Add two complex numbers

 Interface:

c3 = add_complex(c1, c2); /* conceptually, c3 = c1 + c2 */

Implementation:

Complex add_complex(Complex c1, Complex c2); { Complex csum; csum.real = c1.real + c2.real; csum.imag = c1.imag + c2.imag; return csum; }

int main() { Complex c1, c2, c3; c1 = create_complex(2, -3); c2 = create_complex(2, 9); c3 = add_complex(c1, c2); print_complex(c3); return 0; } /* Implementation of Complex functions */ :

How Well are ADTs Supported in C?

 Does C enforce the use of the ADTs interface

and the hiding of its implementation?

 No