adspace
write a program using virtual function to find the
transposing of a square matrix?
Answer Posted / Ashish Kumar Kashyap
In C++, you can use virtual functions in combination with inheritance to implement the transpose of a square matrix. Here's an example:n```cppnclass Matrix {npublic:n int **data;n int rows, cols;n // constructorn Matrix(int r, int c) : rows(r), cols(c) {n data = new int*[rows];n for (int i = 0; i < rows; ++i)n data[i] = new int[cols];n }n // destructorn ~Matrix() {n for (int i = 0; i < rows; ++i) delete[] data[i];n delete[] data;n }n // virtual function to transpose the matrixn virtual void transpose() = 0;n};n// derived class implementing the transpose functionnclass SquareMatrix : public Matrix {npublic:n SquareMatrix(int size) : Matrix(size, size) {}n void transpose() {n for (int i = 0; i < rows; ++i)n for (int j = i + 1; j < cols; ++j)n std::swap(data[i][j], data[j][i]);n }n};
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Question 1: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date. *This Should Be Done IN C++