example3_overloading_constructors.cpp

 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
36
37
38
39
#include <iostream>
#include <initializer_list>
#include <vector>
#include <string>

class Team {
private:
    std::string name;
    std::vector<std::string> members;

public:
    Team(const std::string& teamName) : name(teamName) {
        std::cout << "Team created with name only" << std::endl;
    }

    Team(const std::string& teamName, std::initializer_list<std::string> memberList)
        : name(teamName), members(memberList) {
        std::cout << "Team created with name and member list" << std::endl;
    }

    void display() const {
        std::cout << "Team: " << name << std::endl;
        std::cout << "Members: ";
        for (const auto& member : members) {
            std::cout << member << " ";
        }
        std::cout << std::endl;
    }
};

int main() {
    Team team1("Avengers");
    team1.display();

    Team team2("Justice League", {"Superman", "Batman", "Wonder Woman"});
    team2.display();

    return 0;
}
Back to initializer_list