Question
C++ header file creating given main.cpp and Line.cpp Instructions: Write a header for a nested class called Point inside a class called Line. We have
C++ header file creating given main.cpp and Line.cpp
Instructions:
Write a header for a nested class called Point inside a class called Line. We have already included the main function and the classes definition in the moodle code runner. So you have to just write a header file for the classes. The Line class basically finds out the distance between points (x1,y1) and (x2,y2). We have also attached the main function and the Line.cpp in the moodle just for your reference. You can a look at it and build your class header with help of it.
Note: The class point should be nested inside the class line and the class point should contain necessary variables and a function called print which prints the values of x and y for the Point object.
The main.cpp file looks like this
#include#include "Line.hpp" using namespace std; int main() { Line myLine (2.1, 3.2, 4.5, 5.6); cout<<"I built a line with points: "; myLine.end0.print(); cout<<" and "; myLine.end1.print(); cout< Line.cpp looks like this
#include "Line.hpp" #includeLine::Line(double x1, double y1, double x2, double y2) { //ctor end0.x=x1; end0.y=y1; end1.x=x2; end1.y=y2; } Line::~Line() { //dtor } double Line::length() { return (sqrt(pow(end1.x-end0.x,2)+pow(end1.y-end0.y,2))); }
My naive header file implementation.
#ifndef LINE_H #define LINE_H
class Line { public: double num; class Point { public: double x; double y; double print(double num); }; double end0; double end1; Line(); Line(double, double, double, double);
double length(); };
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started