Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Overview In terms of input and output Assignment 2 is the same as Assignment 1. The difference between Assignment 2 and Assignment 1

In C++

Overview

In terms of input and output Assignment 2 is the same as Assignment 1. The difference between Assignment 2 and Assignment 1 is how the code is implemented. In Assignment 2, some object-oriented constructs are used to enable some polymorphic run-time behaviours.

my assignmen1 code

#include

#include

using namespace std;

int main (){

initializing 3D vector

vector < vector < vector< int >>> vector_3d;

intx=2, y=3, z=4;

for(int 1=0; i

vector < vector< int >> v2d;

for(int j=0; j<=y; ++j){

vectorv1d;

for(int k=0; k

v1d.push_back(k);

}

v2d.push_back(v1d);

}

vector_3d.push_back(v2d);

}

for(int 1=0; i

for(int j=0; j

for(int k=0; k

count << vector_3d[i][j][k] << "";

}

count << end;

}

count << end

}

return 0;

}

Task

In Assignment 1 you wrote a three-dimensional vector class that can be (i) read from a stream, (ii) written to a stream, (iii) manually-initialized (i.e., likely aggregate initialization with defaulted constructors), and (iv) summed. In this assignment you will: (a) add an abstract base class capable of reading from and writing to streams; (b) create IOStream operator overloads so that anything derived from the base class can read from and written to such streams; (c) convert your vector class to be derived (virtually and publicly) from the base class; and (d) some other items addressed below.

Class Basis

Your base class must be defined to be abstract like this:

class basis { public: virtual ~basis() { } // i.e., ensure proper destructor is always called virtual std::istream& load(std::istream& is) = 0; // i.e., pure virtual virtual std::ostream& save(std::ostream& os) const = 0; // i.e., pure virtual }; 

and these are the ONLY two IOStream operator overloaded functions that are permitted to be defined in your assignment code:

inline std::ostream& operator<<(std::ostream& os, basis const& b) { return b.save(os); } inline std::istream& operator>>(std::istream& is, basis& b) { return b.load(is); } 

(You are advised to define these at the near the top of your program.)

Class vector3d

You wrote a three-dimensional vector struct or class in Assignment 1. You can keep the same name for it (vector3d will be used in this write-up to refer to such) but you must alter it as described below.

  • If your vector3d class was declared as a struct, change it to be a class and ensure your vector3d x, y, and z member variables (which are of type double) are all private in scope.
  • In your vector3d class, define a public default constructor that initializes the x, y, and z values to zero values BEFORE execution enters the constructor body (i.e., {}).
  • In your vector3d class, define a constructor accepting three double values (one each for x, y, and z) and use those arguments to set the constructed object's x, y, and z values BEFORE execution enters the constructor body.
  • Since the parent basis class is abstract, you must implement the load() and save() functions in your vector3d object. Ensure they are (in the) public (section) and have these prototypes (followed by their definitions of course):
std::istream& load(std::istream& is) override std::ostream& save(std::ostream& os) const override 

NOTE: You define these functions inside the class. The code should be almost what the code was in your operator<< and operator>> functions in Assignment 1 --except now you are not allowed to have those functions defined in Assignment 2 (see earlier). (The difference in terms of code will be that x, y, and z are part of the object --they are not passed in via an argument as they were in Assignment 1.)

  • Finally, to perform the summing of vectors, you will want at least one more function, e.g., an operator +() definition. There are a variety of ways of writing this --but in this assignment you must write it as a friend function to the class by first writing in your vector3d class:
friend vector3d operator+(vector3d const& a, vector3d const& b); 

ASIDE: A function or a class that is a friend of a type has access to all protected and private symbols of that type. This enables one to write the definition of such in a way that refers to private data members/functions instead of having to write public member functions, etc. in order to access such. Thus, you must write a definition of this operator +() function (completely outside of your vector3d class). (NOTE: Do not write friend in front of it when do this. The friend keyword is only needed in side the class. It tells the compiler that the operator +() function with those types is a friend to your vector3d class.)

main()

Your main() function must be:

int main() { unsigned count = 0; vector3d sum; for (vector3d v; std::cin >> v; ++count) sum = sum + v; basis& bsum = sum; // bsum uses sum polymorphically std::cout << "count: " << count << ' ' << "sum: " << bsum << ' ' ; } 

This main() is intentionally kept very simple: the summed object is used polymorphically via bsum. Since operator<< and operator>> are only defined to work with references to basis objects, this demonstrates the use of the pure virtual load() and save() functions in the basis class. Additionally, you need to write some constructors and an operator +() to make the rest of main() work.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

4. I can tell when team members dont mean what they say.

Answered: 1 week ago

Question

2. How were various roles filled?

Answered: 1 week ago