Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is for C++ only!!! The Expected output is what is below this line, so what you should get after pointing in all of the

This is for C++ only!!! The Expected output is what is below this line, so what you should get after pointing in all of the codes in. The ??? are the ones that actually need coding.

Expected Output of Program:

line1 is: m_p1 = (m_x = 0, m_y = 0), m_p2 = (m_x = 0, m_y = 0)

line2 is: m_p1 = (m_x = 100, m_y = 200), m_p2 = (m_x = -300, m_y = -400)

line3 is: m_p1 = (m_x = -150, m_y = 250), m_p2 = (m_x = 350, m_y = -450)

line3 is: m_p1 = (m_x = 350, m_y = -450), m_p2 = (m_x = -150, m_y = 250)

line3 slope is: -1.4

line4 slope is: -999999999.9

BEGINNING OF ACTUAL CODING

Lab11.cpp

#include "TestLine.hpp"

//-------------------------------------------------------------------------------------------------- // FUNCTION: main() // // DESCRIPTION // This is the starting point for the program. // // PSEUDOCODE // Define a test_line object named 'tester' // Call tester.run() // Return 0 //-------------------------------------------------------------------------------------------------- ???

Line.cpp

??? // For stringstream class declaration ??? // Must be included so the compiler will see the line class declaration.

using namespace std;

// Define a constant double named SLOPE_INFINITE which is equivalent to -999999999.9. This value is // returned by the line::slope() function if the slope of the line is undefined (because it is a // vertical line). See comments in Line.hpp concerning the difference between constant declaration // and definition. It is an important concept to understand. const double SLOPE_INFINITE = -999999999.9;

//-------------------------------------------------------------------------------------------------- // FUNCTION: line() // // DESCRIPTION // Default constructor. Initializes the two point data members, m_p1 and m_p2, to both be at the // origin. The point class default constructor initializes the point object being instantiated to // be at the origin. // // PSEUDOCODE // Define point object p1 calling the default ctor to put p1 at the origin // Define point object p2 calling the default ctor to put p2 at the origin // Call init() passing p1 and p2 as the args //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: line(point, point) // // DESCRIPTION // Second constructor. Initializes the point data members, m_p1 and m_p2, to the input parameters. // // PSEUDOCODE // Call init() passing init_p1 and init_p2 as the args //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: line(int, int, int, int) // // DESCRIPTION // Initializes the point data members, m_p1 and m_p2, so that m_p1 is at (x1, y1) and m_p2 is at // (x2, y2). // // PSEUDOCODE // Define point object p1, passing x1 and y1 as ctor args // Define point object p2, passing x2 and y2 as ctor args // Call init() passing p1 and p2 as args //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: get_p1() // // DESCRIPTION // Accessor function for the m_p1 data member. // // PSEUDOCODE // Return m_p1 //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: get_p2() // // DESCRIPTION // Accessor function for the m_p2 data member. // // PSEUDOCODE // Return m_p2 //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: init(point, point) // // DESCRIPTION // Called from the ctors to initialize the line object being instantiated. // // PSEUDOCODE // Call set_p1(init_p1) // Call set_p2(init_p2) //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: set_p1(point) // // DESCRIPTION // Mutator function for the m_p1 data member. // // PSEUDOCODE // Assign p1 to m_p1. //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: set_p2(point) // // DESCRIPTION // Mutator function for the m_p2 data member. // // PSEUDOCODE // Assign p2 to m_p2. //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: slope() // // DESCRIPTION // Calculates and returns the slope of this line. Note that the slope is infinite if the line is // vertical. Since it would be bad to divide by 0 (the program is likely to crash), then we have // to check to see if the line is vertical before calculating the slope. If the slope is undefined // then we return SLOPE_INFINITE. // // PSEUDOCODE // Define int vars: delta_x, delta_y // Define double var: slope (init slope to SLOPE_INFINITE) // If m_p1.get_x() is not equal to m_p2.get_x() then // delta_x = m_p1.get_x() - m_p2.get_x() // delta_y = m_p1.get_y() - m_p2.get_y() // slope = ConvertToDouble(delta_y) / delta_x // End If // Return slope //-------------------------------------------------------------------------------------------------- ???

//-------------------------------------------------------------------------------------------------- // FUNCTION: to_string() // // DESCRIPTION // Returns a string representation of this line so we can print it. // For a similar example see point::to_string(). // // PSEUDOCODE // Define stringstream object named sout, calling the default ctor // Send "m_p1 = (", m_p1.to_string(), "), " to sout // Send "m_p2 = (", m_p2.to_string(), ")" to sout // Return the string that is returned after calling sout.str() //-------------------------------------------------------------------------------------------------- ???

Line.hpp

// Write the required header file preprocessor include guard ??? ???

// For string class declaration

// We have to include the point class header file so the compiler will "see" the point class declar- // ation. ???

using namespace std;

// The reserved word "extern" is used to *declare* an identifier that is *defined* in another source // code file. The purpose of this declaration (below) is to specify that in some other file, there // is a constant, named SLOPE_INFINITE, that is defined (in fact, it is defined in Line.cpp). By // writing the declaration here, when this header file is included (e.g., in TestLine.cpp), the com- // piler will see the declaration and it will know that SLOPE_INFINITE is a defined, named constant // and it will allow us to use that constant in TestLine.cpp. Without the declaration, when we try // to access or use SLOPE_INFINITE in TestLine.cpp, the compiler will complain that it does not know // what SLOPE_INFINITE is. Notice that the syntax of a constant declaration is: // // extern const datatype constantname; // // and the syntax of the constant definition is (go look in Line.cpp), // // const datatype constantname = constantvalue; // // So, the differences are: (1) a declaration starts with the word "extern" and their is no assign- // ment to the constant; and (2) the definition lacks the word "extern" but the constant is assigned // a value when it is defined. extern const double SLOPE_INFINITE;

// Write the code that declares the line class using the UML class diagram as a guide. ???

// Write the preprocessor directive which ends the include guard. ???

Testline.cpp

#include // For fixed, setprecision() #include // For cout, endl #include "TestLine.hpp" // Includes the test_line class declaration. #include "Line.hpp" // Includes the line class declaration.

using namespace std;

//-------------------------------------------------------------------------------------------------- // FUNCTION: test_line() // // DESCRIPTION // Default constructor. Does nothing since there are no data members to be initialized, but every // class must have at least one constructor. This constructor will be called when a test_line object // is instantiated by writing a statement such as: test_line tester; //-------------------------------------------------------------------------------------------------- test_line::test_line() { }

//-------------------------------------------------------------------------------------------------- // FUNCTION: run() // // DESCRIPTION // Tests the implementation of the line class. Read the comments below which will instruct you on // how to write the code. //-------------------------------------------------------------------------------------------------- void test_line::run() { // Test that default constructor creates a line object at (0,0)-(0,0). Instantiate a line object // named line1. Then call line1.to_string() and send the returned string to cout. Your program // should display, // // line1 is: m_p1 = (m_x = 0, m_y = 0), m_p2 = (m_x = 0, m_y = 0) // // on the console window when line1 is output. If the output is incorrect, then check these // functions for bugs: line::line(), line::init(point, point), line::set_p1(point), and line:: // set_pt(point). Also, check line::to_string() and point::to_string() for mistakes. ??? ???

// Test that the second constructor -- line(point, point) -- works correctly. Instantiate two // point objects p1 at (100, 200) and p2 at (-300, -400). Then, instantiate a line object named // line2 and pass p1 and p2 as as args to the ctor. Then call line2.to_string() and send the // returned string to cout like you did for line1. Verify that line2's data members were prop- // erly initialized. ??? ??? ???

// Test that the third constructor -- line(int, int, int, int) -- works correctly. Define and // instantiate a line object named line3, passing -150, 250, 350, and -450 as args to the ctor. // Then, call line3.to_string() and send the returned string to cout, like you did above. Verify // line3's data members were properly initialized. ??? ???

// Verify that the accessor/mutator functions set_p1(point), get_p1(), set_p2(point), and // get_p2() work correctly. Implement this pseudocode: // // Define a point object named p3_1 and call line3.get_p1() to retrieve the first endpoint of // the line line3. Assign the return value from line3.get_p1() to the point object p3_1. // Define a point object named p3_2 and call line3.get_p2() to retrieve the second endpoint of // the line line3. Assign the return value from line3.get_p2() to the point object p3_2. // Change the first endpoint of line3 to be p3_2 by calling line3.set_p1(p3_2). // Change the second endpoint of line3 to be p3_1 by calling line3.set_p2(p3_1). // Call line3.to_string() and send the returned string to cout. Verify the functions worked // correctly. ??? ??? ??? ??? ???

// Test that the slope() function works correctly. The slope of line3 should be -1.4. Call // slope() on line3 and send the return value to cout. Verify the slope is correctly calculated. ???

// Test that the slope() function correctly returns SLOPE_INFINITE when the line is vertical. // Implement this pseudocode: // // Configure cout to display real numbers in fixed notation with 1 digit after the decimal point. // Define a new line object named line4 and initialize the two endpoints of the line to be // (100, 0) and (100, 200). // Call line4.slope() and send the returned value to cout. Verify that -999999999.9 (the value // of INFINITE_SLOPE) is output. ??? ??? ??? }

TestLine.hpp

#ifndef TESTLINE_HPP #define TESTLINE_HPP

// Class declaration for a class named 'test_line'. The class only has one constructor, the default // constructor. The run() function is called to perform the testing of the line class. class test_line { public: test_line(); // Default constructor void run(); // Performs the testing of the line class };

#endif

Point.cpp

#include  // For sqrt() #include  // For streamstring class // Note that we must include "Point.hpp" because that header file contains the // point class declaration. In order for the compiler to be able to compile // these function definitions, it must first compile the class declaration. // // Note: For C++SL header files, we enclose the filename in angle brackets, e.g, // // #include  // // For programmer-defined header files, we enclose the filename in double quote // marks as shown below. The <> tell the compiler to look in the directory where // the C++SL header files are stored for the header file to be included. The // double quote marks tell the compiler to look in the same folder as your .cpp // files for the header file. #include "Point.hpp" //----- point() ---------------------------------------------------------------- // This is the default constructor for the point class. The default ctor of a // class is the one that gets called with no arguments when an object is in- // stantiated. For example, to create a point object named p1 and to call this // ctor to initialize the data members, we write, // // point p1; // // Note that it would be a mistake to write, // // point p1(); // // Because that is interpreted by the compiler as a function prototype for a // function named p1 that has no arguments and returns a point object. In gener- // al, if C is a class which has a default ctor, then to create an object which // is an instance of C and call the default ctor on the object, we write, // // C obj; // // The job of a default ctor is to initialize some or all of the data members of // the object being instantiated to known default values. Otherwise, if the data // members are not initialized in the ctor, then they contain garbage values. // // This ctor initializes the point to be at the origin (0, 0). //------------------------------------------------------------------------------ point::point() { // Call init() to initialize m_x and m_y to 0 and 0. init(0, 0); } //----- point(int, int) -------------------------------------------------------- // A second ctor. Initializes the m_x and m_y data members to the input params. //------------------------------------------------------------------------------ point::point(int init_x, int init_y) { // Call init() to initialize m_x and m_y to init_x and init_y. init(init_x, init_y); } //----- distance() ------------------------------------------------------------- // Calculates the distance between this point (i.e., the one on which the func- // tion was called) and the input parameter point, p2. Note that when this func- // tion is called, the point argument is passed by-reference; this is done be- // cause it is faster to pass objects by-reference than by-value. //------------------------------------------------------------------------------ double point::distance(point& p2) { int delta_x = get_x() - p2.get_x(); int delta_y = get_y() - p2.get_y(); return sqrt(delta_x * delta_x + delta_y * delta_y); } //----- get_x() ---------------------------------------------------------------- // Accessor function for the m_x data member. The job of an accessor function is // to provide "read" access to a private data member, so an accessor function // always contains one return statement. //------------------------------------------------------------------------------ int point::get_x() { return m_x; } //----- get_y()----------------------------------------------------------------- // Accessor function for the m_y data member. See comments in point::get_x(). //------------------------------------------------------------------------------ int point::get_y() { return m_y; } //----- init(int, int) --------------------------------------------------------- // This private function is called from the ctors to initialize the data members // of the point object being instantiated. //------------------------------------------------------------------------------ void point::init(int init_x, int init_y) { set_x(init_x); set_y(init_y); } //----- move(int, int) --------------------------------------------------------- // Moves the point to the new (x, y) coordinates specified by the input params. //------------------------------------------------------------------------------ void point::move(int new_x, int new_y) { set_x(new_x); // Call the mutator function to set m_x to new_x. set_y(new_y); // Call the mutator function to set m_y to new_y. } //----- set_x(int) ------------------------------------------------------------- // Mutator function for the m_x data member. The job of a mutator function is // to provide "write" access to a private data member, so a mutator function // always contains one assignment statement which assigns the parameter vari- // able to the data member variable. //------------------------------------------------------------------------------ void point::set_x(int new_x) { m_x = new_x; } //----- set_y(int) ------------------------------------------------------------- // Mutator function for the m_y data member. See comments in set_x(). //------------------------------------------------------------------------------ void point::set_y(int new_y) { m_y = new_y; } //----- to_string() ------------------------------------------------------------ // Returns a string representation of this point. This function is primarily // useful during debugging, as it allows us to easily print the values of the // object's data members. //------------------------------------------------------------------------------ string point::to_string() { stringstream sout; sout << "m_x = " << get_x() << ", m_y = " << get_y(); return sout.str(); } 

Point.hpp

#ifndef POINT_HPP #define POINT_HPP #include  using namespace std; class point { public: point(); // Default constructor, called with no args point(int init_x, int init_y); // Secondary constructor double distance(point& p2); // Calcs distance between this point and p2 int get_x(); // Accessor function for m_x data member int get_y(); // Accessor function for m_y data member void move(int new_x, int new_y); // Moves the point to a new coordinate void set_x(int new_x); // Mutator function for m_x data member void set_y(int new_y); // Mutator function for m_y data member string to_string(); // Returns a string representation of the point private: void init(int init_x, int init_y); // Called from ctors to init the point int m_x; // The x coordinate of the Point int m_y; // The y coordinate of the Point }; #endif 

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 X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

1. In what ways has flexible working revolutionised employment?

Answered: 1 week ago