简体   繁体   中英

How do you make an object as a parameter for a function c++

I am trying to make an object as a parameter for my add() function in this code:

class EnterInfo 
{
protected:
    string name;
    int age;
    string birthmonth;
public:
    EnterInfo() 
    {

    }
    EnterInfo(string n, int a, string b) 
    {
        name = n;
        age = a;
        birthmonth = b;
    }
};
class CourseInfo 
{
protected:
    string title;
public:
    CourseInfo() 
    {

    }
    CourseInfo(string t) 
    {
        title = t;
    }
    void add() 
    {

    }
};

int main() 
{
    EnterInfo s1;
    CourseInfo c1;
    s1 = EnterInfo("Sand", 20, "Jan");
    c1 = CourseInfo(" x Records");
}

I want the add() function to gather all the data from the object "s1" and compact it into an array that I can access later. I could add, remove, or edit the data moving forward or even maybe make a new object "c2" which contains "y records" with the same s1 values ("sand", 20, "Jan"), however, I have no idea how to implement this in code.

 c1.add(s1); // assume s1 (EnterInfo): Sand 20 Jan
 c1.add(s2); // assume s2 (EnterInfo): Paul 23 Aug

this is the code I want to work with. I don't know how to make it work though. The end game would be this

c1.print();

output:

x records

Sand 20 Jan

Paul 23 Aug

create a vector of EnterInfo objects and put it in CourceInfo class. The code below does what you need:

#include <iostream>
#include <string>
#include <vector>
using namespace std;


class EnterInfo
{

public:
    string name;
    int age;
    string birthmonth;

    EnterInfo()
    {
        name = "";
        age = 0;
        birthmonth = "";
    }

  
    EnterInfo(string n, int a, string b)
    {
        name = n;
        age = a;
        birthmonth = b;
    }
};



class CourseInfo
{
protected:
    string title;
    vector<EnterInfo> info_vec;


public:
    CourseInfo()
    {
        title = "";
    }

    CourseInfo(string t)
    {
        title = t;
    }
    void add(const EnterInfo enterInfo)
    {
        this->info_vec.push_back(enterInfo);
    }
    void print() {

        cout << this->title << endl;
        for (const auto& it : this->info_vec) {
            cout << it.name << " " << it.age << " " << it.birthmonth << endl;
        }
    }
};

int main()
{
    EnterInfo s1("Sand", 20, "Jan");
    EnterInfo s2("Arash", 21, "Feb");
    CourseInfo c1(" x Records");

    c1.add(s1);
    c1.add(s2);

    c1.print();
}

Side notes:
1- It's better to assign default values to the members of your class in the constructor.
2- I changed the access level of your EnterIndo members into public in order to use them in add function but the standard way is to set them to private and create getters and setters for them.

Research about std::vector and get/setters if you are not familiar with them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM