#Dynamically allocating array using a class?

46 messages · Page 1 of 1 (latest)

sharp mountainBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

burnt echo
#
    test students;
    int studentNum;

    cout << "How many students will you be entering for? " << endl;
    cin >> studentNum;

    test *arr = new test(students);
#

that is what I have so far and I know its sloppy but it is confusing tf outta me

#

it is not having issues in the compiler but it looks... wrong to me

fresh shore
burnt echo
#

yes would you like me to post the header file?

fresh shore
burnt echo
#
#include <string>

class test {
private:
    std::string firstName;
    std::string lastName;
    double test1;
    double test2;
    double test3;

public:
    test();
    test(std::string firstName, std::string lastName, double test1, double test2, double test3);
    void setfirstName(std::string first_name);
    std::string getfirstName();
    void setlastName(std::string last_name);
    std::string getlastName();
    void setTest1(double test_1);
    double getTest1();
    void setTest2(double test_2);
    double getTest2();
    void setTest3(double test_3);
    double getTest3();
    double calcAverage();
};

/*******************************
 *        CONSTRUCTOR          *
 *******************************/
test::test(){
    firstName = "John";
    lastName = "Doe";
    test1 = 100.0;
    test2 = 100.0;
    test3 = 100.0;
}

test::test(std::string nameFirst, std::string nameLast, double firstTest, double secondTest, double thirdTest) {
    firstName = nameFirst;
    lastName = nameLast;
    test1 = firstTest;
    test2 = secondTest;
    test3 = thirdTest;
}

void test::setfirstName(std::string first_name) {
    firstName = first_name;
}

std::string test::getfirstName() {
    return firstName;
}

void test::setlastName(std::string last_name) {
    lastName = last_name;
}

std::string test::getlastName() {
    return lastName;
}

void test::setTest1(double test_1) {
    test1 = test_1;
}

double test::getTest1() {
    return test1;
}

void test::setTest2(double test_2) {
    test2 = test_2;
}

double test::getTest2() {
    return test2;
}

void test::setTest3(double test_3) {
    test3 = test_3;
}

double test::getTest3() {
    return test3;
}

double test::calcAverage() {
    return (test1 + test2 + test3) / 3;
}```
#

(I am sure you can already tell I am still a beginner)

#

haha

fresh shore
burnt echo
#

array

fresh shore
burnt echo
#

I am just trying to create an array of students from my class type, but making it dynamically as the initial number of students is unknown

fresh shore
burnt echo
#

we are not using vectors yet

fresh shore
burnt echo
#

ye my teacher has an "interesting" method of teaching

fresh shore
burnt echo
#

that is what i have been hearing, especially with "using namespace std"

burnt echo
#

lol

fresh shore
#

nvm i was a bit confused

burnt echo
#

yeah its kinda sloppy my bad

fresh shore
fresh shore
#

depends on what you wanna do

#

with it later

burnt echo
#

wait this would work?

#

it would prompt for all of the values of the data members

fresh shore
burnt echo
#

fuck lol

#

that is the whole point

fresh shore
#

but there is nothing coded here wich makes it dynamically resizable

#

sadly

burnt echo
#

yes you have said that

#

its alright man ill just figure it out myself, thanks for the attempt.

#

!solved

sharp mountainBOT
#

Thank you and let us know if you have any more questions!

sharp mountainBOT
#

@burnt echo

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.

fresh shore
#

@burnt echo here i made this resizable class to give you a idea of what to do

class V
{
    V* Next;
    int val;
public:
    V(int val) : val(val) {}
    int operator[](int Pos)
    {
        int currpos{0};
        V* Temp = this;
        if (currpos == Pos)return Temp->val;
        while (Next)
        {
            currpos++;
            Temp = Temp->Next;
            if (currpos == Pos)return Temp->val;
        }
        return 0;
    }

    void push_back(int val)
    {
        V* Temp = this;
        while (Next)Temp = Temp->Next;
        V* next = new V(val);
        Temp->Next = next;
    }
};
fresh shore
#

ah wait it has some issues

#

alr fixed

#

;compile

#include <iostream>

class V
{
    V* Next;
    int val=0;
public:
    V(int val) : val(val) {}
    V() = default;
    int operator[](int Pos)
    {
        int currpos{ 0 };
        V* Temp = this->Next;
        if (currpos == Pos)
            return Temp->val;
        while (Temp->Next)
        {
            currpos++;
            Temp = Temp->Next;
            if (currpos == Pos)return Temp->val;
        }
        return 0;
    }

    void push_back(int val)
    {
        V* Temp = this;
        while (Temp->Next)Temp = Temp->Next;
        V* next = new V(val);
        Temp->Next = next;
    }
};

int main()
{
    V myvec;
    for (int i = 0; i < 5; i++)myvec.push_back(i);
    for (int i = 0; i < 5; i++)std::cout << myvec[i] << "\n";
}
azure lichenBOT
#
Program Output
0
1
2
3
4