going through the alphabet using object arrays

So I was watching a lecture about allocating memory and the instructor gave a challenge where i have to make a program that goes through the alphabet a - z
using classes,objects,arrays,pointers, and new. I figured out how to loop it through the object array 26 times. But it outputs 'a' 26 times instead of going through the alphabet like i wanted. the instructor said that if you do
char c = 'a'; and c++; The 'a' jumps to 'b' and so forth(as a hint to the challenge). I've tried my best to do this challenge on my own based on what i've learned. When i think about it I could probably just write this without the for loop if i do setName('a') 25 more times all the way to z and call speak 26 times. But i find that inefficient. So How do i fix my source so it goes through the alphabet using char = 'a'; and c++; somehow ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

class Animal {
private:
    string a;
public:
    Animal() {cout << "constructor" << endl;}
    ~Animal() {cout << "Destructor" << endl;}
    void setName(string a) {this->a = a;}
    void speak() const { cout << a << endl;}


};

int main()
{
    Animal *Alphabet = new Animal[26];


    for(int i = 0;i < 26;i++) {

    char c = 'a';
    string name(1, c);
    Alphabet[i].setName(name);
    Alphabet[i].speak();
    c++;
    }

    delete  [] Alphabet;

    return 0;
}
Last edited on
put line 24 char c = 'a' before the for loop.
wow so simple and it worked thank you!

Last edited on
Topic archived. No new replies allowed.