Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class LengthsNotEqualException : exception {
- public:
- LengthsNotEqualException() {
- }
- const char* what() const noexcept {
- return "The lengths of two containers are not equal";
- }
- };
- class Container {
- private:
- int lenght;
- int* arr;
- public:
- Container() {
- cin >> lenght;
- arr = new int[lenght];
- for (int i = 0; i < lenght; i++)
- cin >> arr[i];
- }
- friend int dot_product(Container& a, Container& b) {
- if (a.lenght != b.lenght)
- throw LengthsNotEqualException();
- int c = 0;
- for (int i = 0; i < a.lenght; i++)
- c += a.arr[i] * b.arr[i];
- return c;
- }
- };
- int main()
- {
- Container a, b;
- try {
- cout << dot_product(a, b);
- }
- catch (LengthsNotEqualException & exception){
- cout << exception.what();
- }
- catch (exception & exception) {
- cout << exception.what();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement