Design the class Vowel to find the number of vowels present in the given string. Define the constructor member function to assign the values and destructor member function to destroy the data objects
#include <string>
#include <iostream>
using namespace std;
class Vowel
{
public:
Vowel(string data_);
~Vowel();
string GetData() { return data; }
int GetCount() { return count; }
private:
string data;
int count;
};
Vowel::Vowel(string data_) : data(data_), count(0)
{
string vow = "AEIOUYaeiouy";
for (int i = 0; i < data.size(); i++)
{
for (int j = 0; j < vow.size(); j++)
{
if (data[i] == vow[j])
{
count++;
break;
}
}
}
}
Vowel::~Vowel()
{
}
int main()
{
string temp;
cout << "Enter new string: ";
cin >> temp;
Vowel myObj(temp);
cout << myObj.GetData() << " contains " << myObj.GetCount() << " vowels." << endl;
return 0;
}
Comments
Leave a comment