-3

Example class....

class Student
{
    private: 
        int AmtClass;
    public:
        Student(int AmtClass);
}


Student::Student(int amount)
{
    AmtClass = amount;

    string *CName;
    CName = new string[AmtClass];
}

int main()
{
    int amount;
    string Names[amount];
    cin >> amount;
    Student stud(amount);

    for(int i = 0; i > amount; i++)
    {
        getline(cin,Names[i]);
        //How can I access the constructor from here?
    }
}

How do I access the array of strings inside the constructor? I want to put the course names inside the constructor.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Please submit code that actually compiles, so we have somewhere to start. – Keith Mar 26 '14 at 22:36
  • Don't think I can submit a code that actually compiles if I've never finished it yet, I am just asking how I can access my constructor inside the Int main. – user3347541 Mar 26 '14 at 22:37
  • 3
    I highly recommend you start using `std::vector` instead of `new[]`. – chris Mar 26 '14 at 22:37

2 Answers2

0

The CName variable is only declared in the local scope of your constructor mehtod:

Student::Student(int amount)
{
    AmtClass = amount;

    string *CName; // <<<
    pName = new string[AmtClass]; // <<< Just leaks memory, nothing else
}

You probably meant to have it being a class member variable. Also note there's no need for a string* pointer or new, just use a std::vector instead:

class Student
{
private: 
    int AmtClass;
    std::vector<std::string> > StudentNames;
public:
    Student(int numStudents);
}

Student::Student(int numStudents) 
: AmtClass(numStudents)
{
    StudentNames.resize(AmtClass);
}

After this constructor was called, you can access StudentNames[i] where i is in the range of [0-AmtClass[, in subsequently called methods from your Student class.

All of that said, this would also suggest to name your class Students to reflect the intended plural semantics correctly.
Or even better, this should be named Class holding a std::vector<std::shared_ptr<Student> >, where Student is supposed to have a std::string member name.

OK, here we go:

#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <algorithm>

class Student {
private:
    std::string name;

public:
    Student(const std::string& name_) : name(name_) {}
    const std::string& getName() const { return name; }
};

class Class {
private:
    typedef std::vector<std::shared_ptr<Student>> SubscriptionList;
    SubscriptionList students;
    const unsigned int MaxSubscribers;

public:
    Class(unsigned int maxSubscribers) : MaxSubscribers(maxSubscribers) {}

    bool subscribe(std::shared_ptr<Student>& student) {
        if(students.size() >= MaxSubscribers) {
            return false; // Class is full, can't subscribe
        }

        // May be put additional checks here, to prevent multiple subscrptions
        students.push_back(student);
        return true; // Sucessfully subscribed the class
    }

    void unsubscribe(std::shared_ptr<Student>& student) {
        SubscriptionList::iterator it = students.begin();
        for(;it != students.end();++it) {
            if(it->get() == student.get()) {
                break;
            }
        }
        students.erase(it);
    }

    void showSubscribedStudents(std::ostream& os) {
        unsigned int i = 1;
        for(SubscriptionList::iterator it = students.begin();
            it != students.end();
            ++it, ++i) {
            os << i << ". " << (*it)->getName() << std::endl;
        }
    }
};

int main()
{
    unsigned int amount;
    std::cin >> amount;
    std::cin.ignore();

    Class theClass(amount);

    for(unsigned int i = 0; i < amount; i++)
    {
        std::string name;
        std::getline(std::cin,name);
        std::shared_ptr<Student> student(new Student(name));
        theClass.subscribe(student);
    }

    theClass.showSubscribedStudents(std::cout);
}

Input:

5
John Doe
Jane Doe
Aldo Willis
Chuck Norris
Mia Miles

Output:

1. John Doe
2. Jane Doe
3. Aldo Willis
4. Chuck Norris
5. Mia Miles
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

You need to declare pName as member variable of the class. Then you can store the input in the pName array during taking the input.

class Student
{
    public:
        int AmtClass;
        string *pName;

        Student(int AmtClass);
};


Student::Student(int amount)
{
    AmtClass = amount;
    pName = new string[AmtClass];
}

int main()
{
    int amount;
    string Names;
    cin >> amount;
    getline(cin,Names);
    Student stud(amount);


    for(int i = 0; i < amount; i++)
    {
        getline(cin,Names);
        stud.pName[i] = Names;
        //How can I access the constructor from here?
    }
    return 0;
}
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • Yeah, I am trying to keep it close to the original code. The user can get the idea. He can use whatever he wants. – Shashwat Kumar Mar 26 '14 at 22:57
  • In this case, you should at least extend your answer's sample to be **fully** correct by means of the [rule-of-three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), and not leaking memory from the missing destructor! – πάντα ῥεῖ Mar 26 '14 at 23:06