×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to check if the character is vowel or consonant using class

Given a character, we have to check whether it is a vowel or consonant using class and object approach.
Submitted by Shubh Pachori, on August 04, 2022

Problem statement

Given a character, we have to check whether it is a vowel or consonant using class and object approach.

Example:

Input: 'a'
Output: 'a' is a vowel.

C++ code to check if the character is vowel or consonant using class and object approach

#include <iostream>
using namespace std;

// create a class
class Vowel {
  // private char data member
 private:
  char character;

  // public function with a char type parameter
 public:
  void vowel(char c) {
    // copying value of parameter in data member
    character = c;

    // if condition to check whether the character
    // is a alphabet or not in both upper and lower case
    if ((character >= 'a' && character <= 'z') ||
        (character >= 'A' && character <= 'Z')) {
      // if condition to check if the character
      // is a vowel or not in both upper and lower case
      if ((character == 'a') || (character == 'A') || (character == 'e') ||
          (character == 'E') || (character == 'i') || (character == 'I') ||
          (character == 'o') || (character == 'O') || (character == 'u') ||
          (character == 'U')) {
        cout << character << " is a vowel." << endl;
      }

      // else for printing if it is not a vowel
      else {
        cout << character << " is a consonant." << endl;
      }
    }

    // else for printing if it is not a alphabet
    else {
      cout << character << " is not a character." << endl;
    }
  }
};

int main() {
  // create a object
  Vowel V;

  // a char type variable to store character
  char character;

  cout << "Enter Character: ";
  cin >> character;

  // calling function using object
  V.vowel(character);

  return 0;
}

Output

RUN 1:
Enter Character: i
i is a vowel.

RUN 2:
Enter Character: x
x is a consonant.

RUN 3:
Enter Character: @
@ is not a character.

Explanation

In the above code, we have created a class Vowel, one char type data member character to store the character, and a public member function vowel() to check the character.

In the main() function, we are creating an object V of class Vowel, reading a character by the user, and finally calling the vowel() member function to check the given character. The vowel() function contains the logic to check the given character whether it is a vowel or consonant and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.