Hi, can you check my code mycode.h that how can I make it seperate as .h file an
ID: 3886896 • Letter: H
Question
Hi, can you check my code mycode.h that how can I make it seperate as .h file and .cpp file
SortedArrayList.h
#include <iostream>
#include "ArrayList.h"
template <typename E>
class SortedArrayList: public ArrayList<E>{
public:
SortedArrayList(int capacity): ArrayList<E>(capacity) {
}
bool add(const E& elem){
if (this->n_+1> this->capacity_){
std::cout << "adding " << elem << " failed!" << std::endl;
return false;
}
int right_place = getRightPlace(elem);
// shift everything right
for (int i=this->n_-1; i>=right_place; i--)
this->listArray_[i+1] = this->listArray_[i];
this->listArray_[right_place] = elem;
this->n_++;
return true;
}
bool fastSearch(const E& elem){
return binarySearch(elem, 0, this->n_-1);
}
~SortedArrayList(){
}
protected:
int getRightPlace(const E& elem){
for (int i=0; i<this->n_; i++)
if (this->listArray_[i]>= elem)
return i;
return this->n_;
}
bool binarySearch(const E& elem, int start_index, int end_index){
if (start_index > end_index)
return false;
int middle_index = (start_index + end_index)/2;
if (this->listArray_[middle_index]>elem)
return binarySearch(elem, start_index, middle_index-1);
else if (this->listArray_[middle_index]<elem)
return binarySearch(elem, middle_index+1, end_index);
else // this->listArray_[middle_index]==elem
return true;
}
};
Mycode.h
#include <iostream>
#include <fstream>
using namespace std;
class myFavoriteDataType{
public:
myFavoriteDataType(){
name='p';
capacity=0;
}
myFavoriteDataType(string p, int n){
name=p;
capacity = n;
}
bool operator< (const myFavoriteDataType& rhs) const{
return (capacity > rhs.capacity);
}
bool operator== (const myFavoriteDataType& rhs) const{
return (capacity == rhs.capacity);
}
bool operator> (const myFavoriteDataType& rhs) const{
return !(((*this) < rhs) || ((*this == rhs)));
}
bool operator>= (const myFavoriteDataType& rhs) const{
return !((*this) < rhs);
}
bool operator<= (const myFavoriteDataType& rhs) const{
return !((*this) > rhs);
}
bool operator!= (const myFavoriteDataType& rhs) const{
return !((*this) == rhs);
}
friend ostream &operator<<( ostream &output, const myFavoriteDataType &p ) {
output << p.name<<", "<<p.capacity<<endl;
return output;
}
private:
string name;
int capacity;
};
Explanation / Answer
Notes: I broke you MyFavouriteDataType down to a .h and a .cpp, added appropriate names to files and classes. Your first piece of code though has some file dependencies, so I can't work with that.
MyDataType.h:
#ifndef MYDATATYPE_H_
#define MYDATATYPE_H_
#include <string>
using namespace std;
class MyDataType
{
private:
std::string name;
int capacity;
public:
MyDataType();
MyDataType(string p, int n);
bool operator < (const MyDataType& rhs) const;
bool operator == (const MyDataType& rhs) const;
bool operator > (const MyDataType& rhs) const;
bool operator >= (const MyDataType& rhs) const;
bool operator <= (const MyDataType& rhs) const;
bool operator != (const MyDataType& rhs) const;
friend ostream &operator <<( ostream &output, const MyDataType &p );
~MyDataType();
};
#endif /* MYDATATYPE_H_ */
MyDataType.h:
====
/*
* MyDataType.cpp
*
* Created on: 19-Sep-2017
* Author: ajay
*/
#include "MyDataType.h"
MyDataType::MyDataType()
{
name='p';
capacity=0;
}
MyDataType::MyDataType(string p, int n)
{
name=p;
capacity = n;
}
bool MyDataType::operator < (const MyDataType& rhs) const
{
return (capacity > rhs.capacity);
}
bool MyDataType::operator == (const MyDataType& rhs) const
{
return (capacity == rhs.capacity);
}
bool MyDataType::operator > (const MyDataType& rhs) const{
return !(((*this) < rhs) || ((*this == rhs)));
}
bool MyDataType::operator >= (const MyDataType& rhs) const{
return !((*this) < rhs);
}
bool MyDataType::operator <= (const MyDataType& rhs) const{
return !((*this) > rhs);
}
bool MyDataType::operator != (const MyDataType& rhs) const{
return !((*this) == rhs);
}
ostream &operator <<( ostream &output, const MyDataType &p ) {
//output << p.name<<", "<<p.capacity<<endl;
return output;
}
MyDataType::~MyDataType()
{}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.