So I found this problem on Inheritance I am try to figure out. this is what's required:
Create a class called strMetric that will give information about a string. You should provide a default constructor and an overloaded constructor that takes a string as an argument.
Your string metric class should have the following functioality
A method called howLong that returns the length of the string
A method called vowelCnt that returns the number of vowels in a string
A method called charSum that returns the sum of all characters within the string
A method called upperCase that returns the number of upper case characters
A method called lowerCase that returns the number of lower case characters
You are to use the strMetric class as the derived class and use the string class as the base class.
NOTE:
Do not not create your own string class and derive from it. You are to use the string class that is part of the std namespace and defined in
I have been working this out, and this is what I have, (right now I'm only working on 1 of the methods until I figure out how to do this properly)
//// strmetric.h ////
#ifndef STRMETRIC
#define STRMETRIC
#include <string>
using namespace std;
class strMetric : public string
{
private:
public:
strMetric();
strMetric(string &s);
int howLong(string s);
};
#endif
//// strmetric.cpp ////
#include "strmetric.h"
strMetric::strMetric()
:string()
{
}
strMetric::strMetric(string &s)
:string(s)
{
}
int strMetric::howLong(string s)
{
return s.size();
}
/////main.cpp////
#include <iostream>
#include "strmetric.h"
strMetric testRun("Hello There");
int main()
{
cout << "Here is the sentence being tested " << endl;
cout << endl;
cout << testRun << endl;
cout << endl;
cout << "String length " << endl;
cout << endl;
cout << testRun.length(testRun) << endl;
cout << endl;
}
So am I doing this correctly, or am I way off base? I'm having a hard time getting my head around this. If I'm doing it incorrectly could somebody show me how to do it correctly, I don't need the whole thing, just the one part I started so I can get a good idea on what I should be doing, thanks!
stdclasses. Wrap them instead.std::string?stringas your member variable. It's not a good idea to inherit from it.std::stringmember and use it to do what you need, but you would not inherit from it. Your class is not a string, therefore should not inherit from it. However, this is all irrelevant in terms of your assignment as you have to do so. You can just politely tell your teacher that all of SO thinks s/he should re-evaluate the assignment as it is teaching you bad, bad things.