im trying to figure out part of this assignment i was given in c++. the teacher
ID: 3881543 • Letter: I
Question
im trying to figure out part of this assignment i was given in c++. the teacher only gave me the header file with a few instructions. im stuck on the two different kinds of constructors. this is what he gave me
besically it is supposed to count the number of each letter in the vector or string and keep a count of each in a private member array called count[26] where counts[0] is supposed to keep track of the count of a's and counts[1] counts the number of b's and so on. if the number is uppercase im supposed to convert it to lowercase ascii somehow by adding 32 to it. i have no clue how to do either of the above constructors, please help. if you need it the rest of the header and implementation file i have so far are below. the header file is correct forsure, but some of the cpp file is probably wrong and my main is empty. please and thank you :)
//////////// .h
#ifndef LETTERBAG_H
#define LETTERBAG_H
#include <vector>
#include <iostream>
using namespace std;
class LetterBag
{
public:
LetterBag();
LetterBag(const vector<char> & v);
LetterBag(const string & s);
int getCurrentSize() const;
bool isEmpyty() const;
LetterBag operator+(char ch) const;
LetterBag operator+=(char ch);
LetterBag operator+(const LetterBag & other) const;
LetterBag operator+=(const LetterBag & other);
LetterBag operator-(char ch) const;
LetterBag operator-=(char ch);
LetterBag operator-(const LetterBag & other) const;
LetterBag operator-=(const LetterBag & other);
void removeAll(char ch);
void clear();
int getFrequency(char c) const;
vector<char> toVector() const;
string toString() const;
bool operator<(const LetterBag & other);
bool operator<=(const LetterBag & other);
bool operator>(const LetterBag & other);
bool operator>=(const LetterBag & other);
bool operator==(const LetterBag & other);
bool operator!=(const LetterBag & other);
private:
int counts[26];
static bool inRange(char ch);
int size;
};
#endif // LETTERBAG_H
///////// .cpp
#include "LetterBag.h"
LetterBag::LetterBag()
{
}
LetterBag::LetterBag(const vector<char> & v)
{
for (unsigned int i = 0; i < v.size; i++)
{
if v[i] ==
counts[i] = v[i];
}
}
LetterBag::LetterBag(const string & s)
{
return
}
int LetterBag::getCurrentSize() const
{
return this->size;
}
bool LetterBag::isEmpyty() const
{
if this->size == 0
{
return 1;
}
}
LetterBag::LetterBag operator+(char ch) const
{
return
}
LetterBag::LetterBag operator+=(char ch)
{
return
}
LetterBag::LetterBag operator+(const LetterBag & other) const
{
return
}
LetterBag::LetterBag operator+=(const LetterBag & other)
{
return
}
LetterBag::LetterBag operator-(char ch) const
{
return
}
LetterBag::LetterBag operator-=(char ch)
{
return
}
LetterBag::LetterBag operator-(const LetterBag & other) const
{
return
}
LetterBag::LetterBag operator-=(const LetterBag & other)
{
return
}
void LetterBag::removeAll(char ch)
{
for
}
void LetterBag::clear()
{
}
int LetterBag::getFrequency(char c) const
{
return
}
vector<char> LetterBag::toVector() const
{
}
string LetterBag::toString() const
{
}
bool LetterBag::operator<(const LetterBag & other)
{
if
{
return 1;
}
}
bool LetterBag::operator<=(const LetterBag & other)
{
if
{
return 1;
}
}
bool LetterBag::operator>(const LetterBag & other)
{
if
{
return 1;
}
}
bool LetterBag::operator>=(const LetterBag & other)
{
if
{
return 1;
}
}
bool LetterBag::operator==(const LetterBag & other)
{
if
{
return 1;
}
}
bool LetterBag::operator!=(const LetterBag & other)
{
if
{
return 1;
}
}
static bool inRange(char ch)
{
if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch<= 122))
{
return 1;
}
}
Explanation / Answer
PROGRAM CODE:
LetterBag.h
#ifndef LETTERBAG_H
#define LETTERBAG_H
#include <vector>
#include <iostream>
using namespace std;
class LetterBag {
private:
int count[26];
int size;
static bool inRange(char ch);
void increment(char letter);
public:
LetterBag() ; // no-parameter constructor for an empty LetterBag.
// initialize counts using letters in v, omitting non letters.
// e.g. v = {'6','A','b','C','a','G','g','G','g','B','b','%','g'}
// means counts = {2,3,1,0,0,0,5,...} and size = 10.
LetterBag(const vector<char> & v);
// similar to the vector constructor but using a string to initialize.
LetterBag(const string & s);
void clear();
};
#endif // LETTERBAG_H
LetterBag.cpp
/*
* LetterBagImpl.cpp
*
* Created on: 01-Feb-2018
* Author: kasturi
*/
#include "LetterBag.h"
using namespace std;
//constructor without any argumnets
LetterBag::LetterBag()
{
clear();
}
//clear functions clears the entire array
void LetterBag::clear()
{
for(int i=0; i<26; i++)
{
count[i] = 0;
}
size = 0;
}
LetterBag::LetterBag(const string &s)
{
for(int i=0; i<s.length(); i++)
{
if(inRange(s.at(i)))
increment(s.at(i));
}
}
void LetterBag::increment(char letter)
{
if((letter >= 'A' && letter <= 'Z'))
letter = letter+32;
int index = (int )letter - 97;
count[index]++;
size++;
}
bool LetterBag::inRange(char ch)
{
if((ch >= 'a' && ch <= 'z'))
return true;
else if((ch >= 'A' && ch <= 'Z'))
return true;
else return false;
}
LetterBag::LetterBag(const vector<char> & v)
{
for(int i=0; i<v.size(); i++)
{
if(inRange(v[i]))
increment(v[i]);
}
}
int main()
{
LetterBag bag1;
}
Since only the constructor was asked to be implemented in the question, I have chosen all the methods that are required for it to work. Also added a private method to increment the counter.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.