Advanced Concepts in C++ Programming: Classes, Destructors, and Multi-File Programs

csc 270 survey of programming languages n.w
1 / 22
Embed
Share

Explore advanced C++ programming concepts such as classes, destructors, and multi-file programs. Learn how to effectively organize code, manage memory, and implement complex data structures like dynamic arrays through detailed examples and explanations.

  • C++
  • Programming
  • Classes
  • Destructors
  • Multi-File

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSC 270 Survey of Programming Languages C++ Lecture 4 More on Writing Classes

  2. MultiFile Programs Programs can be divided into separate files: When working working with classes, the basic class definition is placed in a header file (name ending with .h ) and the code for the methods are placed in a C++ source file (name ending with .cpp )

  3. Destructors There are occasions when you need to do some cleanup after using objects. The method that is called automatically to do this is called a destructor. In C++, destructors have the same name as the class with a tilde (~) in front of the class s name, e.g., ~MyClass()

  4. DynArray.h #include using namespace std; typedef class DynArray { public: DynArray(void); DynArray(int numrows, int numcols); ~DynArray(void); int getArrayMember(int i, int j); void setArrayMember(int value, int i, int j); private: IntPtr *array; }; <iostream> int *IntPtr;

  5. DynArray.cpp #include "DynArray.h" DynArray::DynArray(void) { array = new IntPtr[3]; for (int i = 0; i < 3; i++) array[i] = new int[3]; } DynArray::DynArray(int numrows, int numcols) { array = new IntPtr[numrows]; for (int i = 0; i < numrows; i++) array[i] = new int[numcols]; }

  6. DynArray::~DynArray(void) { // Free each individual row for (int i = 0; i < 5; i++) delete [] array[i]; // Free the array of pointers delete [] array; } int { DynArray::getArrayMember(int i, int j) return(array[i][j]); }

  7. void DynArray::setArrayMember(int value, int i, int j) { array[i][j] = value; }

  8. DynArrayDemo.cpp the Main Program #include "DynArray.h" int { main(void) DynArray int for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) cin >> x; da.setArrayMember(x, i, j); } da(4, 4); x; {

  9. for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) cout << "x[" << i << "][" << j << "] = " << da.getArrayMember(i, j) << '\t'; cout << "\n"; } { return(0); }

  10. Overloading Operators Just as methods can be overloaded, so can operators. The general syntax for the header is: DataType operator symbol (opndDataType opnd); Operators that can be overloaded include: Standard arithmetic operators (+-*/%) Relational operators (== != > < >= <=) Logical operators (&& || !) Bitwise operators (& | ^ ~ << >>) Autoincrement and autodecrement (++ -- ) Assignment operators (== += -= etc.)

  11. Complex.h #include <iostream> using namespace std; class Complex public: Complex(void); Complex(int i, int j); Complex(float x, float y); Complex(double x, double y); void read(void); void write(void); {

  12. Complex Complex Complex bool bool operator + (Complex u); operator - (Complex u); operator * (Complex u); operator == (Complex u); operator != (Complex u); private: float real; float imag; };

  13. Complex.cpp #include "Complex.h" Complex::Complex(void) { real = imag = (float) 0; } Complex::Complex(int i, int j) { real = (float) i; imag = (float) j; }

  14. Complex::Complex(float x, float y) { real = x; imag = y; } Complex::Complex(double x, double y) { real = (float) x; imag = (float) y; }

  15. void { Complex::read(void) cout <<"Enter real component\t?"; cin >> real; cout <<"Enter imaginary component\t?"; cin >> imag; } void { Complex::write(void) cout << "(" << real << ", " << imag << ")"; }

  16. Complex { Complex::operator + (Complex u) Complex w; w.real = real + u.real; w.imag = imag + u.imag; return w; } Complex { Complex::operator - (Complex u) Complex w; w.real = real - u.real; w.imag = imag - u.imag; return w; }

  17. Complex { Complex::operator * (Complex u) Complex w; w.real = this ->real * u.real - w.imag = this ->real * u.imag + this ->imag * u.real; return w; this -> imag * u.imag; }

  18. bool { Complex::operator == (Complex u) return (real == u.real && imag == u.imag); } bool { Complex::operator != (Complex u) return (real != u.real || imag != u.imag); }

  19. ComplexDemo.cpp The Main Program #include "Complex.h" int { main(void) Complex Complex v(2.0, 3.0); Complex w; u(1, 1); w = u + v; u.write(); cout << " + " ; v.write(); cout << " = ";

  20. w.write(); cout << endl; w = u - v; u.write(); cout << " - " ; v.write(); cout << " = "; w.write(); cout << endl; w = u * v; u.write(); cout << " * " ; v.write(); cout << " = ";

  21. w.write(); cout << "\n" << endl; if (u == u) cout << "u and u are equal" << endl; else cout << "u and u are not equal" << endl; if (u == v) cout << "u and v are equal" << endl; else cout << "u and v are not equal" << endl; cout << "\n" << endl;

  22. if (u != u) cout << "u and u are not equal" << endl; else cout << "u and u are equal" << endl; if (u != v) cout << "u and v are not equal" << endl; else cout << "u and v are equal" << endl; return(0); }

Related


More Related Content