clase derivate

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
david_stelian
Junior
Junior
Posts: 20
Joined: 13 Sep 2014, 22:35
Judet: Dolj

clase derivate

Post by david_stelian » 16 May 2015, 15:59

Intrebare: Cum pot sa accesez metodele din clasa Manager, clasa derivata din Emloyee din afara clasei Manager? (vezi clasa Employee)
Metodele sunt declarate in Employee si accesate din afara clasei.

Code: Select all

#include <iostream>
#include <sstream>
using namespace std;

class Employee
{
protected:
    string name;
    double pay;
public:
    Employee();
    Employee(string, double);
    string getName();
    void setName(string);
    double getPay();
    void setPay(double);
    string toString();
    double grossPay(int);
};

class Manager:public Employee
{
private:
    bool salaried;
public:
    Manager(string name, double payRate, bool isSalaried):Employee(name, payRate)
    {
        salaried = isSalaried;
    }
    bool getSalaried()
    {
        return salaried;
    }
    double grossPay(int hours)
    {
        if (salaried)
            return pay;
        else
            return pay * hours;
    }
    string toString()
    {
        stringstream stm;
        string salary;
        if (salaried)
            salary = "Salaried";
        else
            salary = "Hourly";
        stm << name << ": " << pay << ": " << salary << endl;
        return stm.str();
    }
};

int main()
{
    Employee emp1("Mary Smith", 15.00);
    cout << "\n Employee name: " << emp1.getName() << endl;
    cout << "\n Employee pay rate: " << emp1.getPay() << endl;
    cout << "\n Employee gross pay: " << emp1.grossPay(40) << endl;
    Manager emp2("Bob Brown", 1500, true);
    cout << "\n " << emp2.toString();
    cout << "\n emp2's gross pay: " << emp2.grossPay(100) << endl;
    return 0;
}

Employee::Employee()
{
    name = "";
    pay = 0;
}
Employee::Employee(string empName, double payRate)
{
    name = empName;
    pay = payRate;
}
string Employee::getName()
{
    return name;
}
void Employee::setName(string empName)
{
    name = empName;
}
double Employee::getPay()
{
    return pay;
}
void Employee::setPay(double payRate)
{
    pay = payRate;
}
string Employee::toString()
{
    stringstream stm;
    stm << name << ": " << pay;
    return stm.str();
}
double Employee::grossPay(int hours)
{
    return pay * hours;
}



User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

Re: clase derivate

Post by Silviu Ardelean » 19 May 2015, 10:43

david_stelian wrote:Cum pot sa accesez metodele din clasa Manager, clasa derivata din Emloyee din afara clasei Manager?
Iarta-ma, poti sa ne repeti, mai exact, ce anume doresti sa faci? :)

david_stelian
Junior
Junior
Posts: 20
Joined: 13 Sep 2014, 22:35
Judet: Dolj

Re: clase derivate

Post by david_stelian » 02 Jun 2015, 10:21

Metoda "setPay()"a clasei Employee este accesata in afara clasei in felul urmator:
void Employee::setPay(double payRate) adica tipul metodei, clasa din care face parte, operatorul de rezolutie urmat de numele metodei.

Cum se poate accesa o metoda a clasei Manager in afara clasei ?
Spre exemplu pentru metoda double grossPay(int hours) am incercat la fel ca mai sus dar nu functioneaza:
double Manager::grossPay(int hours)


Imi cer scuze de intarziere.

User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

Re: clase derivate

Post by Silviu Ardelean » 02 Jun 2015, 10:46

david_stelian wrote:Metoda "setPay()"a clasei Employee este accesata in afara clasei in felul urmator:
void Employee::setPay(double payRate) adica tipul metodei, clasa din care face parte, operatorul de rezolutie urmat de numele metodei.
Asta nu e "apel de metoda". Are sens sa declari cu operatorul de rezolutie si numele clasei doar daca metoda setPay() e statica, cu mentiunea ca elimini void din fata.
La cum ai tu clasele, apelul lu' setPay() e ceva de genul:

Code: Select all

instanta.setPay(pay_rate_you_want_to_pass) // unde instanta = emp1 sau emp2 in cazul tau, in functie de ce doresti; ai deja pay_rate in cazul in care folosesti constructorul potrivit
david_stelian wrote: Cum se poate accesa o metoda a clasei Manager in afara clasei ?
Spre exemplu pentru metoda double grossPay(int hours) am incercat la fel ca mai sus dar nu functioneaza:
double Manager::grossPay(int hours)
Ce ai definit tu in interiorul claselor tine de implementarea interna a acelor obiecte (ce actiuni/metode si caracteristici/atribute) iar ce spui tu cu apelatul din "afara clasei" tine de apel pe metodele obiectelor. In cazul tau:

Code: Select all

emp2.grossPay(hoursNoDefinedByYou);

PS. Iti recomand sa te familiarizezi mai bine cu conceptele OOP si C++ in general.
Clasa Employee s-ar preta foarte bine sa fie una abstracta. Asta te va impiedica sa ai instante de genul "emp1" declarata de tine, dar te va "constrange" sa modelezi mai corect ierarhia ta de clase (ex. alte clase derivate din Employee - ex. worker, tesa, etc). Asta, desigur, daca nu vrei ca toti angajatii sa fie manageri. :)

Post Reply