Summary :
As part of the solution following are provided (1) Implementation Notes (ii) Code and Sample Output.
Notes :
Distance class is defined with 2 member variables of type int to hold the feet and inches and the third member which is special , is a static int member used for keeping track of how many instances are created of Class Distance.
The static member requires to be dealt specially , hence its declared again the cpp file and initialized to value 0 so that everytime a new member is created, the counstructor methods keep incrementing this member variable .
Further while additions are being performed as part of + or += operator overloading , inches are converted to feet if the count is > 12 .
And for the ++ operator ( ++Distance ) , the increment is only done for the feet , left code for incrementing the inches as well commented.
########## Code ################
>>>>> Distance.h <<<<<
#ifndef DISTANCE_H_
#define DISTANCE_H_
#include <iostream>
class Distance
{
static int cnt;
private:
int feet;
int inches;
protected:
void cntObject()
{
cnt++;
}
public:
//static int cnt;
Distance();
Distance(int feet, int inches);
void getDistance();
void setFeet(int ft);
void setInches(int in);
void showDistance() const;
int getFeet() const;
int getInches() const;
static int getcnt() { return cnt; };
void operator+=(const Distance&
other);
Distance& operator++();
bool operator < (const Distance&
other);
friend Distance operator+(const Distance&
d1, const Distance& d2);
friend std::ostream& operator<< (
std::ostream& out, const Distance& d1);
};
#endif /* DISTANCE_H_ */
>>>>>>>>>>> Distance.cpp <<<<<<<<<
#include <iostream>
#include "Distance.h"
using namespace std;
int Distance::cnt = 0;
// Default constructor
Distance::Distance()
{
cntObject();
this->feet = 0;
this->inches = 0;
}
// overloaded constructor which accepts distance in feet and
inches
Distance::Distance(int ft, int in)
{
cntObject();
this->feet = ft;
this->inches = in;
}
// Returns the distance in feet
int Distance::getFeet() const
{
return this->feet;
}
// Returns the inches portion of distance
int Distance::getInches() const
{
return this->inches;
}
// sets the feet values of the Distance object
void Distance::setFeet(int ft)
{
this->feet = ft;
}
// sets the inches portion of the distance object
void Distance::setInches(int in)
{
// incase the value is > 12 , it converts to
feet and addits to feet membe
this->inches = (in % 12);
this->feet = this->feet + (in / 12);
}
// Reads the value of Distance in feet and inches
void Distance::getDistance()
{
std::cout << " Enter Distance Feet followed by Inches (eg 5 9
) : ";
std::cin >> this->feet;
std::cin >> this->inches;
std::cout << " Read " << this->feet << " "
<< this->inches << "\n";
}
// Prints the Distance in ft'-inches" format to standard
output
void Distance::showDistance() const
{
cout << " " << feet << "'-" << inches
<<'"' << "\n";
}
// Overloads the + operator to add two distance objects and retuns
the result object
Distance operator+(const Distance& d1, const Distance&
d2)
{
int in = (d1.getInches() +
d2.getInches());
int ft = d1.getFeet() + d2.getFeet() + in /
12;
in = in % 12;
return Distance(ft, in);
}
// Overload the += operator and adds the given distance object to
the lhs object
void Distance::operator+=(const Distance& rhs)
{
int in = (this->inches +
rhs.getInches());
this->feet = this->feet + rhs.getFeet() +
in / 12;
this->inches = in % 12;
}
// Increments the rhs object by 1 feet
Distance& Distance::operator++()
{
//int in = this->inches + 1;
//this->inches = in % 12 ;
this->feet = this->feet + 1;
return *this;
}
// Implements the < operator and returns true if the lhs is
< rhs Distance
bool Distance::operator < (const Distance& other)
{
if (this->feet == other.getFeet())
{
return (this->inches
< other.getInches());
}
else
return (this->feet
< other.getFeet());
}
// Overloads the << operator to print the given Distance
object in ft'-in" format
ostream& operator<< (ostream& out, const
Distance& d1)
{
out << " " << d1.getFeet() <<
"'-" << d1.getInches() << '"' << "\n";
return out;
}
int main()
{
Distance d1;
d1.setFeet(1);
d1.setInches(2);
cout << " d1 : " << d1 << "\n";
Distance d11;
d11.getDistance();
d11 += d1;
cout << " and d11 += d1 = " << d11
<< "\n";
cout << "Total Objects created so far : " << Distance::getcnt() << "\n";
Distance d2(5,11);
Distance d3 = d1 + d2;
cout << "d1 : " << d1 << "
d2 : " << d2 << " and d3 = (d1 + d2 ) " << d3
<< "\n";
cout << " d2 : " << d2 ;
++d2;
cout << " and ++d2 = " << d2 << "\n";
cout << " d1 < d2 : " <<
(d1 < d2) << "\n";
cout << " d3 < d2 : " <<
(d3 < d2) << "\n";
cout << "Total Objects : "
<< Distance::getcnt();
}
############ Sample Output ###############
code in c++ Question 3 [30 Marks In architectural drawings, the distances are measured in feet...
Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template (20 pts) – TempDriver.cpp Template Exercise Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values. Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object). You will need: The FeetInches class (which was provided in Week...
So far I have this code but
I'm not sure if it's right or how to finish it.
#include "distance.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
Distance::Distance()
{
feet = 0;
miles = 0;
yards = 0;
inches = 0;
}
Distance::Distance(int inch)
{
}
Distance::Distance(int m, int y, int f, double i)
{
if(m < 0 || y < 0 || f < 0)
{
feet = 0;
miles = 0;
yards = 0;
}...
#include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...
C++
myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //class declaration 8 class stack Instructions Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same Overload the relational operatorfor the class stackType that returns true if two stacks of the same type are the same; it returns false...
Task:
Tasks to complete:
------------------------------------------------------------------------------------------------------------------------------------------
given code:
------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include
#include "rectangleType.h"
using namespace std;
// part e
int main()
{
rectangleType rectangle1(10, 5);
rectangleType rectangle2(8, 7);
rectangleType rectangle3;
rectangleType rectangle4;
cout << "rectangle1: " << rectangle1 <<
endl;
cout << "rectangle2: " << rectangle2 <<
endl;
rectangle3 = rectangle1 + rectangle2;
cout << "rectangle3: " << rectangle3 << endl;
rectangle4 = rectangle1 * rectangle2;
cout << "rectangle4: " << rectangle4 << endl;
if (rectangle1 > rectangle2)
cout << "Area...
write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...
Question 2 - Programming Exercise 1. Make a directory for this lab and change into it. 2. Copy files using the following command: cp/net/data/ftp/pub/class/115/ftp/cpp/Inheritance/Exercise.cpp Exercise.cpp Finish the program so that it compiles and runs. The instructions are contained in the C++ file. Your completed program should generate output similar to the following: TwoD default constructor This program asks for the coordinates of two points in 3D space and calculates their distance. Please enter the xyz coordinates for the first point:...
This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...
Please answer this question clearly and correctly! the code should
be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN
C++! thank you so much in advance and I will upvote when done
right!
(26 points total) Write an inheritance hierarchy for classes of simple shapes. 1. Create a class Shape. Derive the class ThreeDimensionalShape from the Shape class. Derive the class Sphere from the ThreeDimensionalShape class Use abstract classes to define Shape and ThreeDimensionalShape classes, and then...
8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....