Can Oezlemis
Can Oezlemis

Reputation: 212

Is it possible to use same functions for every class

I have multiple classes and each of them has their own methods. All of these methods perform the same task, as you can see in my code. The only unique thing is the values of the title, code and credit members that are defined inside the classes.

Is there a way to write this code such that a single set of methods can do the required tasks (using the specific values within the class that made the request to the method) for each and every class?

I'm a university student, and due to this I don't want to use inheritance since we haven't learned it yet.

class seng305
{
    string title = "Software design and architecture", code = "SENG305";
    int credit = 4;
public:
    seng305();
    ~seng305();
    string get_info();
    string get_title();
    int get_credit();
};


class comp219
{
    string title = "Electronics in computer engineering", code = "COMP219";
    int credit = 4;
public:
    comp219();
    ~comp219();
    string get_info();
    string get_title();
    int get_credit();
};

seng305::seng305()
{
    cout << '\t' << "Created" << endl;

}
seng305::~seng305()
{
    cout << '\t' << "Destroyed" << endl;
}
string seng305::get_info()
{
    return (code + "-" + title);
}
string seng305::get_title()
{
    return title;
}
int seng305::get_credit()
{
    return credit;
}
//--------------------------------------------------
comp219::comp219()
{
    cout << '\t' << "Created" << endl;

}
comp219::~comp219()
{
    cout << '\t' << "Destroyed" << endl;
}
string comp219::get_info()
{
    return (code + "-" + title);
}
string comp219::get_title()
{
    return title;
}
int comp219::get_credit()
{
    return credit;
}

As you can see, the get_info(), get_title(), and get_credit() methods do the same thing.

I would like for a single get_info(), get_title(), get_credit() to be able to do the task for each class.

Upvotes: 1

Views: 154

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595742

There is no reason to use separate classes at all in this example. A single class will suffice, eg:

class course
{
    string title, code;
    int credit;
public:
    course(const string &title, const string &code, int credit);
    ~course();
    string get_info() const;
    string get_title() const;
    int get_credit() const;
};

course::course(const string &title, const string &code, int credit)
    : title(title), code(code), credit(credit)
{
    cout << '\t' << "Created" << endl;
}

course::~course()
{
    cout << '\t' << "Destroyed" << endl;
}

string course::get_info() const
{
    return (code + "-" + title);
}

string course::get_title() const
{
    return title;
}

int course::get_credit() const
{
    return credit;
}

Then, you simply create instances of your class as needed, eg:

course seng305("Software design and architecture", "SENG305", 4);
course comp219("Electronics in computer engineering", "COMP219", 4);
...

I know you said that you don't want to use inheritance, but that could be the next logical step, using the above code as a base:

class courseSeng305 : public course
{
public:
    courseSeng305() : course("Software design and architecture", "SENG305", 4) {}
};

class courseComp219 : public course
{
public:
    courseComp219() : course("Electronics in computer engineering", "COMP219", 4) {}
};

courseSeng305 seng305;
courseComp219 comp219;
...

Upvotes: 6

Related Questions