Accessor inside a child class

Hi,

Could anyone help me out with creating an accessor function that will allow me to work with a private parent double inside a child class .cpp file?
I understand that making the double protected would be the best solution, but it's one of the homework rules.

Here is the parent class...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
#include <iomanip>
#ifndef PRODUCT_PRODUCT_H
#define PRODUCT_PRODUCT_H


class Product {
private:
    std::string type;
    double taxPercentage;
    double price;
protected:
    int quantity;
    std::string name;
    double calculateCost();
public:
    Product();
    Product(std::string name, std::string type, double price, int quantity);
    virtual void print();
    virtual void sellItem();
    void restock(int amount);
    void setTaxRate(double taxPercent);
};


#endif //PRODUCT_PRODUCT_H 


**edit** the variable I need is the price one.
Last edited on

If it's just a simple accessor ("getter") you need, then something like:

1
2
3
4
5
6
7
8
9
class Product {

  // ...

  public:
    double getPrice();
   
  // ....
};


In Product cpp file:
1
2
3
4
double Product::getPrice()
{
    return price;
}


If you wish for child classes to override this for polymorphism, mark it as virtual.
Topic archived. No new replies allowed.